-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
147 lines (118 loc) · 4.88 KB
/
run.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import asyncio
import logging
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart, Command
from aiogram.types import Message
from config import TOKEN
import requests
bot = Bot(token=TOKEN)
dp = Dispatcher()
@dp.message(CommandStart())
async def cmd_start(message: Message):
await message.answer(
f'Hello, {message.from_user.first_name}! Here is the bot for Taylor Swift fans with lyrics, albums info and quotes')
@dp.message(Command("help"))
async def cmd_help(message: Message):
help_text = (
"Hello! This is the Taylor Swift Fan Bot. Here are the commands you can use:\n\n"
"/start - Welcome message\n"
"/help - Show this help message\n"
"/albums - Get a list of all Taylor Swift albums\n"
"/random_album - Get a random Taylor Swift album with cover image\n"
"/random_song - Get a random Taylor Swift song with lyrics\n"
"/random_quote - Get a random quote from Taylor Swift's songs\n"
"/credits - View the bot credits\n\n"
"You can also search for specific songs by sending a message with the song name."
)
await message.answer(str(help_text))
@dp.message(lambda message: True)
async def handle_message(message: Message):
user_text = message.text.strip()
if user_text.lower().startswith('/albums'):
await get_albums(message)
elif user_text.lower().startswith('/random_quote'):
await get_random_quote(message)
elif user_text.lower().startswith('/random_song'):
await get_random_song(message)
elif user_text.lower().startswith('/random_album'):
await get_random_album(message)
elif user_text.lower().startswith('/credits'):
await get_credits(message)
else:
await search_songs(message, user_text)
async def search_songs(message: Message, query: str):
url = f"https://taylor-swift-api.vercel.app/api/songs/search/?name={query}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data:
results = []
for song in data:
song_info = (f"Name: {song.get('name')}\n"
f"Artist: {song.get('artist')}\n"
f"Duration: {song.get('duration')}\n"
f"Album: {song.get('album')}\n"
f"Lyrics: {song.get('lyrics')}\n")
results.append(song_info)
result = '\n\n'.join(results)
else:
result = "No songs found with that name."
else:
result = "Error fetching data."
await message.answer(result)
@dp.message(Command("albums"))
async def get_albums(message: Message):
response = requests.get("https://taylor-swift-api.vercel.app/api/albums")
if response.status_code == 200:
data = response.json()
titles = [album["title"] for album in data]
result = '\n'.join(titles)
else:
result = "Error fetching data."
await message.answer(result)
@dp.message(Command("random_quote"))
async def get_random_quote(message: Message):
response = requests.get("https://taylor-swift-api.vercel.app/api/quotes")
if response.status_code == 200:
data = response.json()
quote = f"Song: {data.get('song')}\n\n{data.get('quote')}"
else:
quote = "Error fetching data."
await message.answer(quote)
@dp.message(Command("random_song"))
async def get_random_song(message: Message):
response = requests.get("https://taylor-swift-api.vercel.app/api/songs/random")
if response.status_code == 200:
data = response.json()
song = (f"Song: {data.get('name')}\n\n{data.get('lyrics')}\n\n"
f"Duration: {data.get('duration')}\n\nAlbum: {data.get('album')}")
else:
song = "Error fetching data."
await message.answer(song)
@dp.message(Command("credits"))
async def get_credits(message: Message):
link = "https://t.me/south_russian"
await message.answer(link)
@dp.message(Command("random_album"))
async def get_random_album(message: Message):
response = requests.get("https://taylor-swift-api.vercel.app/api/albums/random")
if response.status_code == 200:
data = response.json()
album_info = (f"Title: {data.get('title')}\n"
f"Release Date: {' '.join(map(str, data.get('releaseDate')))}")
album_cover_url = data.get('albumCover')
else:
album_info = "Error fetching data."
album_cover_url = None
if album_cover_url:
await bot.send_photo(chat_id=message.chat.id, photo=album_cover_url, caption=album_info)
else:
await message.answer(album_info)
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Bot stopped.")