diff --git a/Config/Messages.py b/Config/Messages.py index 3f31f57..afe26b0 100644 --- a/Config/Messages.py +++ b/Config/Messages.py @@ -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_TITLE = 'Nothing Found' 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.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' diff --git a/Music/Searcher.py b/Music/Searcher.py index 42a7855..30624a9 100644 --- a/Music/Searcher.py +++ b/Music/Searcher.py @@ -29,8 +29,14 @@ class Searcher(): elif provider == Provider.Spotify: try: musics = self.__Spotify.search(track) + if musics == None or len(musics) == 0: + raise SpotifyError(self.__messages.SPOTIFY_ERROR, self.__messages.GENERIC_TITLE) + 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) elif provider == Provider.Name: diff --git a/Music/Spotify.py b/Music/Spotify.py index 758060a..5e575f4 100644 --- a/Music/Spotify.py +++ b/Music/Spotify.py @@ -1,10 +1,14 @@ from spotipy import Spotify from spotipy.oauth2 import SpotifyClientCredentials +from spotipy.exceptions import SpotifyException +from Exceptions.Exceptions import SpotifyError from Config.Configs import Configs +from Config.Messages import SpotifyMessages class SpotifySearch(): def __init__(self) -> None: + self.__messages = SpotifyMessages() self.__config = Configs() self.__connected = False self.__connect() @@ -17,22 +21,28 @@ class SpotifySearch(): except Exception as e: print(f'DEVELOPER NOTE -> Spotify Connection Error {e}') - def search(self, music: str) -> list: - type = music.split('/')[3].split('?')[0] - code = music.split('/')[4].split('?')[0] + def search(self, url: str) -> list: + if not self.__checkUrlValid(url): + raise SpotifyError(self.__messages.INVALID_SPOTIFY_URL, self.__messages.GENERIC_TITLE) + + type = url.split('/')[3].split('?')[0] + code = url.split('/')[4].split('?')[0] musics = [] - if self.__connected: - if type == 'album': - musics = self.__get_album(code) - elif type == 'playlist': - musics = self.__get_playlist(code) - elif type == 'track': - musics = self.__get_track(code) - elif type == 'artist': - musics = self.__get_artist(code) + try: + if self.__connected: + if type == 'album': + musics = self.__get_album(code) + elif type == 'playlist': + musics = self.__get_playlist(code) + elif type == 'track': + musics = self.__get_track(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: results = self.__api.album_tracks(code) @@ -94,3 +104,15 @@ class SpotifySearch(): title += f'{artist["name"]} ' 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 diff --git a/Tests/VSpotifyTests.py b/Tests/VSpotifyTests.py index cd58cb3..ad5235c 100644 --- a/Tests/VSpotifyTests.py +++ b/Tests/VSpotifyTests.py @@ -1,4 +1,7 @@ +from requests import HTTPError +from Music.Spotify import SpotifySearch from Tests.TestBase import VulkanTesterBase +from Exceptions.Exceptions import SpotifyError class VulkanSpotifyTest(VulkanTesterBase): @@ -41,22 +44,25 @@ class VulkanSpotifyTest(VulkanTesterBase): else: return False - def test_spotifyWrongUrlOne(self) -> bool: - musics = self._runner.run_coroutine( - self._searcher.search(self._constants.SPOTIFY_WRONG1_URL)) + def test_spotifyWrongUrlShouldThrowException(self) -> bool: + try: + musics = self._runner.run_coroutine( + self._searcher.search(self._constants.SPOTIFY_WRONG1_URL)) - print(musics) - if len(musics) == 0: + except SpotifyError as e: + print(f'Spotify Error -> {e.message}') return True - else: + except Exception as e: + print(e) return False - def test_spotifyWrongUrlTwo(self) -> bool: - musics = self._runner.run_coroutine( - self._searcher.search(self._constants.SPOTIFY_WRONG2_URL)) + def test_spotifyWrongUrlTwoShouldThrowException(self) -> bool: + try: + musics = self._runner.run_coroutine( + self._searcher.search(self._constants.SPOTIFY_WRONG2_URL)) - print(musics) - if len(musics) == 0: + except SpotifyError as e: + print(f'Spotify Error -> {e.message}') return True - else: + except Exception as e: return False