forked from kernoeb/Telegramusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
540 lines (475 loc) · 22.3 KB
/
main.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import asyncio
import io
import json
import locale
import logging
import os
import re
import shutil
import traceback
from urllib.parse import quote
import deezloader.deezloader
import requests
from PIL import Image
from aiogram import Bot, Dispatcher, executor, types, exceptions
from aiogram.types import InlineQuery, \
InputTextMessageContent, InlineQueryResultArticle, InputMediaAudio
from aioify import aioify
from mutagen.id3 import ID3, APIC, error
from mutagen.mp3 import MP3
from yt_dlp import YoutubeDL
locale.setlocale(locale.LC_TIME, '')
DEEZER_URL = "https://deezer.com"
API_URL = "https://api.deezer.com"
API_TRACK = API_URL + "/track/%s"
API_ALBUM = API_URL + "/album/%s"
API_SEARCH_TRK = API_URL + "/search/track/?q=%s"
API_PLAYLIST = API_URL + "/playlist/%s"
DEFAULT_QUALITY = "MP3_320"
try:
os.mkdir("tmp")
except FileExistsError:
pass
try:
os.mkdir("tmp/yt/")
except FileExistsError:
pass
logging.basicConfig(level=logging.INFO)
deezloader_async = aioify(obj=deezloader.deezloader, name='deezloader_async')
download = deezloader_async.DeeLogin(os.environ.get('DEEZER_TOKEN'))
downloading_users = []
bot = Bot(token=os.environ.get('TELEGRAM_TOKEN'))
dp = Dispatcher(bot)
LANGS_FILE = json.load(open('langs.json'))
LANG = os.environ.get('BOT_LANG')
if LANG is not None:
print("Lang : " + LANG)
else:
print("Lang : en")
LANG = 'en'
def __(s):
return LANGS_FILE[s][LANG]
def crop_center(pil_img, crop_width, crop_height):
img_width, img_height = pil_img.size
return pil_img.crop(((img_width - crop_width) // 2,
(img_height - crop_height) // 2,
(img_width + crop_width) // 2,
(img_height + crop_height) // 2))
#@dp.message_handler(regexp=r"(?:http?s?:\/\/)?(?:www.)?(?:m.)?(?:music.)?youtu(?:\.?be)(?:\.com)?(?:("
# r"?:\w*.?:\/\/)?\w*.?\w*-?.?\w*\/(?:embed|e|v|watch|.*\/)?\??(?:feature=\w*\.?\w*)?&?("
# r"?:v=)?\/?)([\w\d_-]{11})(?:\S+)?")
async def get_youtube_audio(event: types.Message):
print(event.from_user)
if event.from_user.id not in downloading_users:
tmp_msg = await event.answer(__('downloading'))
downloading_users.append(event.from_user.id)
try:
ydl_opts = {
'outtmpl': 'tmp/yt/%(id)s.%(ext)s',
'format': 'bestaudio/best',
'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '320'}],
}
# Download file
ydl = YoutubeDL(ydl_opts)
dict_info = ydl.extract_info(event.text, download=True)
thumb = dict_info["thumbnail"]
# Get thumb
content = requests.get(thumb).content
image_bytes = io.BytesIO(content)
upload_date = "Unknown date"
try:
if dict_info is not None and dict_info["upload_date"] is not None:
upload_date = dict_info["upload_date"]
upload_date = upload_date[6:8] + "/" + upload_date[4:6] + "/" + upload_date[0:4]
except:
pass
# Send cover
await event.answer_photo(image_bytes.read(),
caption=('<b>Track: {}</b>'
'\n{} - {}\n\n<a href="{}">' + __('track_link') + '</a>')
.format(
dict_info['title'],
dict_info["uploader"], upload_date,
"https://youtu.be/" + dict_info["id"]
),
parse_mode='HTML'
)
# Delete user message
await event.delete()
location = "tmp/yt/" + dict_info["id"] + '.mp3'
tmp_song = open(location, 'rb')
# TAG audio
audio = MP3(location, ID3=ID3)
try:
audio.add_tags()
except error:
pass
audio.tags.add(APIC(mime='image/jpeg', type=3, desc=u'Cover', data=image_bytes.read()))
audio.save()
# Create thumb
roi_img = crop_center(Image.open(image_bytes), 80, 80)
img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format='jpeg')
# Send audio
await event.answer_audio(tmp_song,
title=dict_info['title'],
performer=dict_info['uploader'],
thumb=img_byte_arr.getvalue(),
disable_notification=True)
tmp_song.close()
try:
shutil.rmtree(os.path.dirname(location))
except FileNotFoundError:
pass
except Exception as e:
traceback.print_exc()
await event.answer(__('download_error') + ' ' + str(e))
finally:
await tmp_msg.delete()
try:
downloading_users.remove(event.from_user.id)
except ValueError:
pass
else:
tmp_err_msg = await event.answer(__('running_download'))
await event.delete()
await asyncio.sleep(2)
await tmp_err_msg.delete()
#@dp.message_handler(regexp=r"https?:\/\/(?:www\.)?deezer\.com\/([a-z]*\/)?track\/(\d+)\/?$")
async def get_track(event: types.Message):
print(event.from_user)
while event.text.startswith('h') is False:
event.text = event.text[1:]
event.text = event.text.strip()
if event.from_user.id not in downloading_users:
tmp = event.text
if tmp[-1] == '/':
tmp = tmp[:-1]
tmp_msg = await event.answer(__('downloading'))
downloading_users.append(event.from_user.id)
try:
try:
dl = await download.download_trackdee(tmp, output_dir="tmp", quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True, not_interface=False)
except:
# Let's try again...
await asyncio.sleep(1)
dl = await download.download_trackdee(tmp, output_dir="tmp", quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True, not_interface=False)
tmp_track = requests.get(API_TRACK % quote(str(event.text.split('/')[-1]))).json()
tmp_cover = requests.get(tmp_track['album']['cover_xl'], stream=True).raw
tmp_artist_track = []
for c in tmp_track['contributors']:
tmp_artist_track.append(c['name'])
tmp_date = tmp_track['release_date'].split('-')
tmp_date = tmp_date[2] + '/' + tmp_date[1] + '/' + tmp_date[0]
await event.answer_photo(tmp_cover,
caption=('<b>Track: {}</b>'
'\n{} - {}\n<a href="{}">' + __('album_link')
+ '</a>\n<a href="{}">' + __('track_link') + '</a>')
.format(
tmp_track['title'], tmp_track['artist']['name'],
tmp_date, tmp_track['album']['link'], tmp_track['link']), parse_mode='HTML'
)
# Delete user message
await event.delete()
tmp_song = open(dl.song_path, 'rb')
duration = int(MP3(tmp_song).info.length)
tmp_song.seek(0)
await event.answer_audio(tmp_song,
title=tmp_track['title'],
performer=', '.join(tmp_artist_track),
duration=duration,
disable_notification=True)
await tmp_msg.delete()
tmp_song.close()
try:
shutil.rmtree(os.path.dirname(dl.song_path))
except FileNotFoundError:
pass
except Exception as e:
await tmp_msg.delete()
await event.answer(__('download_error') + ' ' + str(e))
finally:
try:
downloading_users.remove(event.from_user.id)
except ValueError:
pass
else:
tmp_err_msg = await event.answer(__('running_download'))
await event.delete()
await asyncio.sleep(2)
await tmp_err_msg.delete()
#@dp.message_handler(regexp=r"https?:\/\/(?:www\.)?deezer\.com\/([a-z]*\/)?album\/(\d+)\/?$")
async def get_album(event: types.Message):
print(event.from_user)
while event.text.startswith('h') is False:
event.text = event.text[1:]
event.text = event.text.strip()
if event.from_user.id not in downloading_users:
tmp = event.text
if tmp[-1] == '/':
tmp = tmp[:-1]
tmp_msg = await event.answer(__('downloading'))
downloading_users.append(event.from_user.id)
try:
try:
dl = await download.download_albumdee(tmp,
output_dir="tmp",
quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True,
not_interface=False)
except:
# Let's try again...
await asyncio.sleep(1)
dl = await download.download_albumdee(tmp,
output_dir="tmp",
quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True,
not_interface=False)
album = requests.get(API_ALBUM % quote(str(event.text.split('/')[-1]))).json()
tracks = requests.get(API_ALBUM % quote(str(event.text.split('/')[-1])) + '/tracks?limit=100').json()
tmp_cover = requests.get(album['cover_xl'], stream=True).raw
tmp_titles = []
tmp_artists = []
for track in tracks['data']:
tmp_titles.append(track['title'])
tmp_track = requests.get(API_TRACK % quote(str(track['id']))).json()
tmp_artist_track = []
for c in tmp_track['contributors']:
tmp_artist_track.append(c['name'])
tmp_artists.append(tmp_artist_track)
tmp_date = album['release_date'].split('-')
tmp_date = tmp_date[2] + '/' + tmp_date[1] + '/' + tmp_date[0]
await event.answer_photo(tmp_cover,
caption=('<b>Album: {}</b>\n{} - {}\n<a href="{}">' + __('album_link') + '</a>')
.format(
album['title'], album['artist']['name'],
tmp_date, album['link']
),
parse_mode='HTML')
# Delete user message
await event.delete()
try:
tmp_count = 0
group_media = []
if len(dl.tracks) < 2 or len(dl.tracks) > 10:
raise exceptions.NetworkError('One track !')
all_tracks = []
for i in dl.tracks:
tmp_song = open(i.song_path, 'rb')
all_tracks.append(tmp_song)
for track in all_tracks:
duration = int(MP3(track).info.length)
track.seek(0)
group_media.append(InputMediaAudio(media=track,
title=tmp_titles[tmp_count],
performer=', '.join(tmp_artists[tmp_count]),
duration=duration))
tmp_count += 1
await event.answer_media_group(group_media, disable_notification=True)
for track in all_tracks:
track.close()
except exceptions.NetworkError:
tmp_count = 0
all_tracks = []
for i in dl.tracks:
tmp_song = open(i.song_path, 'rb')
all_tracks.append(tmp_song)
for track in all_tracks:
duration = int(MP3(track).info.length)
track.seek(0)
await event.answer_audio(track,
title=tmp_titles[tmp_count],
performer=', '.join(tmp_artists[tmp_count]),
duration=duration,
disable_notification=True)
tmp_count += 1
await tmp_msg.delete()
try:
shutil.rmtree(os.path.dirname(dl.tracks[0].song_path))
except FileNotFoundError:
pass
for track in all_tracks:
track.close()
except Exception as e:
await tmp_msg.delete()
await event.answer(__('download_error') + ' ' + str(e))
finally:
try:
downloading_users.remove(event.from_user.id)
except ValueError:
pass
else:
tmp_err_msg = await event.answer(__('running_download'))
await event.delete()
await asyncio.sleep(2)
await tmp_err_msg.delete()
#@dp.message_handler(regexp=r"https?:\/\/(?:www\.)?deezer\.com\/([a-z]*\/)?playlist\/(\d+)\/?$")
async def get_playlist(event: types.Message):
print(event.from_user)
while event.text.startswith('h') is False:
event.text = event.text[1:]
event.text = event.text.strip()
if event.from_user.id not in downloading_users:
tmp = event.text
if tmp[-1] == '/':
tmp = tmp[:-1]
tmp_msg = await event.answer(__('downloading'))
downloading_users.append(event.from_user.id)
try:
try:
dl = await download.download_playlistdee(tmp,
output_dir="tmp",
quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True,
not_interface=False)
except:
# Let's try again...
await asyncio.sleep(1)
dl = await download.download_playlistdee(tmp,
output_dir="tmp",
quality_download=DEFAULT_QUALITY,
recursive_download=True,
recursive_quality=True,
not_interface=False)
album = requests.get(API_PLAYLIST % quote(str(event.text.split('/')[-1]))).json()
tracks = requests.get(API_PLAYLIST % quote(str(event.text.split('/')[-1])) + '/tracks?limit=100').json()
tmp_cover = requests.get(album['picture_xl'], stream=True).raw
tmp_titles = []
tmp_artists = []
for track in tracks['data']:
tmp_titles.append(track['title'])
tmp_track = requests.get(API_TRACK % quote(str(track['id']))).json()
tmp_artist_track = []
for c in tmp_track['contributors']:
tmp_artist_track.append(c['name'])
tmp_artists.append(tmp_artist_track)
tmp_count = 0
tmp_date = album['creation_date'].split(' ')[0].split('-')
tmp_date = tmp_date[2] + '/' + tmp_date[1] + '/' + tmp_date[0]
await event.answer_photo(tmp_cover,
caption=('<b>Playlist: {}</b>\n{} - {}\n<a href="{}">'
+ __('playlist_link') + '</a>').format(album['title'],
album['creator']['name'],
tmp_date,
album['link']
),
parse_mode='HTML')
# Delete user message
await event.delete()
all_tracks = []
for i in dl.tracks:
tmp_song = open(i.song_path, 'rb')
all_tracks.append(tmp_song)
for track in all_tracks:
duration = int(MP3(track).info.length)
track.seek(0)
await event.answer_audio(track,
title=tmp_titles[tmp_count],
performer=', '.join(tmp_artists[tmp_count]),
duration=duration,
disable_notification=True)
tmp_count += 1
await tmp_msg.delete()
for i in dl.tracks:
try:
shutil.rmtree(os.path.dirname(i.song_path))
except FileNotFoundError:
pass
for i in all_tracks:
i.close()
except Exception as e:
await tmp_msg.delete()
await event.answer(__('download_error') + ' ' + str(e))
finally:
try:
downloading_users.remove(event.from_user.id)
except ValueError:
pass
else:
tmp_err_msg = await event.answer(__('running_download'))
await event.delete()
await asyncio.sleep(2)
await tmp_err_msg.delete()
#@dp.message_handler(regexp=r"^https?:\/\/(?:www\.)?deezer.page.link\/.*$")
async def get_shortlink(event: types.Message):
r = requests.get(event.text)
real_link = r.url.split('?')[0]
# Add a message with a button to make it easier for the user
await event.answer('Please use the real link: ' + real_link,
reply_markup=types.InlineKeyboardMarkup().add(
types.InlineKeyboardButton('Real link', switch_inline_query_current_chat=real_link)))
@dp.message_handler(commands=['help', 'start'])
async def help_start(event: types.Message):
bot_info = await bot.get_me()
bot_name = bot_info.first_name.replace("_", "\\_").replace("*", "\\*").replace("[", "\\[").replace("`", "\\`")
bot_username = bot_info.username.replace("_", "\\_").replace("*", "\\*").replace("[", "\\[").replace("`", "\\`")
msg = "Hey, I'm *{}*\n".format(bot_name)
msg += "_You can use me in inline mode :_\n"
msg += "@{} \\(album\\|track\\|artist\\) \\<search\\>\n".format(bot_username)
msg += "Or just send an *Deezer* album or track *link* \\!"
await event.answer(msg, parse_mode="MarkdownV2")
@dp.inline_handler()
async def inline_echo(inline_query: InlineQuery):
items = []
if inline_query.query:
album = False
if inline_query.query.startswith('artist '):
album = True
tmp_text = 'artist:"{}"'.format(inline_query.query.split('artist ')[1])
text = API_SEARCH_TRK % quote(str(tmp_text))
elif inline_query.query.startswith('track '):
tmp_text = 'track:"{}"'.format(inline_query.query.split('track ')[1])
text = API_SEARCH_TRK % quote(str(tmp_text))
elif inline_query.query.startswith('album '):
album = True
tmp_text = 'album:"{}"'.format(inline_query.query.split('album ')[1])
text = API_SEARCH_TRK % quote(str(tmp_text))
else:
text = API_SEARCH_TRK % quote(str(inline_query.query))
try:
r = requests.get(text).json()
all_ids = []
for i in r['data']:
tmp_url = i['album']['tracklist']
tmp_id = re.search('/album/(.*)/tracks', tmp_url).group(1)
if not (album and tmp_id in all_ids):
tmp_album = requests.get(API_ALBUM % quote(str(tmp_id))).json()
all_ids.append(tmp_id)
tmp_date = tmp_album['release_date'].split('-')
tmp_date = tmp_date[2] + '/' + tmp_date[1] + '/' + tmp_date[0]
if album:
title = i['album']['title']
tmp_input = InputTextMessageContent(DEEZER_URL + "/album/%s" % quote(str(tmp_id)))
try:
nb = str(len(tmp_album['tracks']['data'])) + ' audio(s)'
except KeyError:
nb = ''
show_txt_album = ' | ' + nb + ' (album)'
else:
show_txt_album = ''
tmp_input = InputTextMessageContent(i['link'])
title = i['title']
result_id: str = i['id']
items.append(InlineQueryResultArticle(
id=result_id,
title=title,
description=i['artist']['name'] + ' | ' + tmp_date + show_txt_album,
thumb_url=i['album']['cover_small'],
input_message_content=tmp_input,
))
except KeyError as e:
print(e)
pass
except AttributeError as e:
print(e)
pass
await bot.answer_inline_query(inline_query.id, results=items, cache_time=100)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)