From f4deaee1b1a7ea429b1e04f7cabe0f82c66af7d7 Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Tue, 20 Dec 2022 11:52:33 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=D0=B8=D0=BC=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20=D1=83=D1=81=D0=BF?= =?UTF-8?q?=D0=B5=D1=88=D0=BD=D0=BE=D0=B3=D0=BE/=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D0=B2=D0=B8=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20#17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cms/promoters.py | 99 +++++++++++++++++++++++++++++++++++++ cms/templates/articles/created.html | 11 ----- cms/views.py | 78 +++++------------------------ 3 files changed, 112 insertions(+), 76 deletions(-) create mode 100644 cms/promoters.py delete mode 100644 cms/templates/articles/created.html diff --git a/cms/promoters.py b/cms/promoters.py new file mode 100644 index 0000000..c7d5755 --- /dev/null +++ b/cms/promoters.py @@ -0,0 +1,99 @@ +import abc +import os + +import requests + +from cms.models import Article + + +class PromoteError(Exception): + pass + + +class Promoter(abc.ABC): + def __init__(self, article: Article): + self.article = article + + def promote(self): + raise NotImplementedError + + +class TelegramPromoter(Promoter): + def promote(self): + bot_token = os.getenv('TELEGRAM_BOT_TOKEN') + channel_id = os.getenv('TELEGRAM_CHAT_ID') + + long_text = f'{self.article.body}\n{self.article.link}' + + send_message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={channel_id}&text={long_text}' + + response = requests.get(send_message_url) + result = response.json() + if not result['ok']: + raise PromoteError('Похоже, нас послали доделывать приложение :-(') + + +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') + + import vk_api + session = vk_api.VkApi(login=vk_login, + password=vk_password) + session.auth() + api = session.get_api() + + try: + api.wall.post(owner_id=vk_owner_id, + message=self.article.body, + attachments=self.article.link) + except vk_api.VkApiError as exc: + raise PromoteError(exc) + + +class OdnoklassnikiPromoter(Promoter): + def promote(self): + 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') + + session = ok_api.OkApi(access_token=ok_access_token, + application_key=ok_application_key, + application_secret_key=ok_application_secret_key) + attachments = { + 'media': [ + { + 'type': 'text', + 'text': self.article.body, + }, + { + 'type': 'link', + 'url': self.article.link, + }, + ] + } + encoded_attachments = JSONEncoder().encode(attachments) + try: + session.mediatopic.post(type='GROUP_THEME', + gid='70000001426867', + attachment=encoded_attachments) + except ok_api.OkApiException as exc: + raise PromoteError(exc) + + +class Marketer: + def __init__(self, article: Article): + self.promoters = [ + TelegramPromoter(article), + VkontaktePromoter(article), + OdnoklassnikiPromoter(article), + ] + + def promote(self): + for promoter in self.promoters: + promoter.promote() diff --git a/cms/templates/articles/created.html b/cms/templates/articles/created.html deleted file mode 100644 index 8d53679..0000000 --- a/cms/templates/articles/created.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends 'base.html' %} -{% block content %} - - - Продвиньте новую статью - -{% endblock content %} \ No newline at end of file diff --git a/cms/views.py b/cms/views.py index d0a0837..a213f78 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,7 +1,3 @@ -import os -from json import JSONEncoder - -import requests from django.contrib import messages from django.contrib.auth import authenticate, login from django.contrib.auth.decorators import login_required @@ -11,76 +7,28 @@ from django.shortcuts import render from django.urls import reverse from django.views import View +from cms import promoters from cms.forms import ArticleForm, UserForm from cms.models import Article class ArticleView(LoginRequiredMixin, View): - def _promote_to_telegram(self, article: Article): - bot_token = os.getenv('TELEGRAM_BOT_TOKEN') - channel_id = os.getenv('TELEGRAM_CHAT_ID') - - long_text = f'{article.body}\n{article.link}' - - send_message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={channel_id}&text={long_text}' - - response = requests.get(send_message_url) - result = response.json() - if result['ok']: - print('Мы послали сообщение, ура!') - else: - print('Похоже, нас послали доделывать приложение :-(') - - def _promote_to_vk(self, article: Article): - import vk_api - vk_login = os.getenv('VK_LOGIN') - vk_password = os.getenv('VK_PASSWORD') - vk_owner_id = os.getenv('VK_OWNER_ID') - - session = vk_api.VkApi(login=vk_login, - password=vk_password) - session.auth() - api = session.get_api() - - api.wall.post(owner_id=vk_owner_id, - message=article.body, - attachments=article.link) - - def _promote_to_ok(self, article: Article): - 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') - - session = ok_api.OkApi(access_token=ok_access_token, - application_key=ok_application_key, - application_secret_key=ok_application_secret_key) - attachments = { - 'media': [ - { - 'type': 'text', - 'text': article.body, - }, - { - 'type': 'link', - 'url': article.link, - }, - ] - } - encoded_attachments = JSONEncoder().encode(attachments) - session.mediatopic.post(type='GROUP_THEME', - gid='70000001426867', - attachment=encoded_attachments) - def post(self, request: HttpRequest): post_data = request.POST article = Article.objects.create(body=post_data['body'], link=post_data['link']) - self._promote_to_telegram(article) - self._promote_to_ok(article) - self._promote_to_vk(article) - return render(request, template_name='articles/created.html') + marketer = promoters.Marketer(article) + try: + marketer.promote() + message_type = messages.SUCCESS + message_text = 'Продвижение статьи прошло успешно' + except promoters.PromoteError as exc: + message_type = messages.ERROR + message_text = 'Произошла ошибка: %s' % str(exc) + messages.add_message(request=request, + level=message_type, + message=message_text) + return HttpResponseRedirect(reverse('new-article')) @login_required