2022-04-09 21:56:30 +00:00
|
|
|
import traceback
|
|
|
|
|
2019-01-25 16:33:22 +00:00
|
|
|
import sentry_sdk
|
|
|
|
from django.db import models
|
|
|
|
from djconfig import config
|
2021-03-20 13:21:13 +00:00
|
|
|
from telegram import Bot, InputMediaPhoto
|
2019-01-25 16:33:22 +00:00
|
|
|
from vk_api import VkApi
|
|
|
|
from vk_api.audio import VkAudio
|
2022-04-09 21:28:45 +00:00
|
|
|
import vkwave
|
2019-01-25 16:33:22 +00:00
|
|
|
|
|
|
|
from feeds.models import FeedModuleConfig
|
2019-10-26 23:03:51 +00:00
|
|
|
from feeds.modules.utils import get_vk_photo, get_vk_audio
|
2019-02-08 14:04:37 +00:00
|
|
|
from feeds.utils import DatabaseConfig, captcha_handler
|
2019-01-25 16:33:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VKFeedModuleConfig(FeedModuleConfig):
|
|
|
|
owner_id = models.IntegerField()
|
|
|
|
send_text = models.BooleanField(default=True)
|
|
|
|
send_links = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
MODULE_NAME = 'VK feed'
|
|
|
|
|
2021-03-20 13:21:13 +00:00
|
|
|
def execute(self, bot: Bot, chat_id, last_id):
|
2019-01-25 16:33:22 +00:00
|
|
|
config._reload_maybe()
|
2019-01-25 16:40:47 +00:00
|
|
|
|
|
|
|
if last_id is None:
|
|
|
|
last_id = 0
|
|
|
|
|
2019-01-25 16:33:22 +00:00
|
|
|
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig,
|
2022-04-09 21:40:08 +00:00
|
|
|
api_version='5.131', captcha_handler=captcha_handler)
|
2019-01-25 16:33:22 +00:00
|
|
|
vk_session.auth()
|
2022-04-09 21:28:45 +00:00
|
|
|
# vk_audio = VkAudio(vk_session)
|
2019-01-25 16:33:22 +00:00
|
|
|
vk = vk_session.get_api()
|
|
|
|
wall_data = vk.wall.get(owner_id=self.owner_id)
|
|
|
|
for post in reversed(wall_data['items']):
|
|
|
|
if post['id'] > last_id:
|
|
|
|
try:
|
|
|
|
if self.send_text and post['text']:
|
|
|
|
bot.send_message(chat_id, post['text'])
|
|
|
|
if 'attachments' in post:
|
|
|
|
photos = []
|
|
|
|
for image in filter(lambda a: 'photo' in a, post['attachments']):
|
2022-04-09 21:54:23 +00:00
|
|
|
url = image['photo']['sizes'][-1]['url']
|
2019-01-25 16:33:22 +00:00
|
|
|
if url:
|
|
|
|
photos.append(url.replace('\\', ''))
|
|
|
|
if len(photos) == 1:
|
|
|
|
bot.send_photo(chat_id, photos[0])
|
|
|
|
elif len(photos) > 1:
|
|
|
|
bot.send_media_group(chat_id, [InputMediaPhoto(photo) for photo in photos])
|
2020-05-06 15:54:19 +00:00
|
|
|
# for a in post['attachments']:
|
|
|
|
# if 'audio' in a:
|
|
|
|
# f = get_vk_audio(vk_audio.get_audio_by_id(a['audio']['owner_id'], a['audio']['id']))
|
|
|
|
# bot.send_audio(chat_id, f, None, a['audio'].get('artist'), a['audio'].get('title'))
|
2019-01-25 16:33:22 +00:00
|
|
|
if self.send_links:
|
|
|
|
bot.send_message(chat_id, f"https://vk.com/wall{post['owner_id']}_{post['id']}",
|
|
|
|
disable_web_page_preview=True)
|
|
|
|
except Exception as e:
|
|
|
|
sentry_sdk.capture_exception(e)
|
2022-04-09 21:56:30 +00:00
|
|
|
traceback.print_exc()
|
2019-02-04 09:15:53 +00:00
|
|
|
yield post['id']
|