2019-07-29 11:44:10 +01:00
|
|
|
from flask import Flask, render_template, redirect, request, session, flash, url_for
|
2019-07-26 11:05:27 +01:00
|
|
|
from google.cloud import firestore
|
2019-07-26 14:18:32 +01:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from base64 import b64encode
|
|
|
|
|
2019-07-26 11:05:27 +01:00
|
|
|
import os
|
|
|
|
import urllib
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
from spotify.auth import auth_blueprint
|
|
|
|
from spotify.api import api_blueprint
|
|
|
|
|
2019-07-26 11:05:27 +01:00
|
|
|
# Project ID is determined by the GCLOUD_PROJECT environment variable
|
|
|
|
db = firestore.Client()
|
|
|
|
|
|
|
|
app = Flask(__name__, static_folder=os.path.join(os.path.dirname(__file__), '..', 'build'), template_folder="templates")
|
2019-07-29 11:44:10 +01:00
|
|
|
app.secret_key = db.collection(u'spotify').document(u'config').get().to_dict()['secret_key']
|
|
|
|
app.register_blueprint(auth_blueprint, url_prefix='/auth')
|
|
|
|
app.register_blueprint(api_blueprint, url_prefix='/api')
|
2019-07-26 11:05:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
2019-07-29 11:44:10 +01:00
|
|
|
def index():
|
2019-07-26 11:05:27 +01:00
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
@app.route('/spotify/auth')
|
2019-07-26 11:05:27 +01:00
|
|
|
def auth():
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-07-26 11:05:27 +01:00
|
|
|
client_id = db.document('key/spotify').get().to_dict()['clientid']
|
|
|
|
params = urllib.parse.urlencode(
|
|
|
|
{
|
|
|
|
'client_id': client_id,
|
|
|
|
'response_type': 'code',
|
|
|
|
'scope': 'playlist-modify-public playlist-modify-private playlist-read-private',
|
2019-07-26 14:18:32 +01:00
|
|
|
'redirect_uri': 'https://spotify.sarsoo.xyz/token'
|
2019-07-26 11:05:27 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return redirect(urllib.parse.urlunparse(['https', 'accounts.spotify.com', 'authorize', '', params, '']))
|
|
|
|
|
|
|
|
|
2019-07-26 14:18:32 +01:00
|
|
|
@app.route('/token')
|
|
|
|
def token():
|
|
|
|
|
|
|
|
code = request.args.get('code', None)
|
|
|
|
if code is None:
|
|
|
|
error = request.args.get('error', None)
|
2019-07-29 11:44:10 +01:00
|
|
|
print('error')
|
2019-07-26 14:18:32 +01:00
|
|
|
else:
|
|
|
|
app_credentials = db.document('key/spotify').get().to_dict()
|
|
|
|
|
|
|
|
idsecret = b64encode(bytes(app_credentials['clientid'] + ':' + app_credentials['clientsecret'], "utf-8")).decode("ascii")
|
|
|
|
headers = {'Authorization': 'Basic %s' % idsecret}
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
'code': code,
|
|
|
|
'redirect_uri': 'https://spotify.sarsoo.xyz/token'
|
|
|
|
}
|
|
|
|
|
|
|
|
req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
resp = req.json()
|
|
|
|
# print(str(req.status_code) + str(resp))
|
|
|
|
|
|
|
|
# if 200 <= req.status_code < 300:
|
2019-07-26 14:18:32 +01:00
|
|
|
|
|
|
|
return redirect('/app')
|
|
|
|
|
|
|
|
|
2019-07-30 16:25:01 +01:00
|
|
|
@app.route('/app', defaults={'path': ''})
|
|
|
|
@app.route('/app/<path:path>')
|
|
|
|
def app_route(path):
|
2019-07-29 11:44:10 +01:00
|
|
|
|
|
|
|
if 'username' not in session:
|
|
|
|
flash('please log in')
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2019-07-26 11:05:27 +01:00
|
|
|
return render_template('app.html')
|
|
|
|
|
|
|
|
# [END gae_python37_app]
|