2020-02-17 15:46:18 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-02-17 19:24:39 +00:00
|
|
|
import binascii
|
2020-03-23 16:04:58 +00:00
|
|
|
from Cryptodome.Hash import MD5
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-03-23 16:04:58 +00:00
|
|
|
from Cryptodome.Cipher import Blowfish, AES
|
2020-02-17 19:24:39 +00:00
|
|
|
import requests
|
|
|
|
|
2020-03-08 13:09:34 +00:00
|
|
|
import time
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
USER_AGENT_HEADER = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
|
|
|
|
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
class Deezer:
|
|
|
|
def __init__(self):
|
|
|
|
self.api_url = "http://www.deezer.com/ajax/gw-light.php"
|
|
|
|
self.legacy_api_url = "https://api.deezer.com/"
|
|
|
|
self.http_headers = {
|
2020-02-17 19:24:39 +00:00
|
|
|
"User-Agent": USER_AGENT_HEADER
|
|
|
|
}
|
2020-02-17 15:46:18 +00:00
|
|
|
self.album_pictures_host = "https://e-cdns-images.dzcdn.net/images/cover/"
|
|
|
|
self.artist_pictures_host = "https://e-cdns-images.dzcdn.net/images/artist/"
|
|
|
|
self.user = {}
|
|
|
|
self.session = requests.Session()
|
|
|
|
self.logged_in = False
|
|
|
|
self.session.post("http://www.deezer.com/", headers=self.http_headers)
|
2020-02-17 19:24:39 +00:00
|
|
|
self.sid = self.session.cookies.get('sid')
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
def get_token(self):
|
2020-02-17 19:24:39 +00:00
|
|
|
token_data = self.gw_api_call('deezer.getUserData')
|
|
|
|
return token_data["results"]["checkForm"]
|
|
|
|
|
|
|
|
def get_track_md5(self, sng_id):
|
2020-03-08 13:09:34 +00:00
|
|
|
try:
|
|
|
|
site = self.session.post(
|
|
|
|
"https://api.deezer.com/1.0/gateway.php",
|
|
|
|
params={
|
|
|
|
'api_key': "4VCYIJUCDLOUELGD1V8WBVYBNVDYOXEWSLLZDONGBBDFVXTZJRXPR29JRLQFO6ZE",
|
|
|
|
'sid': self.sid,
|
|
|
|
'input': '3',
|
|
|
|
'output': '3',
|
|
|
|
'method': 'song_getData'
|
|
|
|
},
|
|
|
|
timeout=30,
|
|
|
|
json={'sng_id': sng_id},
|
|
|
|
headers=self.http_headers
|
|
|
|
)
|
|
|
|
except:
|
2020-03-09 16:07:16 +00:00
|
|
|
time.sleep(2)
|
2020-03-08 13:09:34 +00:00
|
|
|
return self.get_track_md5(sng_id)
|
2020-02-17 19:24:39 +00:00
|
|
|
response = site.json()
|
2020-02-17 15:46:18 +00:00
|
|
|
return response['results']['PUID']
|
|
|
|
|
|
|
|
def gw_api_call(self, method, args={}):
|
2020-03-08 13:09:34 +00:00
|
|
|
try:
|
|
|
|
result = self.session.post(
|
|
|
|
self.api_url,
|
|
|
|
params={
|
|
|
|
'api_version': "1.0",
|
|
|
|
'api_token': 'null' if method == 'deezer.getUserData' else self.get_token(),
|
|
|
|
'input': '3',
|
|
|
|
'method': method
|
|
|
|
},
|
|
|
|
timeout=30,
|
|
|
|
json=args,
|
|
|
|
headers=self.http_headers
|
|
|
|
)
|
|
|
|
except:
|
2020-03-09 16:07:16 +00:00
|
|
|
time.sleep(2)
|
2020-03-08 13:09:34 +00:00
|
|
|
return self.gw_api_call(method, args)
|
2020-02-17 19:24:39 +00:00
|
|
|
return result.json()
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
def api_call(self, method, args={}):
|
2020-03-08 13:09:34 +00:00
|
|
|
try:
|
|
|
|
result = self.session.get(
|
|
|
|
self.legacy_api_url + method,
|
|
|
|
params=args,
|
|
|
|
headers=self.http_headers,
|
|
|
|
timeout=30
|
|
|
|
)
|
|
|
|
result_json = result.json()
|
|
|
|
except:
|
2020-03-09 16:07:16 +00:00
|
|
|
time.sleep(2)
|
2020-03-08 13:09:34 +00:00
|
|
|
return self.api_call(method, args)
|
2020-02-17 15:46:18 +00:00
|
|
|
if 'error' in result_json.keys():
|
|
|
|
raise APIError()
|
|
|
|
return result_json
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def login(self, email, password, re_captcha_token):
|
|
|
|
check_form_login = self.gw_api_call("deezer.getUserData")
|
2020-02-17 15:46:18 +00:00
|
|
|
login = self.session.post(
|
|
|
|
"https://www.deezer.com/ajax/action.php",
|
|
|
|
data={
|
2020-02-17 19:24:39 +00:00
|
|
|
'type': 'login',
|
|
|
|
'mail': email,
|
|
|
|
'password': password,
|
|
|
|
'checkFormLogin': check_form_login['results']['checkFormLogin'],
|
|
|
|
'reCaptchaToken': re_captcha_token
|
2020-02-17 15:46:18 +00:00
|
|
|
},
|
2020-02-17 19:24:39 +00:00
|
|
|
headers={'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', **self.http_headers}
|
2020-02-17 15:46:18 +00:00
|
|
|
)
|
2020-02-17 19:24:39 +00:00
|
|
|
if 'success' not in login.text:
|
2020-02-17 15:46:18 +00:00
|
|
|
self.logged_in = False
|
|
|
|
return False
|
2020-02-17 19:24:39 +00:00
|
|
|
user_data = self.gw_api_call("deezer.getUserData")
|
2020-02-17 15:46:18 +00:00
|
|
|
self.user = {
|
|
|
|
'email': email,
|
2020-02-17 19:24:39 +00:00
|
|
|
'id': user_data["results"]["USER"]["USER_ID"],
|
|
|
|
'name': user_data["results"]["USER"]["BLOG_NAME"],
|
|
|
|
'picture': user_data["results"]["USER"]["USER_PICTURE"] if "USER_PICTURE" in user_data["results"][
|
|
|
|
"USER"] else ""
|
2020-02-17 15:46:18 +00:00
|
|
|
}
|
|
|
|
self.logged_in = True
|
|
|
|
return True
|
|
|
|
|
|
|
|
def login_via_arl(self, arl):
|
|
|
|
cookie_obj = requests.cookies.create_cookie(
|
|
|
|
domain='deezer.com',
|
|
|
|
name='arl',
|
|
|
|
value=arl,
|
|
|
|
path="/",
|
|
|
|
rest={'HttpOnly': True}
|
|
|
|
)
|
|
|
|
self.session.cookies.set_cookie(cookie_obj)
|
2020-02-17 19:24:39 +00:00
|
|
|
user_data = self.gw_api_call("deezer.getUserData")
|
|
|
|
if user_data["results"]["USER"]["USER_ID"] == 0:
|
2020-02-17 15:46:18 +00:00
|
|
|
self.logged_in = False
|
|
|
|
return False
|
|
|
|
self.user = {
|
2020-02-17 19:24:39 +00:00
|
|
|
'id': user_data["results"]["USER"]["USER_ID"],
|
|
|
|
'name': user_data["results"]["USER"]["BLOG_NAME"],
|
|
|
|
'picture': user_data["results"]["USER"]["USER_PICTURE"] if "USER_PICTURE" in user_data["results"][
|
|
|
|
"USER"] else ""
|
2020-02-17 15:46:18 +00:00
|
|
|
}
|
|
|
|
self.logged_in = True
|
|
|
|
return True
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_track_gw(self, sng_id):
|
|
|
|
if int(sng_id) < 0:
|
|
|
|
body = self.gw_api_call('song.getData', {'sng_id': sng_id})
|
2020-02-17 15:46:18 +00:00
|
|
|
else:
|
2020-02-17 19:24:39 +00:00
|
|
|
body = self.gw_api_call('deezer.pageTrack', {'sng_id': sng_id})
|
2020-02-17 15:46:18 +00:00
|
|
|
if 'LYRICS' in body['results']:
|
|
|
|
body['results']['DATA']['LYRICS'] = body['results']['LYRICS']
|
|
|
|
body['results'] = body['results']['DATA']
|
|
|
|
return body['results']
|
|
|
|
|
|
|
|
def get_tracks_gw(self, ids):
|
2020-02-17 19:24:39 +00:00
|
|
|
tracks_array = []
|
2020-02-17 15:46:18 +00:00
|
|
|
body = self.gw_api_call('song.getListData', {'sng_ids': ids})
|
|
|
|
errors = 0
|
|
|
|
for i in range(len(ids)):
|
|
|
|
if ids[i] != 0:
|
2020-02-17 19:24:39 +00:00
|
|
|
tracks_array.append(body['results']['data'][i - errors])
|
2020-02-17 15:46:18 +00:00
|
|
|
else:
|
|
|
|
errors += 1
|
2020-02-17 19:24:39 +00:00
|
|
|
tracks_array.append({
|
2020-02-17 15:46:18 +00:00
|
|
|
'SNG_ID': 0,
|
|
|
|
'SNG_TITLE': '',
|
|
|
|
'DURATION': 0,
|
|
|
|
'MD5_ORIGIN': 0,
|
|
|
|
'MEDIA_VERSION': 0,
|
|
|
|
'FILESIZE': 0,
|
|
|
|
'ALB_TITLE': "",
|
|
|
|
'ALB_PICTURE': "",
|
|
|
|
'ART_ID': 0,
|
|
|
|
'ART_NAME': ""
|
|
|
|
})
|
2020-02-17 19:24:39 +00:00
|
|
|
return tracks_array
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_album_gw(self, alb_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.gw_api_call('album.getData', {'alb_id': alb_id})['results']
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_album_tracks_gw(self, alb_id):
|
|
|
|
tracks_array = []
|
|
|
|
body = self.gw_api_call('song.getListByAlbum', {'alb_id': alb_id, 'nb': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
for track in body['results']['data']:
|
|
|
|
_track = track
|
|
|
|
_track['position'] = body['results']['data'].index(track)
|
2020-02-17 19:24:39 +00:00
|
|
|
tracks_array.append(_track)
|
|
|
|
return tracks_array
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_artist_gw(self, art_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.gw_api_call('deezer.pageArtist', {'art_id': art_id})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_playlist_gw(self, playlist_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.gw_api_call('deezer.pagePlaylist', {'playlist_id': playlist_id})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_playlist_tracks_gw(self, playlist_id):
|
|
|
|
tracks_array = []
|
|
|
|
body = self.gw_api_call('playlist.getSongs', {'playlist_id': playlist_id, 'nb': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
for track in body['results']['data']:
|
2020-02-17 19:24:39 +00:00
|
|
|
track['position'] = body['results']['data'].index(track)
|
|
|
|
tracks_array.append(track)
|
|
|
|
return tracks_array
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_artist_toptracks_gw(self, art_id):
|
|
|
|
tracks_array = []
|
|
|
|
body = self.gw_api_call('artist.getTopTrack', {'art_id': art_id, 'nb': 100})
|
2020-02-17 15:46:18 +00:00
|
|
|
for track in body['results']['data']:
|
2020-02-17 19:24:39 +00:00
|
|
|
track['position'] = body['results']['data'].index(track)
|
|
|
|
tracks_array.append(track)
|
|
|
|
return tracks_array
|
|
|
|
|
|
|
|
def get_lyrics_gw(self, sng_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.gw_api_call('song.getLyrics', {'sng_id': sng_id})["results"]
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_user_playlist(self, user_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('user/' + str(user_id) + '/playlists', {'limit': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_track(self, user_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('track/' + str(user_id))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
def get_track_by_ISRC(self, isrc):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('track/isrc:' + isrc)
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
def get_charts_top_country(self):
|
|
|
|
return self.get_user_playlist('637006841')
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_playlist(self, playlist_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('playlist/' + str(playlist_id))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_playlist_tracks(self, playlist_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('playlist/' + str(playlist_id) + '/tracks', {'limit': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_album(self, album_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('album/' + str(album_id))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
|
|
|
def get_album_by_UPC(self, upc):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('album/upc:' + str(upc))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_album_tracks(self, album_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('album/' + str(album_id) + '/tracks', {'limit': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_artist(self, artist_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('artist/' + str(artist_id))
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def get_artist_albums(self, artist_id):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('artist/' + str(artist_id) + '/albums', {'limit': -1})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def search(self, term, search_type, limit=30):
|
2020-03-24 14:56:28 +00:00
|
|
|
return self.api_call('search/' + search_type, {'q': term, 'limit': limit})
|
2020-02-17 15:46:18 +00:00
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def decrypt_track(self, track_id, input, output):
|
2020-02-17 15:46:18 +00:00
|
|
|
response = open(input, 'rb')
|
|
|
|
outfile = open(output, 'wb')
|
2020-02-29 21:21:56 +00:00
|
|
|
blowfish_key = str.encode(self._get_blowfish_key(str(track_id)))
|
2020-02-17 19:24:39 +00:00
|
|
|
i = 0
|
2020-02-17 15:46:18 +00:00
|
|
|
while True:
|
|
|
|
chunk = response.read(2048)
|
|
|
|
if not chunk:
|
|
|
|
break
|
2020-02-17 19:24:39 +00:00
|
|
|
if (i % 3) == 0 and len(chunk) == 2048:
|
2020-03-01 20:50:53 +00:00
|
|
|
chunk = Blowfish.new(blowfish_key, Blowfish.MODE_CBC, b"\x00\x01\x02\x03\x04\x05\x06\x07").decrypt(chunk)
|
2020-02-17 15:46:18 +00:00
|
|
|
outfile.write(chunk)
|
|
|
|
i += 1
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
def stream_track(self, track_id, url, stream):
|
2020-03-02 12:46:48 +00:00
|
|
|
request = requests.get(url, headers=self.http_headers, stream=True)
|
2020-03-01 20:48:57 +00:00
|
|
|
request.raise_for_status()
|
2020-02-29 21:21:56 +00:00
|
|
|
blowfish_key = str.encode(self._get_blowfish_key(str(track_id)))
|
2020-02-17 19:24:39 +00:00
|
|
|
i = 0
|
2020-02-29 19:40:03 +00:00
|
|
|
for chunk in request.iter_content(2048):
|
2020-02-17 19:24:39 +00:00
|
|
|
if (i % 3) == 0 and len(chunk) == 2048:
|
2020-02-29 22:58:33 +00:00
|
|
|
chunk = Blowfish.new(blowfish_key, Blowfish.MODE_CBC, b"\x00\x01\x02\x03\x04\x05\x06\x07").decrypt(chunk)
|
2020-02-17 15:46:18 +00:00
|
|
|
stream.write(chunk)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
def _md5(self, data):
|
2020-03-23 16:03:04 +00:00
|
|
|
h = MD5.new()
|
2020-02-17 15:46:18 +00:00
|
|
|
h.update(str.encode(data) if isinstance(data, str) else data)
|
|
|
|
return h.hexdigest()
|
|
|
|
|
|
|
|
def _ecb_crypt(self, key, data):
|
|
|
|
res = b''
|
2020-03-24 14:56:28 +00:00
|
|
|
for _ in range(int(len(data) / 16)):
|
2020-03-23 16:03:04 +00:00
|
|
|
res += binascii.hexlify(AES.new(key, AES.MODE_ECB).encrypt(data[:16]))
|
2020-02-17 15:46:18 +00:00
|
|
|
data = data[16:]
|
|
|
|
return res
|
|
|
|
|
|
|
|
def _get_blowfish_key(self, trackId):
|
2020-02-17 19:24:39 +00:00
|
|
|
SECRET = 'g4el58wc' + '0zvf9na1'
|
2020-02-17 15:46:18 +00:00
|
|
|
idMd5 = self._md5(trackId)
|
|
|
|
bfKey = ""
|
|
|
|
for i in range(16):
|
2020-02-17 19:24:39 +00:00
|
|
|
bfKey += chr(ord(idMd5[i]) ^ ord(idMd5[i + 16]) ^ ord(SECRET[i]))
|
2020-02-17 15:46:18 +00:00
|
|
|
return bfKey
|
|
|
|
|
|
|
|
def get_track_stream_url(self, sng_id, md5, media_version, format):
|
2020-02-17 19:24:39 +00:00
|
|
|
urlPart = b'\xa4'.join(
|
|
|
|
[str.encode(md5), str.encode(str(format)), str.encode(str(sng_id)), str.encode(str(media_version))])
|
2020-02-17 15:46:18 +00:00
|
|
|
md5val = self._md5(urlPart)
|
2020-02-17 19:24:39 +00:00
|
|
|
step2 = str.encode(md5val) + b'\xa4' + urlPart + b'\xa4'
|
|
|
|
while len(step2) % 16 > 0:
|
2020-02-17 15:46:18 +00:00
|
|
|
step2 += b'.'
|
|
|
|
urlPart = self._ecb_crypt(b'jo6aey6haid2Teih', step2)
|
|
|
|
return "https://e-cdns-proxy-" + md5[0] + ".dzcdn.net/mobile/1/" + urlPart.decode("utf-8")
|
|
|
|
|
2020-02-17 19:24:39 +00:00
|
|
|
|
2020-02-17 15:46:18 +00:00
|
|
|
class APIError(Exception):
|
|
|
|
pass
|