-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
88 lines (75 loc) · 3.4 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pylast
from pyyoutube import Api
from aiogram import F, Router, types
from aiogram.filters import Command
from aiogram.types import Message
from aiogram.types.callback_query import CallbackQuery
from aiogram.fsm.context import FSMContext
import random
import kb
import text
from states import BotStates
from config import API_KEY, API_SECRET, USERNAME, PASSWORD, API_YOUTUBE
router = Router()
network = pylast.LastFMNetwork(
api_key=API_KEY,
api_secret=API_SECRET,
username=USERNAME,
password_hash=pylast.md5(PASSWORD),
)
youtube_api = Api(api_key=API_YOUTUBE)
@router.message(Command("start"))
async def start_handler(msg: Message):
await msg.answer(text.greet.format(name=msg.from_user.full_name), reply_markup=kb.get_tag)
@router.callback_query(F.data == "get_tag")
async def get_album_tag(clbck: CallbackQuery, state: FSMContext):
await state.clear()
tags = network.get_top_tags(50)
tags = [tag.item.name for tag in tags]
buttons = kb.get_tags_menu(tags)
await clbck.bot.send_message(clbck.message.chat.id, text.get_tag, reply_markup=buttons)
await state.set_state(BotStates.getting_tag)
@router.message(BotStates.getting_tag)
async def get_album_tag(msg: Message, state: FSMContext):
await msg.answer('Do you need album or track?', reply_markup=kb.album_or_track)
await state.update_data(tag_text=msg.text)
@router.callback_query(F.data == "get_album")
@router.callback_query(F.data == "get_track")
async def get_random_album(clbck: CallbackQuery, state: FSMContext):
data = await state.get_data()
tag_text = data['tag_text']
tag = network.get_tag(tag_text.lower())
await state.set_state()
if clbck.data == 'get_album':
entities = tag.get_top_albums(250)
else:
entities = tag.get_top_tracks(250)
try:
entity_num = random.randint(0, len(entities) - 1)
entity = entities[entity_num]
try:
entity_cover = entity.item.get_cover_image()
except IndexError:
entity_cover = None
entity_artist_title = entity.item.artist.name + ' - ' + entity.item.title
entity_url = entity.item.get_url()
entity_playcount = str(entity.item.get_playcount())
await clbck.bot.send_message(clbck.message.chat.id, "Here's your result:",
reply_markup=types.ReplyKeyboardRemove())
try:
r = youtube_api.search_by_keywords(q=entity_artist_title, search_type=["video"], count=1, limit=1)
entity_youtube = 'https://www.youtube.com/watch?v=' + r.items[0].id.videoId
keyboard = kb.ger_url_menu(entity_url, entity_youtube)
except Exception as e:
print(e)
await clbck.bot.send_message(clbck.message.chat.id, "Can't get a youtube video.")
keyboard = kb.ger_url_menu(entity_url)
answer = entity_artist_title + '\n\n' + 'Playcount: ' + entity_playcount
if entity_cover:
await clbck.bot.send_photo(clbck.message.chat.id, entity_cover, caption=answer, reply_markup=keyboard)
else:
await clbck.bot.send_message(clbck.message.chat.id, answer, reply_markup=keyboard)
restart_menu = kb.get_restart_menu(clbck.data)
await clbck.bot.send_message(clbck.message.chat.id, "What's next?", reply_markup=restart_menu)
except ValueError:
await clbck.bot.send_message(clbck.message.chat.id, text.error_tag, reply_markup=kb.get_tag)