def edit(): """Edit user information.""" if 'username' not in flask.session: return flask.redirect(flask.url_for('login')) if flask.request.method == 'POST': current_username = flask.session['username'] input_name = flask.request.form['fullname'] if input_name is not None: table = get_db().cursor().execute( ''' UPDATE users SET fullname=? WHERE username=?''', ( input_name, current_username, )) return flask.redirect(flask.url_for('edit')) context = {} table2 = get_db().cursor().execute( ''' SELECT * FROM users WHERE username=?''', (flask.session['username'], )) profile_pic = table2.fetchone() context['logname'] = flask.session["username"] context['fullname'] = profile_pic['fullname'] return flask.render_template('edit.html', **context)
def password(): """Save new password from user.""" if 'username' not in flask.session: return flask.redirect(flask.url_for('login')) if flask.request.method == 'POST': input_password = flask.request.form['password'] input_new = flask.request.form['new_password1'] input_new_2 = flask.request.form['new_password2'] if not check_login(flask.session['username'], input_password): flask.abort(403) if input_new != input_new_2: flask.abort(401) if not check_password(input_new): flask.abort(400) hashed_password_new = hash_function(input_new) get_db().cursor().execute( ''' UPDATE users SET password=? WHERE username=?''', ( hashed_password_new, flask.session['username'], )) return flask.redirect(flask.url_for('edit')) context = {} context["logname"] = flask.session["username"] return flask.render_template('password.html', **context)
def create_user(): """Create a User.""" if 'username' in flask.session: return flask.redirect(flask.url_for('home')) if flask.request.method == 'POST': input_username = flask.request.form['username'] input_password = flask.request.form['password'] input_name = flask.request.form['fullname'] # Abort Checks if not check_username(input_username): flask.abort(409) if not check_password(input_password): flask.ab hashed_password = hash_function(input_password) # Insert into database get_db().cursor().execute( ''' INSERT INTO users (username, fullname, password) VALUES (?, ?, ?) ''', (input_username, input_name, hashed_password)) flask.session['username'] = input_username return flask.redirect(flask.url_for('home')) return flask.render_template('create_user.html')
def spotify_playlist(platformUsername, ourPlaylistID, platformPlayListID, playlistTitle): sp = spotipy.Spotify(auth=token) sp.trace = False tracks = get_db().cursor().execute( '''SELECT * FROM playListSongs WHERE username=? AND playlistID=?''', (platformUsername, ourPlaylistID)).fetchall() user_playlist_create(platformUsername, playlistTitle, public=False, description="") for i in tracks: songID = tracks['songID'] song = get_db().cursor().execute( '''SELECT * FROM songs WHERE songID=?''', (songID)).fetchone() track = search(q=song['songname'] + song['artist'] + sond['album'], type='track') user_playlist_add_tracks(platformUsername, platformPlayListID, track, position=i)
def create_playlist(username, id_requested, initial_platform, destination_platform): # grab playlist from the requested username, id, initial platform and destination platform playlist = get_db().cursor().execute( '''SELECT * FROM playlists WHERE username=? AND id=? AND platform=?''', ( username, id_requested, initial_platform, )).fetchone() platformUsername = playlist['platformUsername'] ourPlaylistID = playlist['playlistID'] platformPlayListID = playlist['id'] playlistTitle = playlist['title'] if (destination_platform == 's'): scope = "playlist-modify-private" token = util.prompt_for_user_token( platformUsername, scope, client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI_LOCAL) if token: spotify_playlist(platformUsername, ourPlaylistID, platformPlayListID, playlistTitle) else: print("Can't get token for", username) else: youtube_playlist(platformUsername, ourPlaylistID, platformPlayListID, playlistTitle)
def check_username(input_username): """Check if username already exists.""" table = get_db().cursor().execute( ''' SELECT * FROM users WHERE username=?''', (input_username, )) result = table.fetchone() if result is None: return True return False
def spotify_playlist(input_username): # if __name__ == '__main__': # if len(sys.argv) > 1: # username = sys.argv[1] # else: # print("Whoops, need your username!") # print("usage: python user_playlists_contents.py [username]") # sys.exit() username = input_username # change after authorizing web app to spotify scope = 'playlist-read-private' # token = util.prompt_for_user_token(username,scope, client_id='03b0cc705dd74be1a04dc7b3ae8751a5',client_secret='48ff64efc61a49efb1358820d846aa0e',redirect_uri='http://localhost:5000/getPlaylists/') credentials = oauth2.SpotifyClientCredentials( client_id=SPOTIPY_SERVER_CLIENT_ID, client_secret=SPOTIPY_SERVER_CLIENT_SECRET) token = credentials.get_access_token() spotify = spotipy.Spotify(auth=token) fromPlatform = 'Spotify' if token: sp = spotipy.Spotify(auth=token) #res = requests.get("https://api.spotify.com/v1/me" -H "Authorization: Bearer {token}") #userID = res['id'] #sp.trace = False playlists = sp.user_playlists(input_username) for playlist in playlists: tracks = playlist['tracks'] for track in tracks: get_db().cursor().execute(''' INSERT INTO songs (songName, artist, album) VALUES (?, ?, ?) ''' ( track['name'], track['artist'][0]['name'], track['album']['name'])) get_db().cursor().execute(''' INSERT INTO playlists (title, id, url, platform, platformUsername) VALUES (?, ?, ?, ?, ?, ?) ''' ( playlist['name'], playlist['id'], playlist['url'], fromPlatform, playlist['owner'])) else: print("Can't get token for", input_username)
def delete(): """Delete logged in user.""" if 'username' not in flask.session: return flask.redirect(flask.url_for('login')) # Delete Account if flask.request.method == 'POST': # Delete Rows get_db().cursor().execute( ''' DELETE FROM users WHERE username=? ''', (flask.session['username'], )) flask.session.pop('username', None) return flask.redirect(flask.url_for('create')) context = {} context["logname"] = flask.session["username"] return flask.render_template('delete.html', **context)
def check_login(username, input_password): """Check if login is valid.""" table = get_db().cursor().execute( ''' SELECT * FROM users WHERE username=?''', (username, )) result = table.fetchone() if result is None: return False hashed_password = result['password'] algorithm = hashed_password.split('$')[0] salt = hashed_password.split('$')[1] password_salted = salt + input_password mal = hashlib.new(algorithm) mal.update(password_salted.encode('utf-8')) check_hash = mal.hexdigest() if check_hash == hashed_password.split('$')[2]: return True return False
def home(): if 'username' not in flask.session: return flask.redirect(flask.url_for('login')) input_username = flask.session['username'] if flask.request.method == 'POST': if 'Spotify' in flask.request.form: username_in = flask.request.form['Spotify Username'] read_playlist(username_in, 's') return flask.redirect(flask.url_for('home')) elif 'Youtube' in flask.request.form: read_playlist(flask.session['username'], 'y') return flask.redirect(flask.url_for('home')) else: playlistid = -1 to_platform = ' ' if 'Convert to Spotify' in flask.request.form: playlistid = flask.request.form['Convert to Spotify'] to_platform = 's' else: playlistid = flask.request.form['Convert to Youtube'] to_platform = 'y' table = get_db().cursor().execute( ''' SELECT * FROM playlists WHERE username=? AND id=?''', ( flask.session['username'], playlistid, )).fetchone() initial_platform = table['platform'] create_playlist(flask.session['username'], playlistid, initial_platform, to_platform) playlists = get_db().cursor().execute( ''' SELECT * FROM playlists WHERE username=?''', (flask.session['username'], )).fetchall() context = {} context['playlists'] = [] for playlist in playlists: temp = {} temp['playlistID'] = playlist['playlistID'] temp['title'] = playlist['title'] songs = get_db().cursor().execute( ''' SELECT * FROM playListSongs WHERE username=? AND databasePlaylistID=?''', ( flask.session['username'], playlist['playlistID'], )).fetchall() temp['playlistID'] = playlist['playlistID'] songTemp = [] for song in songs: specific_song = get_db().cursor().execute( ''' SELECT * FROM songs WHERE songID=? ''', (song['songID'], )).fetchone() songTemp2 = {} songTemp2['title'] = specific_song['title'] songTemp2['artist'] = specific_song['artist'] songTemp.append(songTemp2) temp['songs'] = songTemp context['playlists'].append(temp) return flask.render_template("createPlaylist.html", **context)