Added exceptions for wrong URLs

This commit is contained in:
RemixDev 2020-06-29 13:58:06 +02:00
parent d628eb39e3
commit e5dd2bb4f0
3 changed files with 40 additions and 19 deletions

View File

@ -73,9 +73,12 @@ class Deezer:
json=args,
headers=self.http_headers
)
result_json = result.json()
except:
time.sleep(2)
return self.gw_api_call(method, args)
if len(result_json['error']):
raise APIError(json.dumps(result_json['error']))
return result.json()
def api_call(self, method, args=None):
@ -96,7 +99,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(json.dumps(result_json))
raise APIError(json.dumps(result_json['error']))
return result_json
def login(self, email, password, re_captcha_token, child=0):

View File

@ -2,6 +2,7 @@
from deemix.app.downloader import download
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt
from deemix.api.deezer import APIError
from spotipy.exceptions import SpotifyException
import logging
import json
@ -79,11 +80,16 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
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 ''}"
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
try:
trackAPI = dz.get_track_gw(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = "Wrong URL"
if "DATA_ERROR" in e:
result['error'] += f": {e['DATA_ERROR']}"
return result
if albumAPI:
trackAPI['_EXTRA_ALBUM'] = albumAPI
if settings['createSingleFolder']:
@ -115,9 +121,7 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
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 ''}"
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
if id.startswith('upc'):
id = albumAPI['id']
@ -158,7 +162,14 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
try:
playlistAPI = dz.get_playlist(id)
except:
try:
playlistAPI = dz.get_playlist_gw(id)['results']['DATA']
except APIError as e:
e = json.loads(str(e))
result['error'] = "Wrong URL"
if "DATA_ERROR" in e:
result['error'] += f": {e['DATA_ERROR']}"
return result
newPlaylist = {
'id': playlistAPI['PLAYLIST_ID'],
'title': playlistAPI['TITLE'],
@ -227,9 +238,7 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
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 ''}"
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
if interface:
interface.send("startAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
@ -241,38 +250,47 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
return albumList
elif type == "spotifytrack":
result = {}
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
return result
try:
track_id = sp.get_trackid_spotify(dz, id, settings['fallbackSearch'])
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
if track_id != 0:
return generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate)
else:
logger.warn("Track not found on deezer!")
result['error'] = "Track not found on deezer!"
elif type == "spotifyalbum":
result = {}
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
return result
try:
album_id = sp.get_albumid_spotify(dz, id)
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
if album_id != 0:
return generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate)
else:
logger.warn("Album not found on deezer!")
result['error'] = "Album not found on deezer!"
elif type == "spotifyplaylist":
result = {}
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
return result
if interface:
interface.send("startConvertingSpotifyPlaylist", str(id))
try:
playlist = sp.convert_spotify_playlist(dz, id, settings)
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
playlist['bitrate'] = bitrate
playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}"
result = playlist

View File

@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text()
setup(
name="deemix",
version="1.1.0",
version="1.1.1",
description="A barebone deezer downloader library",
long_description=README,
long_description_content_type="text/markdown",