-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
186 lines (137 loc) · 6.23 KB
/
server.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
#from client import spotipy as S
from flask import Flask, request, render_template, jsonify, redirect, url_for, send_from_directory, send_file
import json, base64, sys, requests
# Spotify API wrapper, documentation here: http://spotipy.readthedocs.io/en/latest/
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from werkzeug.utils import secure_filename
from spotipy.oauth2 import SpotifyClientCredentials
# Authenticate with Spotify using the Client Credentials flow
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
UPLOAD_FOLDER = '/app/images'
GOOGLE_API_KEY = "AIzaSyDShfsGYS3OsyrP46Ea-Lpj_tOvN8fpiVc"
GOOGLE_CLOUD_VISION_URL = "https://vision.googleapis.com/v1/images:annotate"
# Authenticate with Spotify using the Client Credentials flow
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
resulttrack ="global"
app = Flask(__name__, static_folder='public', template_folder='views')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/')
def homepage():
# print(A)
return render_template('index.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/getimage", methods=['GET'])
def getimage():
filename = '/app/images/input_image' + ".jpg"
return send_file(filename)
@app.route("/clearCache", methods=['GET'])
def clearCache():
os.remove("/app/images/input_image.jpg")
print("our track is " + resulttrack)
return resulttrack
@app.route("/upload", methods=['POST'])
def upload():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
print(request.form['token'])
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
ext = filename.split(".")[1]
file.save(os.path.join(app.config['UPLOAD_FOLDER'], "input_image" + "." + "jpg"))
tempfile = os.path.join(app.config['UPLOAD_FOLDER'], "input_image" + "." + "jpg")
# imagedataFile = open('/app/public/imagedataFile.txt','w')
# imagedataFile.write(tempfile)
# imagedataFile.close()
img = open(tempfile,'rb') # reading file from glitch's local
bitimage = base64.b64encode(img.read())
img.close()
req_data = {
"requests":[
{
"image":{
"content":bitimage
},
"features":[
{
"type":"LABEL_DETECTION",
"maxResults":10
}
]
}
]
}
rt = requests.post("%s?key=%s" % (GOOGLE_CLOUD_VISION_URL, GOOGLE_API_KEY), json.dumps(req_data), headers={'content-type': 'application/json'})
# print(r.text)
r = rt.json()
for i in range(len(r["responses"][0]["labelAnnotations"])):
keyword = r["responses"][0]["labelAnnotations"][i]["description"]
result = sp.search(keyword, limit = 1, type='playlist')
if(len(result["playlists"]["items"]) >0):
print(result["playlists"]["items"][0]["name"].encode('utf-8').strip())
if ( i < 1):
resulttrack = result["playlists"]["items"][0]["name"].encode('utf-8').strip()
requestBody = {
"context_uri": result["playlists"]["items"][0]["uri"].encode('utf-8').strip()
}
print(requestBody)
token = request.form['token']
auth = {"Authorization": "Bearer {0}".format(token)}
auth["Content-Type"] = "application/json"
print(auth)
req = requests.put("https://api.spotify.com/v1/me/player/play", data=json.dumps(requestBody), headers = auth )
print(req.text)
return render_template("show-snapmusic.html")
if __name__ == '__main__':
app.run()
################FLASK SPOTIPY#############
# import os
# from flask import Flask, request, render_template, jsonify
# # Spotify API wrapper, documentation here: http://spotipy.readthedocs.io/en/latest/
# import spotipy
# from spotipy.oauth2 import SpotifyClientCredentials
# # Authenticate with Spotify using the Client Credentials flow
# client_credentials_manager = SpotifyClientCredentials()
# sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# app = Flask(__name__, static_folder='public', template_folder='views')
# @app.route('/')
# def homepage():
# # Displays homepage
# return render_template('index.html')
# name = fid_dict(predction1)
# results = sp.search(q='track:' + name, type='track')
# print results
# @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)
# # Return the list of new releases
# return jsonify(new_releases)
# if __name__ == '__main__':
# app.run()