telegram_bots/feeds/modules/vk_music.py
2019-10-27 02:03:51 +03:00

49 lines
1.5 KiB
Python

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
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: 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,
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):
if 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 track['id']