Browse Source

В продвигателях используются дешифрованные значения из .env-файла #19

pull/32/head
Artur Galyamov 2 years ago
parent
commit
d0ac64d244
  1. 2
      cms/management/commands/encode_file.py
  2. 22
      cms/promoters.py
  3. 16
      crossposting_backend/settings.py

2
cms/management/commands/encode_file.py

@ -9,7 +9,7 @@ class Command(BaseCommand):
environments = {}
with open(BASE_DIR / '.env') as 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
signer = signing.Signer(salt=SALT)

22
cms/promoters.py

@ -1,10 +1,10 @@
import abc
import os
from urllib.parse import urlencode
import requests
from cms.models import Article
from crossposting_backend.settings import promoter_secrets
class PromoteError(Exception):
@ -21,13 +21,13 @@ class Promoter(abc.ABC):
class TelegramPromoter(Promoter):
def promote(self):
from crossposting_backend.settings import BOT_TOKEN, CHANNEL_ID
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)
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)
result = response.json()
@ -37,9 +37,9 @@ class TelegramPromoter(Promoter):
class VkontaktePromoter(Promoter):
def promote(self):
vk_login = os.getenv('VK_LOGIN')
vk_password = os.getenv('VK_PASSWORD')
vk_owner_id = os.getenv('VK_OWNER_ID')
vk_login = promoter_secrets['VK_LOGIN']
vk_password = promoter_secrets['VK_PASSWORD']
vk_owner_id = promoter_secrets['VK_OWNER_ID']
import vk_api
session = vk_api.VkApi(login=vk_login,
@ -60,9 +60,9 @@ class OdnoklassnikiPromoter(Promoter):
from json import JSONEncoder
import ok_api
ok_access_token = os.getenv('OK_ACCESS_TOKEN')
ok_application_key = os.getenv('OK_APPLICATION_KEY')
ok_application_secret_key = os.getenv('OK_APPLICATION_SECRET_KEY')
ok_access_token = promoter_secrets['OK_ACCESS_TOKEN']
ok_application_key = promoter_secrets['OK_APPLICATION_KEY']
ok_application_secret_key = promoter_secrets['OK_APPLICATION_SECRET_KEY']
session = ok_api.OkApi(access_token=ok_access_token,
application_key=ok_application_key,

16
crossposting_backend/settings.py

@ -18,11 +18,27 @@ from django.core import signing
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
env_file = path.join(BASE_DIR, '.env')
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'.
# Quick-start development settings - unsuitable for production

Loading…
Cancel
Save