def song(): if 'uid' not in session: print("reached /song with no session['uid']; return to index") return redirect(url_for('index')) uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] if not request.args.get('sid'): return "No sid passed" sid = request.args['sid'] cursor = qry.song_details(g.conn, sid) r = cursor.first() if not r: return "Unrecognized sid" song_details = dict(r) cursor.close() song_details['minutes'] = int(floor(song_details['length'] / 1000 / 60)) song_details['seconds'] = (song_details['length'] - song_details['minutes'] * 1000 * 60) / 1000 song_details['seconds'] = int(round(song_details['seconds'])) cursor = qry.liked_songs(g.conn, uid) likes = set() for r in cursor: likes.add(r['sid']) cursor.close() liked = False if sid in likes: liked = True context = dict(user=name, song_details=song_details, liked=liked) return render_template('song-details.html', **context)
def add_song_to_playlist(): if 'uid' not in session: print("reached /add_song_to_playlist with no session['uid']; return to index") return redirect(url_for('index')) uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] pid = request.args['pid'] cursor = qry.songsinpl(g.conn, pid) songsinpl = set() for r in cursor: songsinpl.add(r['sid']) cursor.close() cursor = qry.liked_songs(g.conn, uid) likes = {} for r in cursor: d = dict(r) if d['sid'] in songsinpl: d['liked'] = True else: d['liked'] = False likes[d['sid']] = d cursor.close() context = dict(user=name, likes=likes, pid=pid) return render_template('addsongs.html', **context)
def library(): if 'uid' not in session: print("reached /library with no session['uid']; return to index") return redirect(url_for('index')) uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] cursor = qry.liked_songs(g.conn, uid) likes = [] for r in cursor: likes.append(r) cursor.close() context = dict(user=name, likes=likes) return render_template('library.html', **context)
def artist(): if 'uid' not in session: print("reached /artist with no session['uid']; return to index") return redirect(url_for('index')) uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] if not request.args.get('aid'): return "No aid passed" aid = request.args['aid'] cursor = qry.aname(g.conn, aid) r = cursor.first() if not r: return "Unrecognized aid" aname = r['aname'] cursor.close() cursor = qry.publish(g.conn, aid) albums = [] for r in cursor: d = {r['albumid']: r} albums.append(d) cursor.close() songs_by_album = {} for d in albums: for albumid, r in d.items(): cursor = qry.list_album_songs(g.conn, albumid) songs = [] for row in cursor: songs.append(row) cursor.close() songs_by_album[albumid] = songs cursor = qry.liked_songs(g.conn, uid) likes = set() for r in cursor: likes.add(r['sid']) cursor.close() context = dict(aid=aid, aname=aname, albums=albums, songs_by_album=songs_by_album, user=name, likes=likes) return render_template('artist.html', **context)
def playlist(): uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] if not request.args.get('pid'): return "No pid passed" pid = request.args['pid'] cid = request.args['cid'] cursor = qry.playlist_songs(g.conn, pid, cid) songs1 = {} for r in cursor: songs1[r['sid']] = r cursor.close() plname = qry.playlist_name(g.conn, pid, cid).first()['pname'] cursor = qry.liked_songs(g.conn, uid) likes = set() for r in cursor: likes.add(r['sid']) cursor.close() context = dict(user=name, songs1=songs1, pname=plname, likes=likes, pid=pid, cid=cid, uid=uid) return render_template('playlist.html', **context)
def songs(): if 'uid' not in session: print("reached /songs with no session['uid']; return to index") return redirect(url_for('index')) uid = session['uid'] name = qry.uname(g.conn, uid).first()['uname'] cursor = qry.liked_songs(g.conn, uid) likes = set() for r in cursor: likes.add(r['sid']) cursor.close() fname = request.form['field'] if fname == 'sname': cursor = qry.search_songs(g.conn, request.form['song']) elif fname == 'albumname': cursor = qry.search_songs_by_album(g.conn, request.form['song']) elif fname == 'aname': cursor = qry.search_songs_by_artist(g.conn, request.form['song']) elif fname == 'gname': cursor = qry.search_songs_by_genre(g.conn, request.form['song']) else: return redirect(url_for('index')) songs = {} for r in cursor: d = dict(r) if d['sid'] in likes: d['liked'] = True else: d['liked'] = False songs[d['sid']] = d cursor.close() context = dict(user=name, songs=songs) return render_template("songs.html", **context)