telegram_bots/feeds/modules/vk_music.py

49 lines
1.5 KiB
Python
Raw Normal View History

2019-01-26 06:36:56 +00:00
import os
import tempfile
import requests
import sentry_sdk
from django.db import models
from djconfig import config
from telebot import TeleBot
from telebot.types import InputMediaPhoto
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'
def execute(self, bot: TeleBot, chat_id, last_id):
config._reload_maybe()
if last_id is None:
last_id = 0
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):
if audio['id'] > last_id:
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-02-04 09:15:53 +00:00
yield track['id']