-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
197 lines (177 loc) · 6.35 KB
/
main.py
File metadata and controls
197 lines (177 loc) · 6.35 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import telebot
from telebot import types
from config import *
from musixmatch import *
from iso3166 import countries
from lyrics import *
bot = telebot.TeleBot(BOT_TOKEN)
@bot.message_handler(commands=["tracks"]) # "/tracks queens"
def tracks_of_author(message):
author = " ".join(
[t.capitalize() for t in message.json["text"].split(" ")[1:]]
).strip()
response, status = get_tracks_of_author(author)
if status == 200:
ans = "Here's list of popular tracks of *" + author + "*:\n"
for track in response["message"]["body"]["track_list"]:
tr = track["track"]["track_name"]
album_name = track["track"]["album_name"]
url = track["track"]["track_share_url"]
ans += f"Track: [{tr}]({url})\n"
if album_name:
ans += "Album: *" + album_name + "*"
ans += "\n\n"
bot.send_message(message.chat.id, text=ans, parse_mode="Markdown")
return
bot.send_message(message.chat.id, text=MUSIXMATCH_ERROR)
@bot.message_handler(commands=["charts"])
def get_charts_of_country(message):
country_code = " ".join(message.json["text"].split(" ")[1:]).strip()
if not country_code or len(country_code) == 0:
bot.send_message(
message.chat.id,
text=FORMAT_ISO3166_ERROR,
)
return
else:
response, status = get_country_charts(country_code)
if status == 200:
try:
cc = countries.get(country_code.lower()).name
except:
bot.send_message(
message.chat.id,
text=FORMAT_ISO3166_ERROR,
)
return
answer = "Here's top chart of *" + cc + "*:\n"
for track in response["message"]["body"]["track_list"]:
tr = track["track"]["track_name"]
artist = track["track"]["artist_name"]
url = track["track"]["track_share_url"]
answer += f"Track: [{tr}]({url}) (lyrics)\nArtist: *" + artist \
+ "*\n\n"
bot.send_message(message.chat.id, text=answer, parse_mode="Markdown")
return
bot.send_message(message.chat.id, text=MUSIXMATCH_ERROR)
@bot.message_handler(commands=["chart_artists"])
def get_artists_of_country_chart(message):
country_code = " ".join(message.json["text"].split(" ")[1:]).strip()
if not country_code or len(country_code) == 0:
bot.send_message(
message.chat.id,
text=FORMAT_ISO3166_ERROR,
)
return
response, status = get_chart_artists(country_code)
if status == 200:
if (not countries.__contains__(country_code.lower())):
bot.send_message(message.chat.id, text="Didn't find country. " + FORMAT_ISO3166_ERROR, parse_mode="Markdown")
return
country = countries.get(country_code.lower()).name
answer = (
"Here's artists' top chart of *"
+ country
+ "*:\n"
)
for artist in response["message"]["body"]["artist_list"]:
name = artist["artist"]["artist_name"]
if len(artist["artist"]["artist_name_translation_list"]) > 0:
for transl in artist["artist"]["artist_name_translation_list"]:
if transl["artist_name_translation"]["language"] == "EN":
name = transl["artist_name_translation"]["translation"]
break
answer += f"Artist: *{name}*\n\n"
bot.send_message(message.chat.id, text=answer, parse_mode="Markdown")
return
bot.send_message(
message.chat.id,
text=MUSIXMATCH_ERROR,
)
def shorten(name):
if "-" in name:
l = name.split("-")
artist, track = l[0], " ".join(l[1:])
elif " " in name:
l = name.split(" ")
artist, track = l[:-1], l[-1:]
if len(artist) > 15:
artist = artist[:15]
if len(track) > 15:
track = track[:15]
return [artist, track]
@bot.message_handler(commands=["lyrics"])
def get_lyrics(message):
res = [
tmp.strip() for tmp in " ".join(
message.json["text"].split(" ")[1:]
).split("-")
]
if len(res) == 1 and not len(res[0]):
bot.send_message(
message.chat.id,
text="Please add searching parameters: artist name "
"or/and track name.",
)
return
elif len(res) == 1:
track = res[0]
artist = ""
else:
track, artist = res
songs, status = search(track + " " + artist)
if not status and "Error" in songs:
bot.reply_to(message, text=songs["Error"])
return
elif not status:
bot.reply_to(
message,
text=MUSIXMATCH_ERROR,
)
return
keyboard = types.InlineKeyboardMarkup(row_width=2)
for k in songs:
shortened = shorten(k)
button = types.InlineKeyboardButton(k,
callback_data=" ".join(shortened)
)
keyboard.add(button)
text = "Which song are you searching for?\n" \
"Please pass track details more correctly " \
"(maybe you missed any apostrophe)"
bot.send_message(message.chat.id, text, reply_markup=keyboard)
@bot.callback_query_handler(func=lambda callback: True)
def callbacks(callback):
mkup = types.InlineKeyboardMarkup(row_width=1)
button = types.InlineKeyboardButton("Back", callback_data="B")
mkup.add(button)
k = callback.data
songs, status = search(k)
for i in songs:
if k in " ".join(shorten(i)):
k = i
break
if status:
text = parse_lyrics(songs[k])
else:
text = MUSIXMATCH_ERROR
bot.edit_message_text(
text[:4096], callback.message.chat.id, callback.message.message_id
)
@bot.message_handler()
def greet(message):
bot.send_message(message.chat.id, text=message.json["text"])
# def polling():
# try:
# bot.polling()
# except:
# polling()
#
#
# polling()
if __name__ == "__main__":
FORMAT_ISO3166_ERROR = "Please pass country code in the format of ISO3166. For example: /charts ru"
MUSIXMATCH_ERROR = """Couldn't connect to MusixMatch. Please try again.
Anyway you can report it to me @starboy369.
Thanks! """
bot.polling(none_stop=True, timeout=5)