-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
112 lines (90 loc) · 3.62 KB
/
api.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
import requests
import urllib.parse
from flask import Flask, redirect, jsonify, request, session
from datetime import datetime
import time
import spotifyManager
from dotenv import load_dotenv, find_dotenv
import os
app = Flask(__name__)
app.secret_key = '123456789'
load_dotenv(find_dotenv())
CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
REDIRECT_URI = 'http://localhost:5000/callback'
AUTH_URL = 'https://accounts.spotify.com/authorize'
TOKEN_URL = 'https://accounts.spotify.com/api/token'
API_BASE_URL = 'https://api.spotify.com/v1/'
@app.route('/')
def index():
return "Please sign into your spotify account <a href ='/login'>Login with Spotify</a>"
@app.route('/login')
def login():
scope = 'user-read-currently-playing'
params = {
'client_id': CLIENT_ID,
'response_type': 'code',
'scope': scope,
'redirect_uri': REDIRECT_URI
}
auth_url = f'{AUTH_URL}?{urllib.parse.urlencode(params)}'
return redirect(auth_url)
@app.route('/callback')
def callback():
if 'error' in request.args:
return jsonify({"error": request.args['error']})
if 'code' in request.args:
req_body = {
'code': request.args['code'],
'grant_type': 'authorization_code',
'redirect_uri': REDIRECT_URI,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET
}
response = requests.post(TOKEN_URL, data=req_body)
token_info = response.json()
session['access_token'] = token_info['access_token'] #uses to authorize access
session['refresh_token'] = token_info['refresh_token'] #refreshes the access token
session['expires_at'] = datetime.now().timestamp() + token_info['expires_in'] #number of seconds before access tokens expires
return "<a href ='/check-ads'>click here to start the service</a>"
@app.route('/check-ads')
def get_playlists():
if 'access_token' not in session:
return redirect('/login')
if datetime.now().timestamp() > session['expires_at']:
return redirect('/refresh-token')
headers = {
'Authorization': f"Bearer {session['access_token']}",
}
previous_track = ""
while True:
response = requests.get(API_BASE_URL + 'me/player/currently-playing', headers=headers)
current_song = response.json()
if current_song['currently_playing_type'] == "ad":
spotifyManager.restartSpotify()
previous_track = ""
time.sleep(3)
continue
if current_song['item']['name'] != previous_track:
pause = current_song['item']['duration_ms'] - current_song['progress_ms']
time.sleep(pause/1000 - 5)
previous_track = current_song['item']['name']
@app.route('/refresh-token')
def refresh_token():
if 'access_token' not in session:
return redirect('/login')
if datetime.now().timestamp() > session['expires_at']:
req_body = {
'grant_type': 'refresh_token',
'refresh_token': session['refresh_token'],
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET
}
response = requests.post(TOKEN_URL, data = req_body)
new_token_info = response.json()
session['access_token'] = new_token_info['access_token']
session['expires_at'] = datetime.now().timestamp() + new_token_info['expires_in']
return redirect('/refresh-token')
return redirect('/check-ads')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)