-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
134 lines (117 loc) · 5.82 KB
/
server.py
File metadata and controls
134 lines (117 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
# python 3
import os
from flask import Flask, request, render_template, jsonify
import json
# Spotify API wrapper, documentation here: http://spotipy.readthedocs.io/en/latest/
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
from spotipy import oauth2
# UDF
from utility import *
from load_creds import *
#------------------------------------
# config
# Authenticate with Spotify using the Client Credentials flow
try:
SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET = get_spotify_client_id_secret()
except:
SPOTIPY_CLIENT_ID = os.environ['SPOTIPY_CLIENT_ID']
SPOTIPY_CLIENT_SECRET = os.environ['SPOTIPY_CLIENT_SECRET']
else:
print (' No API key , please set up via : ')
print (' https://developer.spotify.com/dashboard/applications ')
client_credentials_manager = SpotifyClientCredentials(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
app = Flask(__name__, static_folder='templates', template_folder='templates')
#------------------------------------
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
@app.route('/')
def homepage():
# Displays homepage
# return access_token when load the landing page
access_token = generate_token(SPOTIPY_CLIENT_ID,SPOTIPY_CLIENT_SECRET)
print ('access_token : ', access_token)
return render_template('artist_recommend.html')
@app.route('/slide_recommend')
def slide_recommend_page():
# hard code here, will have to make it dynamic
artist_ = 'HONNE'
artist_id = get_artist(artist_)['id']
recommend_ = sp.recommendations(seed_artists = [artist_id],seed_genres=['dubstep','deep-house','edm'],country='FR',limit=5)
pic_url = [ recommend_['tracks'][i]['album']['images'][0]['url'] for i in range(len(recommend_['tracks']))]
artist_name = [ recommend_['tracks'][i]['album']['artists'][0]['name'] for i in range(len(recommend_['tracks']))]
album_name = [ recommend_['tracks'][i]['name'] for i in range(len(recommend_['tracks']))]
preview_url = [ recommend_['tracks'][i]['preview_url'] for i in range(len(recommend_['tracks']))]
data_ = pd.DataFrame({'pic_url': pic_url,'artist_name':artist_name, 'album_name': album_name,'preview_url':preview_url })
print ('data_ : ', data_.head(5))
print (' ********* recommend_ ********* : ' , recommend_)
print (' ***** pic_url : ***** ', (pic_url))
return render_template('slide_recommend.html',data=data_ )
#return render_template('slide_recommend.html',data=jsonify(recommend_['tracks']))
@app.route('/dev')
def dev_page():
recommend_ = sp.recommendations(seed_artists = [get_artist('common')['id']],seed_genres=['dubstep','deep-house','edm'],country='FR',limit=5)
pic_url = [ recommend_['tracks'][i]['album']['images'][0]['url'] for i in range(len(recommend_['tracks']))]
artist_name = [ recommend_['tracks'][i]['album']['artists'][0]['name'] for i in range(len(recommend_['tracks']))]
album_name = [ recommend_['tracks'][i]['name'] for i in range(len(recommend_['tracks']))]
preview_url = [ recommend_['tracks'][i]['preview_url'] for i in range(len(recommend_['tracks']))]
data_ = pd.DataFrame({'pic_url': pic_url,'artist_name':artist_name, 'album_name': album_name,'preview_url':preview_url })
return render_template('dev.html',data=data_ )
@app.route('/new_releases', methods=['GET'])
def new_releases():
# Use the country from the query parameters, if provided
if 'country' in request.args:
country = request.args['country']
else:
country = 'SE'
# Send request to the Spotify API
new_releases = sp.new_releases(country=country, limit=20, offset=0)
print ('new_releases : ', new_releases)
# Return the list of new releases
return jsonify(new_releases)
@app.route('/recommend', methods=['GET','POST'])
def recommend():
print (' request.args : ' , request.args)
print (' request : ' , request)
# Use the country from the query parameters, if provided
try:
artist_=request.args['artist']
artist_id = get_artist(artist_)['id']
except:
artist_='HONNE'
artist_id = get_artist(artist_)['id']
print ('artist_ : ', artist_)
print ('artist_id : ', artist_id)
"""
Send request to the Spotify API
https://spotipy.readthedocs.io/en/latest/
https://github.com/plamere/spotipy/blob/4c2c1d763a3653aa225c4af848409ec31286a6bf/spotipy/client.py#L797
recommend_ = sp.recommendations(seed_artists=None, seed_genres=None, seed_tracks=None, limit=20, country=None, **kwargs)
"""
recommend_ = sp.recommendations(seed_artists = [artist_id],seed_genres=['dubstep','deep-house','edm'],country='FR',limit=100)
#print ('recommendation : ', recommend_)
print ('type : ', type(jsonify(recommend_['tracks'])))
print ('artist_ : ', artist_)
data = request.get_json()
current_genre = request.__dict__['args']
current_genre_dict = request.__dict__['args'].to_dict(flat=False)
# in case user update current genre
#if len(fix_genre_dict(current_genre_dict)) > 0:
# recommend_ = sp.recommendations(seed_artists = [artist_id],seed_genres=fix_genre_dict(current_genre_dict),country='FR',limit=5)
#else:
# pass
print (' 1) current request_get_status : ', jsonify(request.get_json()) )
print (' 2) current request_get_json : ', (request.__dict__) )
print (' 3) current genres (ImmutableMultiDict) : ', current_genre)
print (' 4) current genres (dict) : ', current_genre_dict)
print (' 5) fixed current genres (dict) : ', fix_genre_dict(current_genre_dict))
return jsonify(recommend_['tracks'])
if __name__ == '__main__':
app.run(host='0.0.0.0',port=7777)