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, json=args,
headers=self.http_headers headers=self.http_headers
) )
result_json = result.json()
except: except:
time.sleep(2) time.sleep(2)
return self.gw_api_call(method, args) return self.gw_api_call(method, args)
if len(result_json['error']):
raise APIError(json.dumps(result_json['error']))
return result.json() return result.json()
def api_call(self, method, args=None): 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: if 'code' in result_json['error'] and result_json['error']['code'] == 4:
time.sleep(5) time.sleep(5)
return self.api_call(method, args) return self.api_call(method, args)
raise APIError(json.dumps(result_json)) raise APIError(json.dumps(result_json['error']))
return result_json return result_json
def login(self, email, password, re_captcha_token, child=0): def login(self, email, password, re_captcha_token, child=0):

View File

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

View File

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