telegram_bots/feeds/modules/vk_music.py
2021-03-20 16:21:13 +03:00

48 lines
1.5 KiB
Python

import logging
from django.db import models
from djconfig import config
from telegram import Bot
from vk_api import VkApi
from vk_api.audio import VkAudio
from feeds.models import FeedModuleConfig
from feeds.modules.utils import get_vk_audio
from feeds.utils import DatabaseConfig, captcha_handler
class VKMusicFeedModuleConfig(FeedModuleConfig):
owner_id = models.IntegerField()
MODULE_NAME = 'VK music'
def execute(self, bot: Bot, chat_id, last_id):
config._reload_maybe()
if last_id is None:
last_id = 0
return []
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig,
api_version='5.60', captcha_handler=captcha_handler)
if config.russian_proxy_string:
vk_session.http.proxies = {
'http': config.russian_proxy_string,
'https': config.russian_proxy_string,
}
vk_session.auth()
vk_audio = VkAudio(vk_session)
lst = []
for audio in vk_audio.get_iter(self.owner_id):
logging.warning(audio)
if int(audio['id']) > last_id:
lst = [audio] + lst
else:
break
for track in lst:
f = get_vk_audio(track['url'])
bot.send_audio(chat_id, f, duration=track['duration'], performer=track['artist'],
title=track['title'], timeout=40)
yield int(track['id'])