telegram_bots/feeds/modules/vk_music.py

48 lines
1.5 KiB
Python
Raw Permalink Normal View History

2020-02-19 18:40:30 +00:00
import logging
2019-01-26 06:36:56 +00:00
from django.db import models
from djconfig import config
2021-03-20 13:21:13 +00:00
from telegram import Bot
2019-01-26 06:36:56 +00:00
from vk_api import VkApi
from vk_api.audio import VkAudio
from feeds.models import FeedModuleConfig
2019-10-26 23:03:51 +00:00
from feeds.modules.utils import get_vk_audio
2019-02-08 14:04:37 +00:00
from feeds.utils import DatabaseConfig, captcha_handler
2019-01-26 06:36:56 +00:00
class VKMusicFeedModuleConfig(FeedModuleConfig):
owner_id = models.IntegerField()
MODULE_NAME = 'VK music'
2021-03-20 13:21:13 +00:00
def execute(self, bot: Bot, chat_id, last_id):
2019-01-26 06:36:56 +00:00
config._reload_maybe()
if last_id is None:
last_id = 0
2020-05-06 16:05:14 +00:00
return []
2020-05-06 15:54:19 +00:00
2019-01-26 06:36:56 +00:00
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig,
2019-02-08 14:04:37 +00:00
api_version='5.60', captcha_handler=captcha_handler)
2019-01-26 06:36:56 +00:00
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):
2020-02-19 18:43:26 +00:00
logging.warning(audio)
2019-11-24 14:53:03 +00:00
if int(audio['id']) > last_id:
2019-01-26 06:36:56 +00:00
lst = [audio] + lst
else:
break
2019-01-26 07:35:46 +00:00
for track in lst:
2019-10-26 23:03:51 +00:00
f = get_vk_audio(track['url'])
2019-01-26 06:36:56 +00:00
bot.send_audio(chat_id, f, duration=track['duration'], performer=track['artist'],
2019-06-19 16:47:41 +00:00
title=track['title'], timeout=40)
2019-11-24 14:53:03 +00:00
yield int(track['id'])