mirror of
https://github.com/RafaelSolVargas/Vulkan.git
synced 2025-10-29 16:57:23 +00:00
Upgrading PlayController and Spotify Connection
This commit is contained in:
107
Music/Spotify.py
107
Music/Spotify.py
@@ -1,4 +1,4 @@
|
||||
import spotipy
|
||||
from spotipy import Spotify
|
||||
from spotipy.oauth2 import SpotifyClientCredentials
|
||||
from Config.Config import Configs
|
||||
|
||||
@@ -9,86 +9,67 @@ class SpotifySearch():
|
||||
self.__connected = False
|
||||
self.__connect()
|
||||
|
||||
@property
|
||||
def connected(self):
|
||||
return self.__connected
|
||||
|
||||
def __connect(self) -> bool:
|
||||
def __connect(self) -> None:
|
||||
try:
|
||||
# Initialize the connection with Spotify API
|
||||
self.__api = spotipy.Spotify(auth_manager=SpotifyClientCredentials(
|
||||
client_id=self.__config.SPOTIFY_ID, client_secret=self.__config.SPOTIFY_SECRET))
|
||||
auth = SpotifyClientCredentials(self.__config.SPOTIFY_ID, self.__config.SPOTIFY_SECRET)
|
||||
self.__api = Spotify(auth_manager=auth)
|
||||
self.__connected = True
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f'DEVELOPER NOTE -> Spotify Connection {e}')
|
||||
return False
|
||||
print(f'DEVELOPER NOTE -> Spotify Connection Error {e}')
|
||||
|
||||
def search(self, music=str) -> list:
|
||||
def search(self, music: str) -> list:
|
||||
type = music.split('/')[3].split('?')[0]
|
||||
code = music.split('/')[4].split('?')[0]
|
||||
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)
|
||||
else:
|
||||
return None
|
||||
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)
|
||||
|
||||
return musics
|
||||
|
||||
def __get_album(self, code=str) -> list:
|
||||
if self.__connected == True:
|
||||
try:
|
||||
results = self.__api.album_tracks(code)
|
||||
musics = results['items']
|
||||
def __get_album(self, code: str) -> list:
|
||||
results = self.__api.album_tracks(code)
|
||||
musics = results['items']
|
||||
|
||||
while results['next']: # Get the next pages
|
||||
results = self.__api.next(results)
|
||||
musics.extend(results['items'])
|
||||
while results['next']: # Get the next pages
|
||||
results = self.__api.next(results)
|
||||
musics.extend(results['items'])
|
||||
|
||||
musicsTitle = []
|
||||
musicsTitle = []
|
||||
|
||||
for music in musics:
|
||||
try:
|
||||
title = self.__extract_title(music)
|
||||
musicsTitle.append(title)
|
||||
except:
|
||||
pass
|
||||
return musicsTitle
|
||||
except Exception as e:
|
||||
raise e
|
||||
for music in musics:
|
||||
title = self.__extract_title(music)
|
||||
musicsTitle.append(title)
|
||||
|
||||
def __get_playlist(self, code=str) -> list:
|
||||
try:
|
||||
results = self.__api.playlist_items(code)
|
||||
itens = results['items']
|
||||
return musicsTitle
|
||||
|
||||
while results['next']: # Load the next pages
|
||||
results = self.__api.next(results)
|
||||
itens.extend(results['items'])
|
||||
def __get_playlist(self, code: str) -> list:
|
||||
results = self.__api.playlist_items(code)
|
||||
itens = results['items']
|
||||
|
||||
musics = []
|
||||
for item in itens:
|
||||
musics.append(item['track'])
|
||||
while results['next']: # Load the next pages
|
||||
results = self.__api.next(results)
|
||||
itens.extend(results['items'])
|
||||
|
||||
titles = []
|
||||
for music in musics:
|
||||
try:
|
||||
title = self.__extract_title(music)
|
||||
titles.append(title)
|
||||
except Exception as e:
|
||||
raise e
|
||||
musics = []
|
||||
for item in itens:
|
||||
musics.append(item['track'])
|
||||
|
||||
return titles
|
||||
titles = []
|
||||
for music in musics:
|
||||
title = self.__extract_title(music)
|
||||
titles.append(title)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
return titles
|
||||
|
||||
def __get_track(self, code=str) -> list:
|
||||
def __get_track(self, code: str) -> list:
|
||||
results = self.__api.track(code)
|
||||
name = results['name']
|
||||
artists = ''
|
||||
@@ -97,7 +78,7 @@ class SpotifySearch():
|
||||
|
||||
return [f'{name} {artists}']
|
||||
|
||||
def __get_artist(self, code=str) -> list:
|
||||
def __get_artist(self, code: str) -> list:
|
||||
results = self.__api.artist_top_tracks(code, country='BR')
|
||||
|
||||
musics_titles = []
|
||||
|
||||
Reference in New Issue
Block a user