From 2c1cdec1c2c9faf0444e6d51c3b9ebd80b1f7bb1 Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Sun, 18 Dec 2022 21:21:39 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B2=D0=B8?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=BE=D0=B4=D0=BD?= =?UTF-8?q?=D0=BE=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BD=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=85=20#10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cms/views.py | 30 +++++++++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cms/views.py b/cms/views.py index 7148f56..b71ab91 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,5 +1,5 @@ import os -from json import JSONDecoder +from json import JSONDecoder, JSONEncoder import requests from django.http import JsonResponse @@ -42,10 +42,38 @@ class ArticleView(View): 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): article_data = JSONDecoder().decode(request.body.decode()) article = Article.objects.create(**article_data) self._promote_to_telegram(article) + self._promote_to_ok(article) self._promote_to_vk(article) response = {'ok': True} return JsonResponse(response) diff --git a/requirements.txt b/requirements.txt index 672b5ac..e78a5c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ charset-normalizer==2.1.1 Django==4.1.4 django-dotenv==1.4.2 idna==3.4 +ok-api==1.0.1 requests==2.28.1 sqlparse==0.4.3 urllib3==1.26.13