Реализовано продвижение новости в ВК #9
This commit is contained in:
parent
fc7173b9f4
commit
a192d7e2d0
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@
|
|||||||
db.sqlite3
|
db.sqlite3
|
||||||
.env
|
.env
|
||||||
__pycache__/
|
__pycache__/
|
||||||
identifier.sqlite
|
identifier.sqlite
|
||||||
|
vk_config.v2.json
|
22
cms/migrations/0002_remove_article_title_article_link.py
Normal file
22
cms/migrations/0002_remove_article_title_article_link.py
Normal file
@ -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),
|
||||||
|
),
|
||||||
|
]
|
@ -2,5 +2,5 @@ from django.db import models
|
|||||||
|
|
||||||
|
|
||||||
class Article(models.Model):
|
class Article(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
body = models.TextField()
|
||||||
body = models.TextField()
|
link = models.CharField(max_length=100, default='https://zakonvremeni.ru/news/')
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from .views import ArticleView
|
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
|
from .views import ArticleView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('articles/', ArticleView.as_view())
|
path('articles/', ArticleView.as_view())
|
||||||
]
|
]
|
||||||
|
52
cms/views.py
52
cms/views.py
@ -1,9 +1,10 @@
|
|||||||
from json import JSONDecoder, JSONEncoder
|
|
||||||
import requests
|
|
||||||
import os
|
import os
|
||||||
|
from json import JSONDecoder
|
||||||
|
|
||||||
|
import requests
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.views import View
|
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
from django.views import View
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
from cms.models import Article
|
from cms.models import Article
|
||||||
@ -11,11 +12,11 @@ from cms.models import Article
|
|||||||
|
|
||||||
@method_decorator(csrf_exempt, name='dispatch')
|
@method_decorator(csrf_exempt, name='dispatch')
|
||||||
class ArticleView(View):
|
class ArticleView(View):
|
||||||
def _promote_to_channel(self, article: Article):
|
def _promote_to_telegram(self, article: Article):
|
||||||
bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
|
bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
|
||||||
channel_id = os.getenv('TELEGRAM_CHAT_ID')
|
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}'
|
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:
|
else:
|
||||||
print('Похоже, нас послали доделывать приложение :-(')
|
print('Похоже, нас послали доделывать приложение :-(')
|
||||||
|
|
||||||
def _promote_to_joomla(self, article: Article):
|
def _promote_to_vk(self, article: Article):
|
||||||
joomla_token = os.getenv('JOOMLA_TOKEN')
|
import vk_api
|
||||||
headers = {
|
vk_login = os.getenv('VK_LOGIN')
|
||||||
'X-Joomla-Token': joomla_token,
|
vk_password = os.getenv('VK_PASSWORD')
|
||||||
'Content-Type': 'application/json'
|
vk_owner_id = os.getenv('VK_OWNER_ID')
|
||||||
}
|
|
||||||
article_json = {
|
session = vk_api.VkApi(login=vk_login,
|
||||||
"alias": article.title,
|
password=vk_password)
|
||||||
"articletext": article.body,
|
session.auth()
|
||||||
"catid": "8",
|
api = session.get_api()
|
||||||
"language": "*",
|
|
||||||
"metadesc": "",
|
api.wall.post(owner_id=vk_owner_id,
|
||||||
"metakey": "",
|
message=article.body,
|
||||||
"title": article.title,
|
attachments=article.link)
|
||||||
"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 post(self, request):
|
def post(self, request):
|
||||||
article_data = JSONDecoder().decode(request.body.decode())
|
article_data = JSONDecoder().decode(request.body.decode())
|
||||||
article = Article.objects.create(**article_data)
|
article = Article.objects.create(**article_data)
|
||||||
self._promote_to_channel(article)
|
self._promote_to_telegram(article)
|
||||||
self._promote_to_joomla(article)
|
self._promote_to_vk(article)
|
||||||
response = {'ok': True}
|
response = {'ok': True}
|
||||||
return JsonResponse(response)
|
return JsonResponse(response)
|
||||||
|
10
requirements.txt
Normal file
10
requirements.txt
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user