В продвигателях используются дешифрованные значения из .env-файла #19
This commit is contained in:
parent
47cbb7905a
commit
d0ac64d244
@ -9,7 +9,7 @@ class Command(BaseCommand):
|
|||||||
environments = {}
|
environments = {}
|
||||||
with open(BASE_DIR / '.env') as env_file:
|
with open(BASE_DIR / '.env') as env_file:
|
||||||
for line in env_file:
|
for line in env_file:
|
||||||
env_key, env_value = line.split('=')
|
env_key, env_value = line.strip().split('=')
|
||||||
environments[env_key] = env_value
|
environments[env_key] = env_value
|
||||||
|
|
||||||
signer = signing.Signer(salt=SALT)
|
signer = signing.Signer(salt=SALT)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import abc
|
import abc
|
||||||
import os
|
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from cms.models import Article
|
from cms.models import Article
|
||||||
|
from crossposting_backend.settings import promoter_secrets
|
||||||
|
|
||||||
|
|
||||||
class PromoteError(Exception):
|
class PromoteError(Exception):
|
||||||
@ -21,13 +21,13 @@ class Promoter(abc.ABC):
|
|||||||
|
|
||||||
class TelegramPromoter(Promoter):
|
class TelegramPromoter(Promoter):
|
||||||
def promote(self):
|
def promote(self):
|
||||||
from crossposting_backend.settings import BOT_TOKEN, CHANNEL_ID
|
|
||||||
|
|
||||||
long_text = f'{self.article.body}\n{self.article.link}'
|
long_text = f'{self.article.body}\n{self.article.link}'
|
||||||
querystring = (('chat_id', CHANNEL_ID), ('text', long_text))
|
channel_id = promoter_secrets['TELEGRAM_CHAT_ID']
|
||||||
|
bot_token = promoter_secrets['TELEGRAM_BOT_TOKEN']
|
||||||
|
querystring = (('chat_id', channel_id), ('text', long_text))
|
||||||
encoded_querystring = urlencode(querystring)
|
encoded_querystring = urlencode(querystring)
|
||||||
|
|
||||||
send_message_url = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?{encoded_querystring}'
|
send_message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?{encoded_querystring}'
|
||||||
|
|
||||||
response = requests.get(send_message_url)
|
response = requests.get(send_message_url)
|
||||||
result = response.json()
|
result = response.json()
|
||||||
@ -37,9 +37,9 @@ class TelegramPromoter(Promoter):
|
|||||||
|
|
||||||
class VkontaktePromoter(Promoter):
|
class VkontaktePromoter(Promoter):
|
||||||
def promote(self):
|
def promote(self):
|
||||||
vk_login = os.getenv('VK_LOGIN')
|
vk_login = promoter_secrets['VK_LOGIN']
|
||||||
vk_password = os.getenv('VK_PASSWORD')
|
vk_password = promoter_secrets['VK_PASSWORD']
|
||||||
vk_owner_id = os.getenv('VK_OWNER_ID')
|
vk_owner_id = promoter_secrets['VK_OWNER_ID']
|
||||||
|
|
||||||
import vk_api
|
import vk_api
|
||||||
session = vk_api.VkApi(login=vk_login,
|
session = vk_api.VkApi(login=vk_login,
|
||||||
@ -60,9 +60,9 @@ class OdnoklassnikiPromoter(Promoter):
|
|||||||
from json import JSONEncoder
|
from json import JSONEncoder
|
||||||
import ok_api
|
import ok_api
|
||||||
|
|
||||||
ok_access_token = os.getenv('OK_ACCESS_TOKEN')
|
ok_access_token = promoter_secrets['OK_ACCESS_TOKEN']
|
||||||
ok_application_key = os.getenv('OK_APPLICATION_KEY')
|
ok_application_key = promoter_secrets['OK_APPLICATION_KEY']
|
||||||
ok_application_secret_key = os.getenv('OK_APPLICATION_SECRET_KEY')
|
ok_application_secret_key = promoter_secrets['OK_APPLICATION_SECRET_KEY']
|
||||||
|
|
||||||
session = ok_api.OkApi(access_token=ok_access_token,
|
session = ok_api.OkApi(access_token=ok_access_token,
|
||||||
application_key=ok_application_key,
|
application_key=ok_application_key,
|
||||||
|
@ -18,11 +18,27 @@ from django.core import signing
|
|||||||
|
|
||||||
from .private.settings import *
|
from .private.settings import *
|
||||||
|
|
||||||
|
|
||||||
|
def decode_env(env_key: str) -> str:
|
||||||
|
signer = signing.Signer(salt=SALT)
|
||||||
|
signed_telegram_chat_id_dict = getenv(env_key)
|
||||||
|
return signer.unsign_object(signed_telegram_chat_id_dict)[env_key]
|
||||||
|
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
env_file = path.join(BASE_DIR, '.env')
|
env_file = path.join(BASE_DIR, '.env')
|
||||||
|
|
||||||
dotenv.read_dotenv(env_file)
|
dotenv.read_dotenv(env_file)
|
||||||
|
|
||||||
|
promoter_env_keys = (
|
||||||
|
'TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID', 'JOOMLA_TOKEN',
|
||||||
|
'VK_LOGIN', 'VK_PASSWORD', 'VK_OWNER_ID', 'OK_ACCESS_TOKEN', 'OK_APPLICATION_KEY',
|
||||||
|
'OK_APPLICATION_SECRET_KEY',
|
||||||
|
)
|
||||||
|
promoter_secrets = {}
|
||||||
|
for promoter_env_key in promoter_env_keys:
|
||||||
|
promoter_secrets[promoter_env_key] = decode_env(promoter_env_key)
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
|
|
||||||
# Quick-start development settings - unsuitable for production
|
# Quick-start development settings - unsuitable for production
|
||||||
|
Loading…
x
Reference in New Issue
Block a user