def get_playlist_tracks(playlistUrl: str) -> List[SongObj]: ''' `str` `playlistUrl` : Spotify Url of the album whose tracks are to be retrieved returns a `list<songObj>` containing Url's of each track in the given playlist ''' spotifyClient = SpotifyClient() playlistTracks = [] playlistResponse = spotifyClient.playlist_tracks(playlistUrl) # while loop to mimic do-while while True: for songEntry in playlistResponse['items']: if songEntry['track'] is None or songEntry['track']['id'] is None: continue song = SongObj.from_url('https://open.spotify.com/track/' + songEntry['track']['id']) if song.get_youtube_link() is not None: playlistTracks.append(song) # check if more tracks are to be passed if playlistResponse['next']: playlistResponse = spotifyClient.playlist_tracks( playlistUrl, offset=playlistResponse['offset'] + playlistResponse['limit']) else: break return playlistTracks
def search_for_song(query: str) -> SongObj: ''' `str` `query` : what you'd type into spotify's search box Queries Spotify for a song and returns the best match ''' # get a spotify client spotifyClient = get_spotify_client() # get possible matches from spotify result = spotifyClient.search(query, type='track') # return first result link or if no matches are found, raise and Exception if len(result['tracks']['items']) == 0: raise Exception('No song matches found on Spotify') else: for songResult in result['tracks']['items']: songUrl = 'http://open.spotify.com/track/' + songResult['id'] song = SongObj.from_url(songUrl) if song.get_youtube_link() != None: return song raise Exception('Could not match any of the results on YouTube')
def get_album_tracks(albumUrl: str) -> List[SongObj]: ''' `str` `albumUrl` : Spotify Url of the album whose tracks are to be retrieved returns a `list<songObj>` containing Url's of each track in the given album ''' spotifyClient = get_spotify_client() albumTracks = [] trackResponse = spotifyClient.album_tracks(albumUrl) # while loop acts like do-while while True: for track in trackResponse['items']: song = SongObj.from_url('https://open.spotify.com/track/' + track['id']) if song.get_youtube_link() != None: albumTracks.append(song) # check if more tracks are to be passed if trackResponse['next']: trackResponse = spotifyClient.album_tracks(albumUrl, offset=len(albumTracks)) else: break return albumTracks
def get_artist_tracks(artistUrl: str) -> List[SongObj]: ''' `str` `albumUrl` : Spotify Url of the artist whose tracks are to be retrieved returns a `list<songObj>` containing Url's of each track in the artist profile ''' spotifyClient = SpotifyClient() artistTracks = [] offset = 0 artistResponse = spotifyClient.artist_albums(artistUrl) # while loop acts like do-while while True: for album in artistResponse['items']: # get albums and singles if not (album['album_group'] == 'appears_on' and album['album_type'] in ['album', 'compilation']): artistTracks.extend(get_album_tracks(album['id'])) # get features from other artists albums elif album['album_group'] == 'appears_on' and album[ 'album_type'] == 'album': trackResponse = spotifyClient.album_tracks(album['uri']) albumTracks = [] # while loop acts like do-while while True: for track in trackResponse['items']: for artist in track['artists']: if artist['id'] == artistResponse['href'].split( '/')[-2]: song = SongObj.from_url( 'https://open.spotify.com/track/' + track['id']) if song.get_youtube_link() is not None: albumTracks.append(song) # check if more tracks are to be passed if trackResponse['next']: trackResponse = spotifyClient.album_tracks( album['uri'], offset=len(albumTracks)) else: break artistTracks.extend(albumTracks) offset += len(artistResponse['items']) # check if more albums are to be passed if artistResponse['next']: artistResponse = spotifyClient.artist_albums(artistUrl, offset=offset) else: break return artistTracks
def console_entry_point(): ''' This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. ''' arguments = parse_arguments() SpotifyClient.init(client_id='4fe3fecfe5334023a1472516cc99d805', client_secret='0f02b7c483c04257984695007a4a8d5c') if arguments.path: if not os.path.isdir(arguments.path): sys.exit("The output directory doesn't exist.") print(f"Will download to: {os.path.abspath(arguments.path)}") os.chdir(arguments.path) with DownloadManager() as downloader: for request in arguments.url: if 'open.spotify.com' in request and 'track' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() is not None: downloader.download_single_song(song) else: print( 'Skipping %s (%s) as no match could be found on youtube' % (song.get_song_name(), request)) elif 'open.spotify.com' in request and 'album' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'playlist' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'artist' in request: print('Fetching artist...') artistObjList = get_artist_tracks(request) downloader.download_multiple_songs(artistObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception as e: print(e)
def console_entry_point(): ''' This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. ''' if '--help' in sys.argv or '-h' in sys.argv or len(sys.argv) == 1: print(help_notice) #! We use 'return None' as a convenient exit/break from the function return None spotifyClient.initialize( clientId='4fe3fecfe5334023a1472516cc99d805', clientSecret='0f02b7c483c04257984695007a4a8d5c' ) downloader = DownloadManager() for request in sys.argv[1:]: if 'open.spotify.com' in request and 'track' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() != None: downloader.download_single_song(song) else: print('Skipping %s (%s) as no match could be found on youtube' % ( song.get_song_name(), request )) elif 'open.spotify.com' in request and 'album' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'playlist' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception: print('No song named "%s" could be found on spotify' % request) downloader.close()
def dlmusic(): Dir=Dirfield.get() url=musfield.get() temp=SongObj.from_url(url) currdir=os.getcwd() os.chdir(Dir) download=DownloadManager() download.download_single_song(songObj=temp) os.remove('./Temp') os.chdir(currdir) return
def get_song_obj_from_url(songEntry, playlistResponse, spotifyClient, playlistUrl): playlistTracks = [] song = SongObj.from_url('https://open.spotify.com/track/' + songEntry['track']['id']) print('Got song info: %s' % (song.get_song_name())) if song.get_youtube_link() != None: playlistTracks.append(song) # check if more tracks are to be passed if playlistResponse['next']: playlistResponse = spotifyClient.playlist_tracks( playlistUrl, offset=len(playlistTracks)) return playlistTracks
def process_request(downloader, request): if 'open.spotify.com' in request and 'track' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() is not None: downloader.download_single_song(song) else: print('Skipping %s (%s) as no match could be found on youtube' % (song.get_song_name(), request)) elif 'open.spotify.com' in request and 'album' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'playlist' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'artist' in request: print('Fetching artist...') artistObjList = get_artist_tracks(request) downloader.download_multiple_songs(artistObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception as e: print(e)
def console_entry_point(): ''' This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. ''' arguments = parse_arguments() if ffmpeg.has_correct_version(arguments.ignore_ffmpeg_version) is False: sys.exit(1) SpotifyClient.init(client_id='5f573c9620494bae87890c0f08a60293', client_secret='212476d9b0f3472eaa762d90b19b0ba8') if arguments.path: if not os.path.isdir(arguments.path): sys.exit("The output directory doesn't exist.") print(f"Will download to: {os.path.abspath(arguments.path)}") os.chdir(arguments.path) with DownloadManager() as downloader: for request in arguments.url: if 'open.spotify.com' in request and 'track' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() is not None: downloader.download_single_song(song) else: print( 'Skipping %s (%s) as no match could be found on youtube' % (song.get_song_name(), request)) elif 'open.spotify.com' in request and 'album' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'playlist' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'artist' in request: print('Fetching artist...') artistObjList = get_artist_tracks(request) downloader.download_multiple_songs(artistObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception as e: print(e)
from spotdl.search.songObj import SongObj from spotdl.search.spotifyClient import initialize from spotdl.lyrics.genius import Genius initialize( clientId="4fe3fecfe5334023a1472516cc99d805", clientSecret="0f02b7c483c04257984695007a4a8d5c", ) # g = Genius() # print(g.from_query("A$ap Rocky", "j",lyric_fail=True)) s = SongObj.from_url( 'spotify:track:02kDW379Yfd5PzW5A6vuGt?context=spotify%3Aplaylist%3A37i9dQZF1DXcBWIGoYBM5M' ) print(s.get_lyrics()) # print(Genius.from_query("Pop Smoke", "For The Night"))
def console_entry_point(): ''' This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. ''' if '--help' in cliArgs or '-h' in cliArgs: print(help_notice) #! We use 'return None' as a convenient exit/break from the function return None if '--quiet' in cliArgs: #! removing --quiet so it doesnt mess up with the download cliArgs.remove('--quiet') #! make stdout & stderr silent sys.stdout = quiet() sys.stderr = quiet() initialize(clientId='4fe3fecfe5334023a1472516cc99d805', clientSecret='0f02b7c483c04257984695007a4a8d5c') downloader = DownloadManager() for request in cliArgs[1:]: if ('open.spotify.com' in request and 'track' in request) or 'spotify:track:' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() != None: downloader.download_single_song(song) else: print( 'Skipping %s (%s) as no match could be found on youtube' % (song.get_song_name(), request)) elif ('open.spotify.com' in request and 'album' in request) or 'spotify:album:' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif ('open.spotify.com' in request and 'playlist' in request) or 'spotify:playlist:' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif request.endswith('.txt'): print('Fetching songs from %s...' % request) songObjList = [] with open(request, 'r') as songFile: for songLink in songFile.readlines(): song = SongObj.from_url(songLink) songObjList.append(song) downloader.download_multiple_songs(songObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception: print('No song named "%s" could be found on spotify' % request) downloader.close()
def console_entry_point(): ''' This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. ''' arguments = parse_arguments() spotifyClient.initialize(clientId='03eb56e5ab2843e98507b3a6a0359a56', clientSecret='4e6600fae80845ef8dab67ccaaecee4d') if arguments.path: if not os.path.isdir(arguments.path): sys.exit("The output directory doesn't exist.") print(f"Will download to: {os.path.abspath(arguments.path)}") os.chdir(arguments.path) downloader = DownloadManager() for request in arguments.url: if 'open.spotify.com' in request and 'track' in request: print('Fetching Song...') song = SongObj.from_url(request) if song.get_youtube_link() is not None: downloader.download_single_song(song) else: print( 'Skipping %s (%s) as no match could be found on youtube' % (song.get_song_name(), request)) elif 'open.spotify.com' in request and 'album' in request: print('Fetching Album...') songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'playlist' in request: print('Fetching Playlist...') songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif 'open.spotify.com' in request and 'artist' in request: print('Fetching artist...') artistObjList = get_artist_tracks(request) downloader.download_multiple_songs(artistObjList) elif request.endswith('.spotdlTrackingFile'): print('Preparing to resume download...') downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception: print('No song named "%s" could be found on spotify' % request) downloader.close()
def console_entry_point(): """ This is where all the console processing magic happens. Its super simple, rudimentary even but, it's dead simple & it works. """ if "--help" in cliArgs or "-h" in cliArgs: print(help_notice) #! We use 'return None' as a convenient exit/break from the function return None initialize( clientId="4fe3fecfe5334023a1472516cc99d805", clientSecret="0f02b7c483c04257984695007a4a8d5c", ) downloader = DownloadManager() for request in cliArgs[1:]: if "?" in request: # strip unnecessary data for both url and uri # e.g https://open.spotify.com/track/4Q34FP1AT7GEl9oLgNtiWj?context=spotify%3Aplaylist%3A37i9dQZF1DXcBWIGoYBM5M&si=DlMAsJ5pSD6tdUSn2XqB0g # becomes https://open.spotify.com/track/4Q34FP1AT7GEl9oLgNtiWj # e.g spotify:track:4Q34FP1AT7GEl9oLgNtiWj?context=spotify%3Aplaylist%3A37i9dQZF1DXcBWIGoYBM5M # becomes spotify:track:4Q34FP1AT7GEl9oLgNtiWj request = request[:request.find("?")] if "open.spotify.com" in request: # it's a url if "track" in request: print("Fetching Song...") song = SongObj.from_url(request) if song.get_youtube_link() != None: downloader.download_single_song(song) else: print( "Skipping %s (%s) as no match could be found on youtube" % (song.get_song_name(), request)) elif "album" in request: print("Fetching Album...") songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif "playlist" in request: print("Fetching Playlist...") songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif "spotify:" in request: # it's a URI with format Spotify:...:ID if "track:" in request: print("Fetching Song...") # yes, passing a URI to this function still works coz it relies on another # spotipy function that simply extracts the ID, ideally u can just pass the ID # and the track downloads song = SongObj.from_url(request) if song.get_youtube_link() != None: downloader.download_single_song(song) else: print( f"Skipping {song.get_song_name()} ({request}) as no match could be found on youtube" ) elif "album:" in request: print("Fetching Album...") songObjList = get_album_tracks(request) downloader.download_multiple_songs(songObjList) elif "playlist:" in request: print("Fetching Playlist...") songObjList = get_playlist_tracks(request) downloader.download_multiple_songs(songObjList) elif request.endswith(".spotdlTrackingFile"): print("Preparing to resume download...") downloader.resume_download_from_tracking_file(request) else: print('Searching for song "%s"...' % request) try: song = search_for_song(request) downloader.download_single_song(song) except Exception: print('No song named "%s" could be found on spotify' % request) downloader.close()