Ejemplo n.º 1
0
def get_playlist_songs(auth_token, playlist):
    url = "https://api.spotify.com/v1/playlists/{}/tracks?limit={}&offset={}"
    songs = list()
    offset = 0
    limit = 50
    get_playlist_songs_response = requests.get(url.format(playlist.id, limit, offset),
                                               headers={'Authorization': header.format(auth_token)})

    while (len(json.loads(get_playlist_songs_response.text).get("items"))):

        for response_item in json.loads(get_playlist_songs_response.text).get("items"):
            response_song = response_item.get("track")
            if (response_song.get("is_local")):
                continue
            new_song = Song(
                response_song.get("id"),
                response_song.get("name"),
                response_song.get("artists")[0].get("name")
            )
            new_song.is_known = True
            songs.append(new_song)

        offset += limit
        get_playlist_songs_response = requests.get(url.format(playlist.id, limit, offset),
                                                   headers={'Authorization': header.format(auth_token)})

    return songs
def get_favorites_songs(auth_token):
    get_song_url = "https://api.spotify.com/v1/me/tracks?market=DE&limit={}&offset={}"
    songs = list()
    offset = 0
    limit = 50
    get_song_response = requests.get(
        get_song_url.format(limit, offset),
        headers={'Authorization': header.format(auth_token)})

    while (get_song_response.status_code != 502):

        full_response = json.loads(get_song_response.text).get("items")

        if (len(full_response) == 0):
            break

        for response_item in full_response:
            response_song = response_item.get("track")
            if (response_song.get("is_local")):
                continue
            new_song = Song(response_song.get("id"), response_song.get("name"),
                            response_song.get("artists")[0].get("name"))
            new_song.is_known = True
            songs.append(new_song)

        offset += limit
        get_song_response = requests.get(
            get_song_url.format(limit, offset),
            headers={'Authorization': header.format(auth_token)})

    return songs
Ejemplo n.º 3
0
def get_playlists(auth_token):
    url = "https://api.spotify.com/v1/me/playlists"
    playlists = list()
    response = requests.get(url, headers={'Authorization': header.format(auth_token)})

    for response_item in json.loads(response.text).get("items"):
        playlists.append(Playlist(response_item.get("id"), response_item.get("name"),
                                  response_item.get("external_urls").get("spotify")))

    return playlists
Ejemplo n.º 4
0
def create_playlist(name, describtion, public, auth_token, user_id):
    data = json.dumps({
        'name': name,
        'description': describtion,
        'public': public
    })
    url = "https://api.spotify.com/v1/users/{user_id}/playlists".format(user_id=user_id)

    response = requests.post(url=url, data=data, headers={'Authorization': header.format(auth_token)})
    response_json = json.loads(response.text)
    return Playlist(response_json.get("id"), name, response_json.get("external_urls").get("spotify"))
def set_features_for_songs(auth_token, songs):
    get_song_features_url = "https://api.spotify.com/v1/audio-features?ids={}"
    offset = 0
    limit = 0
    step_size = 50
    id_concat_list = list()

    for song in songs:
        id_concat_list.append(song.id + "%2C")

    while limit < len(id_concat_list):
        id_concat = ""

        if limit + step_size > len(id_concat_list):
            limit = len(id_concat_list)
        else:
            limit = limit + step_size

        for id in range(offset, limit):
            id_concat += id_concat_list[id]

        get_feature_response = requests.get(
            get_song_features_url.format(id_concat),
            headers={'Authorization': header.format(auth_token)})

        while (get_feature_response.status_code != 200):
            get_feature_response = requests.get(
                get_song_features_url.format(id_concat),
                headers={'Authorization': header.format(auth_token)})

        response_features = json.loads(get_feature_response.text)

        for feature in response_features.get("audio_features"):
            songs[offset].set_features(
                Features(feature.get("danceability"), feature.get("energy"),
                         feature.get("acousticness"), feature.get("liveness"),
                         feature.get("valence"), feature.get("tempo")))
            offset += 1
Ejemplo n.º 6
0
def add_songs_to_playlist(auth_token, songs, playlist_id):
    if (len(songs) > 50):
        i = 0
        while (i < len(songs)):
            add_songs_to_playlist(auth_token, songs[i:i + 50], playlist_id)
            if i + 50 >= len(songs):
                i += len(songs) - i
            else:
                i += 50
    else:
        songs_uri = ""
        for song in songs:
            songs_uri += "spotify%3Atrack%3A{}%2C".format(song.id)
        songs_uri = songs_uri[:-3]

        url = "https://api.spotify.com/v1/playlists/{playlist_id}/tracks?uris={tracks}".format(playlist_id=playlist_id,
                                                                                               tracks=songs_uri)

        requests.post(url=url, headers={'Authorization': header.format(auth_token)})
Ejemplo n.º 7
0
def get_user_info(auth_token):
    get_me_url = "https://api.spotify.com/v1/me"
    get_me_response = json.loads(requests.get(get_me_url, headers={'Authorization': header.format(auth_token)}).text)
    return get_me_response.get("id"), get_me_response.get("display_name")