Exemple #1
0
def songs():
    """Show list of songs in playlist that user clicked on"""

    if session.get("user_id") is not None and session.get(
            "track_ids") is not None:
        saved_spotify_info = spotify.saved_songs(spotify.generate_token(),
                                                 session["track_ids"])

    saved_spotify_info_list = []
    for track in saved_spotify_info["tracks"]:
        saved_spotify_info_dict = {}
        millis = int(track["duration_ms"])
        minutes = millis / (1000 * 60)
        minutes = int(minutes)
        seconds = (millis % (1000 * 60)) / 1000
        seconds = int(seconds)
        if seconds == 0:
            seconds = "00"
        if seconds == 1:
            seconds = "01"
        if seconds == 2:
            seconds = "02"
        if seconds == 3:
            seconds = "03"
        if seconds == 4:
            seconds = "04"
        if seconds == 5:
            seconds = "05"
        if seconds == 6:
            seconds = "06"
        if seconds == 7:
            seconds = "07"
        if seconds == 8:
            seconds = "08"
        if seconds == 9:
            seconds = "09"
        track["duration_ms"] = f'{minutes}:{seconds}'

        saved_spotify_info_dict["name"] = track["name"]
        saved_spotify_info_dict["artist"] = [
            f' {artist["name"]}' for artist in track["artists"]
        ]
        saved_spotify_info_dict["album"] = track["album"]["name"]
        saved_spotify_info_dict["url"] = track["preview_url"]
        saved_spotify_info_dict["cover_art_url"] = track["album"]["images"][0][
            "url"]
        saved_spotify_info_list.append(saved_spotify_info_dict)

    final_spotify_dict = {}
    final_spotify_dict["songs"] = saved_spotify_info_list

    return render_template("amplitude_user_songs_page.html",
                           saved_spotify_info=saved_spotify_info,
                           final_spotify_dict=final_spotify_dict)
def lastfm_fav_to_spotify_playlist():
    """Main method of the project that brings together other modules that are using APIs."""
    (loved_tracks, spotify_username, playlist_name) = extract_variables()

    try:
        token = spotify.generate_token()
    except spotify.TokenGenerationException:
        print('Error generating token.')  # GUI => dialog window
    else:
        sp = spotify.create_spotify_object(token)
        tracks_ids = spotify.create_spotify_tracks_ids_list_from_loved(loved_tracks, sp)
        playlist_id = spotify.create_playlist_for_user(sp, spotify_username, playlist_name)
        spotify.add_tracks_to_playlist(sp, spotify_username, playlist_id, tracks_ids)
 def test_1_generate_token(self):
     """Method testing Spotify API token generation. Needs to be run as first, because
     generated token is used in further tests."""
     SpotifyTest.token = spotify.generate_token()
     self.assertIsNotNone(SpotifyTest.token)
Exemple #4
0
 def test_1_generate_token(self):
     """Method testing Spotify API token generation. Needs to be run as first, because
     generated token is used in further tests."""
     SpotifyTest.token = spotify.generate_token()
     self.assertIsNotNone(SpotifyTest.token)
Exemple #5
0
def generate_playlist():

    original_spotify_info = spotify.base_playlist(
        spotify.generate_token(), session["genre"].lower(),
        session["minimum_danceability"], session["maximum_danceability"])

    clean_array = []

    index = 0
    for track in original_spotify_info["tracks"]:

        if track["preview_url"] is not None:
            clean_array.append(track)
        index += 1

    spotify_info = {"tracks": clean_array}

    if len(spotify_info.get("tracks")) <= 0:
        flash(
            "Change the search parameters of danceability. No playlist was generated."
        )
        return redirect("/create")
    else:

        playlist = Playlist(
            user_id=session["user_id"],
            playlist_image=spotify_info["tracks"][0]["album"]["images"][1]
            ["url"],
            playlist_genre=session["genre"].lower(),
            playlist_mindanceability=session["minimum_danceability"],
            playlist_maxdanceability=session["maximum_danceability"])
        db.session.add(playlist)
        db.session.commit()

        #store songs into Song database and song-playlist data into SongPlaylist database
        for track in spotify_info["tracks"]:
            #if Song database is empty, add generated song as new song in the database
            if len(db.session.query(Song).all()) <= 0:
                song = Song(
                    track_id=track["id"],
                    track_title=track["name"],
                    artist=[artist["name"] for artist in track["artists"]])
                db.session.add(song)
                db.session.commit()
        #if a song(s) exists in the database, check to see if there is a match with generated song
        #and existing song(s) match. If there is no match, add generated song as new song in the database.
        #Both if statements check to make sure new songs that are added into database do not already
        #exist in the database.
            if len(
                    db.session.query(Song).filter(
                        Song.track_id == track["id"]).all()) <= 0:
                song = Song(
                    track_id=track["id"],
                    track_title=track["name"],
                    artist=[artist["name"] for artist in track["artists"]])
                db.session.add(song)
                db.session.commit()
            songplaylist = SongPlaylist(track_id=track["id"],
                                        playlist_id=playlist.playlist_id)
            db.session.add(songplaylist)
            db.session.commit()

    #reveal newly generated playlist on generate_playlist.html page based on stored session from user input above

    #create json format that in the form that Amplitude.js can read
    spotify_info_list = []
    for track in spotify_info["tracks"]:
        spotify_info_dict = {}
        millis = int(track["duration_ms"])
        minutes = millis / (1000 * 60)
        minutes = int(minutes)
        seconds = (millis % (1000 * 60)) / 1000
        seconds = int(seconds)
        if seconds == 0:
            seconds = "00"
        if seconds == 1:
            seconds = "01"
        if seconds == 2:
            seconds = "02"
        if seconds == 3:
            seconds = "03"
        if seconds == 4:
            seconds = "04"
        if seconds == 5:
            seconds = "05"
        if seconds == 6:
            seconds = "06"
        if seconds == 7:
            seconds = "07"
        if seconds == 8:
            seconds = "08"
        if seconds == 9:
            seconds = "09"

        track["duration_ms"] = f'{minutes}:{seconds}'

        spotify_info_dict["name"] = track["name"]
        spotify_info_dict["artist"] = [
            f' {artist["name"]}' for artist in track["artists"]
        ]
        spotify_info_dict["album"] = track["album"]["name"]
        spotify_info_dict["url"] = track["preview_url"]
        spotify_info_dict["cover_art_url"] = track["album"]["images"][0]["url"]

        spotify_info_list.append(spotify_info_dict)

    fin_spotify_dict = {}
    fin_spotify_dict["songs"] = spotify_info_list

    return render_template("amplitude_generate_playlist.html",
                           spotify_info=spotify_info,
                           fin_spotify_dict=fin_spotify_dict)