diff --git a/.gitignore b/.gitignore index d6f97d1..310e716 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ db.sqlite3 .env __pycache__/ -identifier.sqlite \ No newline at end of file +identifier.sqlite +vk_config.v2.json \ No newline at end of file diff --git a/cms/migrations/0002_remove_article_title_article_link.py b/cms/migrations/0002_remove_article_title_article_link.py new file mode 100644 index 0000000..0c1bf60 --- /dev/null +++ b/cms/migrations/0002_remove_article_title_article_link.py @@ -0,0 +1,22 @@ +# Generated by Django 4.1.4 on 2022-12-16 07:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cms', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='article', + name='title', + ), + migrations.AddField( + model_name='article', + name='link', + field=models.CharField(default='https://zakonvremeni.ru/news/', max_length=100), + ), + ] diff --git a/cms/models.py b/cms/models.py index 00b9e62..a7dd029 100644 --- a/cms/models.py +++ b/cms/models.py @@ -2,5 +2,5 @@ from django.db import models class Article(models.Model): - title = models.CharField(max_length=100) - body = models.TextField() \ No newline at end of file + body = models.TextField() + link = models.CharField(max_length=100, default='https://zakonvremeni.ru/news/') diff --git a/cms/urls.py b/cms/urls.py index 290752a..b1c5b39 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -1,6 +1,7 @@ -from .views import ArticleView from django.urls import path +from .views import ArticleView + urlpatterns = [ path('articles/', ArticleView.as_view()) ] diff --git a/cms/views.py b/cms/views.py index fcadb18..7148f56 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,9 +1,10 @@ -from json import JSONDecoder, JSONEncoder -import requests import os +from json import JSONDecoder + +import requests from django.http import JsonResponse -from django.views import View from django.utils.decorators import method_decorator +from django.views import View from django.views.decorators.csrf import csrf_exempt from cms.models import Article @@ -11,11 +12,11 @@ from cms.models import Article @method_decorator(csrf_exempt, name='dispatch') class ArticleView(View): - def _promote_to_channel(self, article: Article): + 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.title}\n{article.body}' + 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}' @@ -26,32 +27,25 @@ class ArticleView(View): else: print('Похоже, нас послали доделывать приложение :-(') - def _promote_to_joomla(self, article: Article): - joomla_token = os.getenv('JOOMLA_TOKEN') - headers = { - 'X-Joomla-Token': joomla_token, - 'Content-Type': 'application/json' - } - article_json = { - "alias": article.title, - "articletext": article.body, - "catid": "8", - "language": "*", - "metadesc": "", - "metakey": "", - "title": article.title, - "state": 1 - } - response = requests.post('http://zv.mirokod.ru/api/index.php/v1/content/articles', - headers=headers, - data=JSONEncoder().encode(article_json)) - result = response.json() - print(result) + 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 post(self, request): article_data = JSONDecoder().decode(request.body.decode()) article = Article.objects.create(**article_data) - self._promote_to_channel(article) - self._promote_to_joomla(article) + self._promote_to_telegram(article) + self._promote_to_vk(article) response = {'ok': True} - return JsonResponse(response) \ No newline at end of file + return JsonResponse(response) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..672b5ac --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +asgiref==3.5.2 +certifi==2022.12.7 +charset-normalizer==2.1.1 +Django==4.1.4 +django-dotenv==1.4.2 +idna==3.4 +requests==2.28.1 +sqlparse==0.4.3 +urllib3==1.26.13 +vk-api==11.9.9