Upgrading Spotify Invalid Input Dealing

This commit is contained in:
Rafael Vargas 2022-07-10 14:18:07 -03:00
parent 8336a95eda
commit 7e9a6d45c0
4 changed files with 69 additions and 27 deletions

View File

@ -76,5 +76,13 @@ class SearchMessages(Singleton):
self.UNKNOWN_INPUT = f'This type of input was too strange, try something else or type {config.BOT_PREFIX}help play' self.UNKNOWN_INPUT = f'This type of input was too strange, try something else or type {config.BOT_PREFIX}help play'
self.UNKNOWN_INPUT_TITLE = 'Nothing Found' self.UNKNOWN_INPUT_TITLE = 'Nothing Found'
self.SPOTIFY_ERROR = 'Spotify could not process any songs with this input, verify your link or try again later.' self.SPOTIFY_ERROR = 'Spotify could not process any songs with this input, verify your link or try again later.'
self.GENERIC_TITLE = 'Input could not be processed' self.GENERIC_TITLE = 'URL could not be processed'
self.YOUTUBE_ERROR = 'Youtube could not process any songs with this input, verify your link or try again later.' self.YOUTUBE_ERROR = 'Youtube could not process any songs with this input, verify your link or try again later.'
self.INVALID_SPOTIFY_URL = 'Invalid Spotify URL, verify your link.'
class SpotifyMessages(Singleton):
def __init__(self) -> None:
if not super().created:
self.INVALID_SPOTIFY_URL = 'Invalid Spotify URL, verify your link.'
self.GENERIC_TITLE = 'URL could not be processed'

View File

@ -29,8 +29,14 @@ class Searcher():
elif provider == Provider.Spotify: elif provider == Provider.Spotify:
try: try:
musics = self.__Spotify.search(track) musics = self.__Spotify.search(track)
if musics == None or len(musics) == 0:
raise SpotifyError(self.__messages.SPOTIFY_ERROR, self.__messages.GENERIC_TITLE)
return musics return musics
except: except SpotifyError as error:
raise error # Redirect already processed error
except Exception as e:
print(f'[Spotify Error] -> {e}')
raise SpotifyError(self.__messages.SPOTIFY_ERROR, self.__messages.GENERIC_TITLE) raise SpotifyError(self.__messages.SPOTIFY_ERROR, self.__messages.GENERIC_TITLE)
elif provider == Provider.Name: elif provider == Provider.Name:

View File

@ -1,10 +1,14 @@
from spotipy import Spotify from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.exceptions import SpotifyException
from Exceptions.Exceptions import SpotifyError
from Config.Configs import Configs from Config.Configs import Configs
from Config.Messages import SpotifyMessages
class SpotifySearch(): class SpotifySearch():
def __init__(self) -> None: def __init__(self) -> None:
self.__messages = SpotifyMessages()
self.__config = Configs() self.__config = Configs()
self.__connected = False self.__connected = False
self.__connect() self.__connect()
@ -17,22 +21,28 @@ class SpotifySearch():
except Exception as e: except Exception as e:
print(f'DEVELOPER NOTE -> Spotify Connection Error {e}') print(f'DEVELOPER NOTE -> Spotify Connection Error {e}')
def search(self, music: str) -> list: def search(self, url: str) -> list:
type = music.split('/')[3].split('?')[0] if not self.__checkUrlValid(url):
code = music.split('/')[4].split('?')[0] raise SpotifyError(self.__messages.INVALID_SPOTIFY_URL, self.__messages.GENERIC_TITLE)
type = url.split('/')[3].split('?')[0]
code = url.split('/')[4].split('?')[0]
musics = [] musics = []
if self.__connected: try:
if type == 'album': if self.__connected:
musics = self.__get_album(code) if type == 'album':
elif type == 'playlist': musics = self.__get_album(code)
musics = self.__get_playlist(code) elif type == 'playlist':
elif type == 'track': musics = self.__get_playlist(code)
musics = self.__get_track(code) elif type == 'track':
elif type == 'artist': musics = self.__get_track(code)
musics = self.__get_artist(code) elif type == 'artist':
musics = self.__get_artist(code)
return musics return musics
except SpotifyException:
raise SpotifyError(self.__messages.INVALID_SPOTIFY_URL, self.__messages.GENERIC_TITLE)
def __get_album(self, code: str) -> list: def __get_album(self, code: str) -> list:
results = self.__api.album_tracks(code) results = self.__api.album_tracks(code)
@ -94,3 +104,15 @@ class SpotifySearch():
title += f'{artist["name"]} ' title += f'{artist["name"]} '
return title return title
def __checkUrlValid(self, url: str) -> bool:
try:
type = url.split('/')[3].split('?')[0]
code = url.split('/')[4].split('?')[0]
if type == '' or code == '':
return False
return True
except:
return False

View File

@ -1,4 +1,7 @@
from requests import HTTPError
from Music.Spotify import SpotifySearch
from Tests.TestBase import VulkanTesterBase from Tests.TestBase import VulkanTesterBase
from Exceptions.Exceptions import SpotifyError
class VulkanSpotifyTest(VulkanTesterBase): class VulkanSpotifyTest(VulkanTesterBase):
@ -41,22 +44,25 @@ class VulkanSpotifyTest(VulkanTesterBase):
else: else:
return False return False
def test_spotifyWrongUrlOne(self) -> bool: def test_spotifyWrongUrlShouldThrowException(self) -> bool:
musics = self._runner.run_coroutine( try:
self._searcher.search(self._constants.SPOTIFY_WRONG1_URL)) musics = self._runner.run_coroutine(
self._searcher.search(self._constants.SPOTIFY_WRONG1_URL))
print(musics) except SpotifyError as e:
if len(musics) == 0: print(f'Spotify Error -> {e.message}')
return True return True
else: except Exception as e:
print(e)
return False return False
def test_spotifyWrongUrlTwo(self) -> bool: def test_spotifyWrongUrlTwoShouldThrowException(self) -> bool:
musics = self._runner.run_coroutine( try:
self._searcher.search(self._constants.SPOTIFY_WRONG2_URL)) musics = self._runner.run_coroutine(
self._searcher.search(self._constants.SPOTIFY_WRONG2_URL))
print(musics) except SpotifyError as e:
if len(musics) == 0: print(f'Spotify Error -> {e.message}')
return True return True
else: except Exception as e:
return False return False