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_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'

View File

@ -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:

View File

@ -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,11 +21,15 @@ 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 = []
try:
if self.__connected:
if type == 'album':
musics = self.__get_album(code)
@ -33,6 +41,8 @@ class SpotifySearch():
musics = self.__get_artist(code)
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

View File

@ -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:
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:
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