コード例 #1
0
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
    '''

    # initialize worker pool
    print('Retrieving song information...')
    print()
    workerPool = Pool(4)

    spotifyClient = get_spotify_client()

    playlistResponse = spotifyClient.playlist_tracks(playlistUrl)

    playlistTracks = workerPool.starmap(
        func=get_song_obj_from_url,
        iterable=((songEntry, playlistResponse, spotifyClient, playlistUrl)
                  for songEntry in playlistResponse['items']),
        chunksize=50)
    print()
    print('Done retrieving song information!')

    playlistTracks = [ent for sublist in playlistTracks for ent in sublist]

    return playlistTracks
コード例 #2
0
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')
コード例 #3
0
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
コード例 #4
0
ファイル: utils.py プロジェクト: zvimoss/spotify-downloader
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 = get_spotify_client()
    playlistTracks = []

    playlistResponse = spotifyClient.playlist_tracks(playlistUrl)

    # while loop to mimic do-while
    while True:

        for songEntry in playlistResponse['items']:
            song = SongObj.from_url(
                'https://open.spotify.com/track/' + songEntry['track']['id'])
            
            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)
            )
        else:
            break
    
    return playlistTracks
コード例 #5
0
    def from_url(cls, spotifyURL: str):
        # check if URL is a playlist, user, artist or album, if yes raise an Exception,
        # else procede
        if not ('open.spotify.com' in spotifyURL and 'track' in spotifyURL):
            raise Exception('passed URL is not that of a track: %s' %
                            spotifyURL)

        # query spotify for song, artist, album details
        spotifyClient = get_spotify_client()

        rawTrackMeta = spotifyClient.track(spotifyURL)

        primaryArtistId = rawTrackMeta['artists'][0]['id']
        rawArtistMeta = spotifyClient.artist(primaryArtistId)

        albumId = rawTrackMeta['album']['id']
        rawAlbumMeta = spotifyClient.album(albumId)

        # get best match from the given provider
        songName = rawTrackMeta['name']

        albumName = rawTrackMeta['album']['name']

        duration = round(rawTrackMeta['duration_ms'] / 1000, ndigits=3)

        contributingArtists = []

        for artist in rawTrackMeta['artists']:
            contributingArtists.append(artist['name'])

        youtubeLink = SongObj.searchProvider(songName, contributingArtists,
                                             albumName, duration)

        return cls(rawTrackMeta, rawAlbumMeta, rawArtistMeta, youtubeLink)
コード例 #6
0
ファイル: utils.py プロジェクト: okoibraun/spotify-downloader
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 = get_spotify_client()
    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
コード例 #7
0
def new_initialize(clientId, clientSecret):
    """This function allows calling `initialize()` multiple times"""
    try:
        return spotifyClient.get_spotify_client()
    except:
        return ORIGINAL_INITIALIZE(clientId, clientSecret)