Adding Deezer Module and Deezer Tests

This commit is contained in:
Rafael Vargas
2022-07-10 16:08:22 -03:00
parent c826af229c
commit 863b079a01
11 changed files with 232 additions and 21 deletions

72
Music/DeezerSearcher.py Normal file
View File

@@ -0,0 +1,72 @@
import deezer
from Exceptions.Exceptions import DeezerError
from Config.Messages import DeezerMessages
class DeezerSearcher:
def __init__(self) -> None:
self.__client = deezer.Client()
self.__messages = DeezerMessages()
self.__acceptedTypes = ['track', 'artist', 'playlist', 'album']
def search(self, url: str) -> None:
if not self.__verifyValidUrl(url):
raise DeezerError(self.__messages.INVALID_DEEZER_URL, self.__messages.GENERIC_TITLE)
urlType = url.split('/')[4].split('?')[0]
code = int(url.split('/')[5].split('?')[0])
try:
musics = []
if urlType == 'album':
musics = self.__get_album(code)
elif urlType == 'playlist':
musics = self.__get_playlist(code)
elif urlType == 'track':
musics = self.__get_track(code)
elif urlType == 'artist':
musics = self.__get_artist(code)
return musics
except Exception as e:
print(f'[DEEZER ERROR] -> {e}')
raise DeezerError(self.__messages.INVALID_DEEZER_URL, self.__messages.GENERIC_TITLE)
def __get_album(self, code: int) -> list:
album = self.__client.get_album(code)
return [track.title for track in album.tracks]
def __get_track(self, code: int) -> list:
track = self.__client.get_track(code)
return [track.title]
def __get_playlist(self, code: int) -> list:
playlist = self.__client.get_playlist(code)
return [track.title for track in playlist.tracks]
def __get_artist(self, code: int) -> list:
artist = self.__client.get_artist(code)
topMusics = artist.get_top()
return [track.title for track in topMusics]
def __verifyValidUrl(self, url: str) -> bool:
try:
urlType = url.split('/')[4].split('?')[0]
code = url.split('/')[5].split('?')[0]
code = int(code)
if urlType == '' or code == '':
return False
if urlType not in self.__acceptedTypes:
return False
return True
except:
return False

View File

@@ -65,6 +65,7 @@ class Downloader():
with YoutubeDL(options) as ydl:
try:
extracted_info = ydl.extract_info(url, download=False)
# Some links doesn't extract unless extract_flat key is passed as False in options
if self.__failed_to_extract(extracted_info):
extracted_info = self.__get_forced_extracted_info(url)

View File

@@ -1,7 +1,8 @@
from Exceptions.Exceptions import InvalidInput, SpotifyError, YoutubeError
from Exceptions.Exceptions import DeezerError, InvalidInput, SpotifyError, YoutubeError
from Music.Downloader import Downloader
from Music.Types import Provider
from Music.Spotify import SpotifySearch
from Music.DeezerSearcher import DeezerSearcher
from Utils.Utils import Utils
from Utils.UrlAnalyzer import URLAnalyzer
from Config.Messages import SearchMessages
@@ -9,7 +10,8 @@ from Config.Messages import SearchMessages
class Searcher():
def __init__(self) -> None:
self.__Spotify = SpotifySearch()
self.__spotify = SpotifySearch()
self.__deezer = DeezerSearcher()
self.__messages = SearchMessages()
self.__down = Downloader()
@@ -24,20 +26,35 @@ class Searcher():
musics = await self.__down.extract_info(track)
return musics
except:
raise YoutubeError(self.__messages.YOUTUBE_ERROR, self.__messages.GENERIC_TITLE)
raise YoutubeError(self.__messages.YOUTUBE_NOT_FOUND, self.__messages.GENERIC_TITLE)
elif provider == Provider.Spotify:
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)
raise SpotifyError(self.__messages.SPOTIFY_NOT_FOUND,
self.__messages.GENERIC_TITLE)
return musics
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_NOT_FOUND, self.__messages.GENERIC_TITLE)
elif provider == Provider.Deezer:
try:
musics = self.__deezer.search(track)
if musics == None or len(musics) == 0:
raise DeezerError(self.__messages.DEEZER_NOT_FOUND,
self.__messages.GENERIC_TITLE)
return musics
except DeezerError as error:
raise error # Redirect already processed error
except Exception as e:
print(f'[Deezer Error] -> {e}')
raise DeezerError(self.__messages.DEEZER_NOT_FOUND, self.__messages.GENERIC_TITLE)
elif provider == Provider.Name:
return [track]
@@ -52,7 +69,7 @@ class Searcher():
if 'start_radio' or 'index' in trackAnalyzer.queryParams.keys():
return trackAnalyzer.getCleanedUrl()
def __identify_source(self, track) -> Provider:
def __identify_source(self, track: str) -> Provider:
if not Utils.is_url(track):
return Provider.Name
@@ -62,4 +79,7 @@ class Searcher():
if "https://open.spotify.com" in track:
return Provider.Spotify
if "https://www.deezer.com" in track:
return Provider.Deezer
return Provider.Unknown

View File

@@ -1,8 +1,9 @@
from enum import Enum
class Provider(Enum):
class Provider(str, Enum):
Spotify = 'Spotify'
Deezer = 'Deezer'
YouTube = 'YouTube'
Name = 'Track Name'
Unknown = 'Unknown'