Skip to content

Commit f1b1a4c

Browse files
committed
added Deezer support
1 parent f7d9325 commit f1b1a4c

File tree

3 files changed

+211
-1
lines changed

3 files changed

+211
-1
lines changed

deezer.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import requests
2+
3+
class Deezer(object):
4+
5+
def __init__(self):
6+
7+
'''
8+
Init function
9+
Creating deezer object
10+
:return: None
11+
'''
12+
13+
self.__url = 'http://api.deezer.com/'
14+
15+
16+
def getSongInfo(self, id):
17+
18+
try:
19+
20+
response = requests.get(f'{self.__url}/track/{id}').json()
21+
22+
return ({
23+
'uri' : f"D{response['id']}T",
24+
'name' : response['title'],
25+
'artist' : [response['artist']['name']],
26+
'album' : response['album']['title'],
27+
'image' : response['album']['cover_xl'],
28+
'duration_ms' : response['duration']
29+
})
30+
31+
except: return None
32+
33+
def getAlbum(self, id):
34+
35+
try:
36+
37+
response = requests.get(f'{self.__url}/album/{id}').json()
38+
39+
alb = {
40+
'name':response['title'],
41+
'artist':response['artist']['name'],
42+
'copyright': None,
43+
'image':response['cover_xl'],
44+
}
45+
46+
tracks = []
47+
48+
for item in response['tracks']['data']:
49+
50+
tracks.append({
51+
'uri' : f"D{item['id']}T",
52+
'name' : item['title'],
53+
'artist' : [item['artist']['name']],
54+
'album' : alb['name'],
55+
'image' : alb['image'],
56+
'preview_url' : item['preview'],
57+
'duration_ms' : item['duration']
58+
})
59+
60+
alb.setdefault(
61+
'tracks', tracks
62+
)
63+
64+
return alb
65+
66+
except: return None
67+
68+
if __name__ == '__main__':
69+
70+
deezer = Deezer()
71+
data = deezer.getSongInfo('636758392')
72+
73+
print(data)

main.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from youtube import Youtube
44
from editor import TagEditor
55
from lastfm import LastFM
6+
from deezer import Deezer
67
import sys, getopt, shutil
78
import os
89

@@ -22,6 +23,7 @@ def __init__(self):
2223
self.__spotify = Spotify()
2324
self.__editor = TagEditor()
2425
self.__last = LastFM()
26+
self.__deezer = Deezer()
2527

2628

2729
def __downloadMusicFromYoutube(self, name, uri, dur):
@@ -59,6 +61,9 @@ def getData(self, uri):
5961
def getLastFMTags(self, name):
6062
return self.__last.get(name)
6163

64+
def getDeezerTags(self, id):
65+
return self.__deezer.getSongInfo(id)
66+
6267
def getYoutubeMusicInfo(self, url):
6368
return self.__youtube.getNameFromYoutube(url)
6469

@@ -302,12 +307,70 @@ def downloadFromYoutubeMusic(self, url, info):
302307
else:
303308
return False, None
304309

310+
def downloadByDeezerID(self, uri):
311+
#get info
312+
info = self.__deezer.getSongInfo(uri)
313+
314+
if info:
315+
316+
fixed_name = f'{info["artist"][0]} - {info["name"]}'
317+
fixed_name = fixed_name.replace('.','')
318+
fixed_name = fixed_name.replace(',','')
319+
fixed_name = fixed_name.replace("'",'')
320+
fixed_name = fixed_name.replace("/","")
321+
322+
#finding and download from YouTube and tagging
323+
if self.__downloadMusicFromYoutube(fixed_name, info['uri'], info['duration_ms']):
324+
325+
self.__editor.setTags(
326+
data=info
327+
)
328+
329+
cachepath = os.getcwd() + '/cache'
330+
fullpath = os.getcwd() + '/Downloads'
331+
332+
#logging
333+
logging.info(f'CACHEPATH {cachepath}')
334+
logging.info(f'FULLPATH {fullpath}')
335+
336+
if not os.path.exists(fullpath):
337+
os.makedirs(fullpath)
338+
339+
os.rename(
340+
f"{cachepath}/{info['uri']}/{info['uri']}.png",
341+
f"{fullpath}/{info['uri']}.png"
342+
)
343+
#logging
344+
logging.info(f"MOVE TO Downloads/{info['uri']}.png")
345+
346+
os.rename(
347+
f"{cachepath}/{info['uri']}/{info['uri']}.mp3",
348+
f"{fullpath}/{info['uri']}.mp3"
349+
)
350+
#logging
351+
logging.info(f"MOVE TO Downloads/{info['uri']}.mp3")
352+
353+
#deleting cache
354+
try:
355+
shutil.rmtree(f"cache/{info['uri']}")
356+
#logging
357+
logging.info(f"DELETED cache/{info['uri']}")
358+
except:
359+
#logging
360+
logging.error(f"DELETING cache/{info['uri']}")
361+
362+
return True
363+
return False
364+
305365
def search(self, query):
306366
return self.__spotify.search(query=query)
307367

308368
def getAlbum(self, uri):
309369
return self.__spotify.getAlbum(uri)
310370

371+
def getAlbumDeezer(self, id):
372+
return self.__deezer.getAlbum(id)
373+
311374

312375

313376
class CLI(object):

telegram.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def DL_SPOTIFY_ALBUM(self, message, user):
304304

305305
for data, i in zip(album['tracks'], range(count)):
306306
#logging
307-
logging.info(f'ALBUM {i+1}/{count} | {data["artist"][0]} - {data["name"]}')
307+
logging.info(f'S-ALBUM {i+1}/{count} | {data["artist"][0]} - {data["name"]}')
308308

309309
if self.downloader.downloadBySpotifyUri(data['uri']):
310310

@@ -390,6 +390,41 @@ def DL_YOUTUBE_MUSIC(self, message, user):
390390

391391
return True
392392

393+
def DL_DEEZER_ALBUM(self, message, user):
394+
395+
uri = message
396+
397+
data = self.downloader.getAlbumDeezer(uri)
398+
path = f"Downloads/{uri}.png"
399+
400+
401+
downloadAlbumImage(data['image'], path)
402+
logging.info(f'Downloaded {path}')
403+
404+
self.bot.sendPhoto(
405+
chat_id=user,
406+
photo=open(path,'rb'),
407+
text=f'Album <b>{data["name"]}</b> by <b>{data["artist"]}</b>'
408+
)
409+
410+
logging.info(f'Sended {path}')
411+
album = data
412+
count = len(album['tracks'])
413+
414+
for data, i in zip(album['tracks'], range(count)):
415+
#logging
416+
logging.info(f'D-ALBUM {i+1}/{count} | {data["artist"][0]} - {data["name"]}')
417+
418+
if self.downloader.downloadByDeezerID(str(data['uri'][1:-1])):
419+
420+
self.sendSong(data=data, user=user)
421+
422+
os.remove(path)
423+
#logging
424+
logging.info(f'DELETED {path}')
425+
426+
return True
427+
393428

394429
def sendSong(self, data, user):
395430

@@ -489,6 +524,12 @@ def classify(self, message):
489524
elif str(message) == '/status':
490525
return 'status'
491526

527+
elif str(message).find('deezer.com/track/') > 0:
528+
return 'dtrack'
529+
530+
elif str(message).find('deezer.com/album/') > 0:
531+
return 'dalbum'
532+
492533
else:
493534
return 'text'
494535

@@ -577,6 +618,39 @@ def controller(self, message, id):
577618

578619
return self.DL_QUERY(message, user=id)
579620

621+
elif type == 'dtrack':
622+
623+
#logging
624+
logging.info(f'DEEZER TRACK DETECTED')
625+
626+
track = str(str(message).split('/track/')[1]).split('?')[0]
627+
data = self.downloader.getDeezerTags(track)
628+
629+
if data:
630+
631+
#logging
632+
logging.info(f'SONG {data["artist"][0]} - {data["name"]}')
633+
634+
if self.downloader.downloadByDeezerID(track):
635+
636+
return self.sendSong(data=data, user=id)
637+
638+
else:
639+
640+
#logging
641+
logging.error(f'SENDED "Something went wrong" MESSAGE')
642+
self.bot.sendSticker(id,sticker=open(f"Data/s3.webp",'rb'),)
643+
self.bot.sendText(id,text='Couldn\'t find that:(')
644+
645+
return False
646+
647+
elif type == 'dalbum':
648+
649+
#logging
650+
logging.info(f'DEEZER ALBUM DETECTED')
651+
album = str(str(message).split('album/')[1]).split('?')[0]
652+
653+
return self.DL_DEEZER_ALBUM(album, id)
580654

581655
elif type == 'link':
582656

0 commit comments

Comments
 (0)