-
Notifications
You must be signed in to change notification settings - Fork 22
/
spotify.py
445 lines (402 loc) · 17.5 KB
/
spotify.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import asyncio
import datetime
import textwrap
from math import ceil
from pyrogram import Client, filters
from pyrogram.types import Message, Document
from utils.misc import modules_help, prefix
from utils.db import db
from utils.scripts import import_library
spotipy = import_library("spotipy")
client_id = "e0708753ab60499c89ce263de9b4f57a"
client_secret = "80c927166c664ee98a43a2c0e2981b4a"
scope = (
"user-read-playback-state playlist-read-private playlist-read-collaborative"
" app-remote-control user-modify-playback-state user-library-modify"
" user-library-read"
)
sp_auth = spotipy.oauth2.SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri="https://fuccsoc.com/",
scope=scope,
)
def auth_required(function):
async def wrapped(client: Client, message: Message):
if db.get("custom.spotify", "token") is None:
await message.edit(
f"<b>⚠️Для использования модуля необходима авторизация.\n"
f"ℹ️Выполните <code>{prefix}spauth</code> для авторизации.</b>"
)
else:
return await function(client, message)
return wrapped
async def check_token():
if db.get("custom.spotify", "token") is not None:
if db.get("custom.spotify", "last_token_update") is None:
db.set(
"custom.spotify",
"token",
sp_auth.refresh_access_token(
db.get("custom.spotify", "token")["refresh_token"]
),
)
db.set(
"custom.spotify",
"last_token_update",
datetime.datetime.now().isoformat(),
)
else:
ttc = datetime.datetime.strptime(
db.get("custom.spotify", "last_token_update"), "%Y-%m-%dT%H:%M:%S.%f"
) + datetime.timedelta(minutes=45)
if ttc < datetime.datetime.now():
db.set(
"custom.spotify",
"token",
sp_auth.refresh_access_token(
db.get("custom.spotify", "token")["refresh_token"]
),
)
db.set(
"custom.spotify",
"last_token_update",
datetime.datetime.now().isoformat(),
)
async def check_token_loop():
while True:
await check_token()
await asyncio.sleep(600)
loop = asyncio.get_event_loop()
loop.create_task(check_token_loop())
@Client.on_message(filters.command("spauth", prefix) & filters.me)
async def auth(client: Client, message: Message):
if not db.get("custom.spotify", "token") is None:
await message.edit("⚠️Вы уже авторизованы")
else:
sp_auth.get_authorize_url()
await message.edit(
f'<a href="{sp_auth.get_authorize_url()}">ℹ️Перейдите по этой ссылке</a>,'
" подтвердите доступ, затем скопируйте адрес редиректа и выполните"
f" <code>{prefix}spcodeauth [адрес редедиректа]</code>"
)
@Client.on_message(filters.command("spcodeauth", prefix) & filters.me)
async def codeauth(client: Client, message: Message):
if db.get("custom.spotify", "token") is not None:
await message.edit("⚠️Вы уже авторизованы")
else:
try:
url = message.text.split(" ")[1]
code = sp_auth.parse_auth_response_url(url)
db.set(
"custom.spotify", "token", sp_auth.get_access_token(code, True, False)
)
await message.edit(
"<b>✅Авторизация успешна. Теперь вы можете использовать модуль\n"
f"Список команд: <code>{prefix}help spotify</code></b>"
)
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("spunauth", prefix) & filters.me)
@auth_required
async def unauth(client: Client, message: Message):
db.remove("custom.spotify", "token")
db.remove("custom.spotify", "last_token_update")
await message.edit("<b>✅Данные авторизации удалены успешно.</b>")
@Client.on_message(filters.command("spnow", prefix) & filters.me)
@auth_required
async def now(client: Client, message: Message):
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
current_playback = sp.current_playback()
success = True
from_playlist = False
try:
track = current_playback["item"]["name"]
artists = [
'<a href="'
+ artist["external_urls"]["spotify"]
+ '">'
+ artist["name"]
+ "</a>"
for artist in current_playback["item"]["artists"]
]
artists_names = [
artist["name"] for artist in current_playback["item"]["artists"]
]
track_id = current_playback["item"]["id"]
track_url = current_playback["item"]["external_urls"]["spotify"]
device = (
current_playback["device"]["name"]
+ " "
+ current_playback["device"]["type"].lower()
)
volume = str(current_playback["device"]["volume_percent"]) + "%"
percentage = ceil(
current_playback["progress_ms"]
/ current_playback["item"]["duration_ms"]
* 100
)
bar_filled = ceil(percentage / 10)
bar_empty = 10 - bar_filled
bar = "".join("█" for _ in range(bar_filled))
for _ in range(bar_empty):
bar += "░"
bar += (
f" {str(int((current_playback['progress_ms'] / (1000 * 60)) % 60)).zfill(2)}:{str(int((current_playback['progress_ms'] / 1000) % 60)).zfill(2)} "
f"/ {str(int((current_playback['item']['duration_ms'] / (1000 * 60)) % 60)).zfill(2)}:{str(int((current_playback['item']['duration_ms'] / 1000) % 60)).zfill(2)}"
)
bar += str(" (" + str(percentage) + "%)")
try:
from_playlist = True
playlist_id = current_playback["context"]["uri"].split(":")[-1]
playlist = sp.playlist(playlist_id)
playlist_link = playlist["external_urls"]["spotify"]
playlist_name = playlist["name"]
playlist_owner = (
'<a href = "'
+ playlist["owner"]["external_urls"]["spotify"]
+ '">'
+ playlist["owner"]["display_name"]
+ "</a>"
+ " <code>("
+ playlist["owner"]["id"]
+ ")</code>"
)
except:
from_playlist = False
except Exception as e:
success = False
if from_playlist and success:
res = textwrap.dedent(
f"""
<b>🎶 Сейчас играет: <i>{", ".join(artists)} - <a href='{track_url}'>{track}</a> <a href="https://song.link/s/{track_id}">(другие платформы)</a></i>
📱 Устройство: <code>{device}</code>
🔊 Громкость: {volume}
🎵 Плейлист: <a href="{playlist_link}">{playlist_name}</a> (<code>{playlist_id}</code>)
🫂 Владелец плейлиста: {playlist_owner}
<code>{bar}</code></b>
"""
)
err = False
try:
for r in (
await client.get_inline_bot_results(
"vkm4bot", f"{', '.join(artists_names)} - {track}"
)
)["results"]:
if r["type"] == "audio":
await client.send_cached_media(
message.chat.id,
Document._parse(client, r["document"], "audio")["file_id"],
res,
reply_to_message_id=(
message.reply_to_message.message_id
if message.reply_to_message is not None
else None
),
)
await message.delete()
return
except Exception as e:
err = True
res += (
"\n<b>ℹ️Не удалось найти песню.\nОшибка:</b>"
f" <code>{e.__class__.__name__}</code>"
)
await message.edit(res, disable_web_page_preview=True)
if not err:
res += "\n<b>ℹ️Не удалось найти песню.</b>"
await message.edit(res, disable_web_page_preview=True)
elif success:
res = textwrap.dedent(
f"""
<b>🎶 Сейчас играет: <i>{", ".join(artists)} - <a href='{track_url}'>{track}</a> <a href="https://song.link/s/{track_id}">(другие платформы)</a></i>
📱 Устройство: <code>{device}</code>
🔊 Громкость: {volume}
<code>{bar}</code></b>
"""
)
try:
for r in (
await client.get_inline_bot_results(
"vkm4bot", f"{', '.join(artists_names)} - {track}"
)
)["results"]:
if r["type"] == "audio":
await client.send_cached_media(
message.chat.id,
Document._parse(client, r["document"], "audio")["file_id"],
res,
reply_to_message_id=(
message.reply_to_message.message_id
if message.reply_to_message is not None
else None
),
)
await message.delete()
return
except:
pass
try:
for r in (
await client.get_inline_bot_results(
"spotifysavebot", f"{', '.join(artists_names)} - {track}"
)
)["results"]:
if r["type"] == "audio":
await client.send_cached_media(
message.chat.id,
Document._parse(client, r["document"], "audio")["file_id"],
res,
reply_to_message_id=(
message.reply_to_message.message_id
if message.reply_to_message is not None
else None
),
)
await message.delete()
return
except:
pass
try:
for r in (
await client.get_inline_bot_results(
"lybot", f"{', '.join(artists_names)} - {track}"
)
)["results"]:
if r["type"] == "audio":
await client.send_cached_media(
message.chat.id,
Document._parse(client, r["document"], "audio")["file_id"],
res,
reply_to_message_id=(
message.reply_to_message.message_id
if message.reply_to_message is not None
else None
),
)
await message.delete()
return
except:
pass
res += "\n<b>ℹ️Не удалось найти песню.</b>"
await message.edit(res, disable_web_page_preview=True)
else:
await message.edit(
"<b>⚠️Не удалось получить трек\n"
"Проверьте, что Spotify включен и проигрывает трек</b>"
)
@Client.on_message(filters.command("repeat", prefix) & filters.me)
@auth_required
async def repeat(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.repeat("track")
await message.edit("🔂Поставлено на репит успешно. Счастливого прослушивания!")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("derepeat", prefix) & filters.me)
@auth_required
async def derepeat(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.repeat("context")
await message.edit("🎶Снято с репита успешно.")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("next", prefix) & filters.me)
@auth_required
async def next(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.next_track()
await message.edit("⏭️Трек переключен успешно.")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("pausetr", prefix) & filters.me)
@auth_required
async def pausetr(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.pause_playback()
await message.edit("⏸️Поставлено на паузу успешно.")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("unpausetr", prefix) & filters.me)
@auth_required
async def unpausetr(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.start_playback()
await message.edit("▶️Снято с паузы успешно")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("back", prefix) & filters.me)
@auth_required
async def back(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.previous_track()
await message.edit("◀️Вернул трек назад успешно.")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("restr", prefix) & filters.me)
@auth_required
async def restr(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
sp.seek_track(0)
await message.edit("🔁Трек перезапущен.")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
@Client.on_message(filters.command("liketr", prefix) & filters.me)
@auth_required
async def liketr(client: Client, message: Message):
try:
sp = spotipy.Spotify(auth=db.get("custom.spotify", "token")["access_token"])
cupl = sp.current_playback()
sp.current_user_saved_tracks_add([cupl["item"]["id"]])
await message.edit("💚Лайкнуто!")
except Exception as e:
await message.edit(
"<b>⚠️Произошла какая-то ошибка. Проверьте, что вы все делаете верно.\n"
f"Ошибка:</b> <code>{e.__class__.__name__}</code>"
)
modules_help["spotify"] = {
"spauth": "First auth step",
"spcodeauth": "Second auth step",
"spunauth": "Remove auth data",
"spnow": "Display now playing track",
"repeat": "Set track on-repeat",
"derepeat": "Set track out from repeat",
"next": "Turn on next track",
"back": "Turn on previous track",
"restr": "Restart currently playing track from start",
"liketr": "Like current playing track",
"pausetr": "Pause current playing track",
"unpausetr": "Play currently paused track",
}