Beispiel #1
0
def show_top_40():
    """Display user's top 40 Spotify artists."""

    if 'spotify_token' not in session:
        if 'user' not in session:
            flash('Please login or register. 👋🏻')
            return redirect('/')
        else:
            flash('Please login to Spotify 🎧')
            return redirect('/get-top-40')
    else:
        access_token = session['spotify_token']

        # get user profile and artist info
        user_data = spotify.get_user_profile(access_token)
        response = spotify.get_top_artists(access_token)
        artists = spotify.format_artist_data(response)

        # add new user's info to db
        username = session['user']
        if helper.check_spotify_not_in_db(username):
            helper.add_user_db(access_token)
            helper.add_artist_db(access_token)
            helper.add_user_artist_link(access_token)

        return render_template("top-40.html",
                               artists=artists,
                               all_data=response,
                               user=user_data)
Beispiel #2
0
def add_user_artist_link(access_token):
    """Add user and artist link info to association table."""

    # get current user
    username = session['user']
    current_user_id = User.query.filter_by(username=username).first().user_id

    # get list of artist info
    # iterate over each elem which is a tuple
    # add user_id, artist_id, and created_at to db
    response = spotify.get_top_artists(access_token)
    artists = spotify.format_artist_data(response)

    for artist in artists:
        name = artist[1]
        artist_id = Artist.query.filter_by(name=name).first().artist_id

        # user artist link object
        link = UserArtistLink(user_id=current_user_id,
                              artist_id=artist_id,
                              created_at=datetime.now())

        db.session.add(link)

    db.session.commit()
Beispiel #3
0
def show_top_40():
    """Display user's top 40 Spotify artists."""

    access_token = session['access_token']
    user_data = spotify.get_user_profile(access_token)
    artists = spotify.get_top_artists(access_token)
    username = session['user']

    if db_helper.check_spotify_not_in_db(username):
        db_helper.add_user_spotify_db(access_token)
        db_helper.add_artist_db(access_token)
        db_helper.add_user_artist_link(access_token)

    return render_template("top-40.html", artists=artists, user=user_data)
Beispiel #4
0
def process_event_search():
    """Return event search results from Eventbrite."""

    city = request.form.get('city')
    distance = request.form.get('distance')

    access_token = session['access_token']
    artists = spotify.get_top_artists(access_token)
    results = eventbrite.search_batch_events(artists, city, distance)

    if results:
        db_helper.add_events_db(results)
        db_helper.add_user_event_link(results)
        return jsonify(results)
    else:
        return jsonify(results)
Beispiel #5
0
def add_user_artist_link(access_token):
    """Add user and artist link info to association table."""

    # get current user from session
    username = session['user']
    current_user_id = User.query.filter_by(username=username).first().user_id

    # get list of artist info from spotify response.json()
    artists = spotify.get_top_artists(access_token)

    for artist in artists:
        name = artist[1]
        artist_id = Artist.query.filter_by(name=name).first().artist_id

        # instantiate user artist link object
        link = UserArtistLink(user_id=current_user_id,
                              artist_id=artist_id,
                              created_at=datetime.now())

        db.session.add(link)

    db.session.commit()
Beispiel #6
0
def add_artist_db(access_token):
    """Add artist info to artists table after Spotify login."""

    # get artist info from spotify response.json()
    artists = spotify.get_top_artists(access_token)

    for artist in artists:
        artist_name = artist[1]
        artist_spotify_url = artist[2]
        artist_img = artist[3]

        # if artist already exists in db, skip and go to next one
        # if artist doesn't exist, add to db
        if Artist.query.filter_by(name=artist_name).first():
            continue
        else:
            artist = Artist(name=artist_name,
                            spotify_url=artist_spotify_url,
                            img=artist_img)
            db.session.add(artist)

    db.session.commit()