From f72fe5ceee302d58212ab3d1781d42dfed8f3da4 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 26 Jun 2020 12:20:48 +0200 Subject: [PATCH] Added exceptions for wrong URLs and added support for UPC and ISRC URLs --- deemix/api/deezer.py | 3 ++- deemix/app/queuemanager.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/deemix/api/deezer.py b/deemix/api/deezer.py index 16826d9..1639a32 100755 --- a/deemix/api/deezer.py +++ b/deemix/api/deezer.py @@ -7,6 +7,7 @@ from Cryptodome.Cipher import Blowfish, AES from Cryptodome.Hash import MD5 from Cryptodome.Util.Padding import pad import re +import json USER_AGENT_HEADER = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/79.0.3945.130 Safari/537.36" @@ -95,7 +96,7 @@ class Deezer: if 'code' in result_json['error'] and result_json['error']['code'] == 4: time.sleep(5) return self.api_call(method, args) - raise APIError(result_json) + raise APIError(json.dumps(result_json)) return result_json def login(self, email, password, re_captcha_token, child=0): diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index 776c514..0db4c80 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 from deemix.app.downloader import download from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt +from deemix.api.deezer import APIError import logging +import json logging.basicConfig(level=logging.INFO) logger = logging.getLogger('deemix') @@ -67,6 +69,20 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf logger.warn("URL not recognized") result['error'] = "URL not recognized" elif type == "track": + if id.startswith("isrc"): + try: + trackAPI = dz.get_track(id) + if 'id' in dz_track and 'title' in dz_track: + id = trackAPI['id'] + else: + result['error'] = "Track ISRC is not available on deezer" + return result + except APIError as e: + e = json.loads(str(e)) + result['error'] = "Wrong URL" + if 'error' in e: + result['error'] += f": {e['error']['type']+': ' if 'type' in e['error'] else ''}{e['error']['message'] if 'message' in e['error'] else ''}" + return result trackAPI = dz.get_track_gw(id) if albumAPI: trackAPI['_EXTRA_ALBUM'] = albumAPI @@ -95,7 +111,16 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf result['single'] = trackAPI elif type == "album": - albumAPI = dz.get_album(id) + try: + albumAPI = dz.get_album(id) + except APIError as e: + e = json.loads(str(e)) + result['error'] = "Wrong URL" + if 'error' in e: + result['error'] += f": {e['error']['type']+': ' if 'type' in e['error'] else ''}{e['error']['message'] if 'message' in e['error'] else ''}" + return result + if id.startswith('upc'): + id = albumAPI['id'] albumAPI_gw = dz.get_album_gw(id) albumAPI['nb_disk'] = albumAPI_gw['NUMBER_DISK'] albumAPI['copyright'] = albumAPI_gw['COPYRIGHT'] @@ -198,7 +223,14 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf playlistAPI['explicit'] = False elif type == "artist": - artistAPI = dz.get_artist(id) + try: + albumAPI = artistAPI = dz.get_artist(id) + except APIError as e: + e = json.loads(str(e)) + result['error'] = "Wrong URL" + if 'error' in e: + result['error'] += f": {e['error']['type']+': ' if 'type' in e['error'] else ''}{e['error']['message'] if 'message' in e['error'] else ''}" + return result if interface: interface.send("startAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']}) artistAPITracks = dz.get_artist_albums(id)