from flask import Flask, render_template, request, url_for, redirect import time import requests from urllib.parse import urlencode import webbrowser import base64 import json import os app = Flask(__name__) client_id = '1cb8bc27872c4bcaaad0e95f123b4f7d' client_secret = '9893dfb6d9eb43eebf082f9173ce937c' redirect_uri = 'http://127.0.0.1:8888/callback' encoded_creds = base64.b64encode(client_id.encode() + b':' + client_secret.encode()).decode("utf-8") headers = { "client_id": client_id, "response_type": "code", "redirect_uri": redirect_uri, "scope": "user-read-playback-state,user-modify-playback-state,user-library-read,user-library-modify" } token_headers = { "Authorization": "Basic " + encoded_creds, "Content-Type": "application/x-www-form-urlencoded" } song_info = { 'name': "None", 'artist': "None", 'album': "None", 'image': "None" } @app.route('/') def index(): global code if os.path.exists('code'): with open('code', 'r') as file: code = file.read() return redirect(url_for('webapp')) else: return redirect("https://accounts.spotify.com/authorize?" + urlencode(headers)) # return render_template('index.html') # @app.route('/update') # def update(): # # Generate new information # new_info = generate_new_info() # # Wait for 1 second # time.sleep(1) # return new_info @app.route('/webapp', methods=['GET', 'POST']) def webapp(): global access_token # NEED TO FIND A BETTER WAY THAT DOESN'T INVOLVE A TRY EXCEPT BLOCK if 'code' not in globals() or not code: return redirect(url_for('index')) token_data = { "grant_type": "authorization_code", "code": code, "redirect_uri": redirect_uri } access_object = requests.post("https://accounts.spotify.com/api/token", data=token_data, headers=token_headers) if "error" in access_object.json(): if os.path.exists('code'): os.remove('code') return redirect(url_for('index')) access_token = access_object.json()["access_token"] return render_template('webapp.html', song_info=song_info) @app.route('/callback', methods=['GET']) def callback(): global code code = request.args.get('code') with open('code', 'w') as file: file.write(code) return redirect(url_for('webapp')) @app.route('/appdata') def appdata(): global access_token user_headers = { "Authorization": "Bearer " + access_token, "Content-Type": "application/json" } currently_playing = requests.get("https://api.spotify.com/v1/me/player/currently-playing", headers=user_headers) if currently_playing.json()["is_playing"] == True: return { 'id': currently_playing.json()["item"]["id"], 'name': currently_playing.json()["item"]["name"], 'artist': currently_playing.json()["item"]["artists"][0]["name"], 'album': currently_playing.json()["item"]["album"]["name"], 'image': currently_playing.json()["item"]["album"]["images"][0]["url"], 'is_playing': currently_playing.json()["is_playing"], 'progress_ms': currently_playing.json()["progress_ms"], 'duration_ms': currently_playing.json()["item"]["duration_ms"], 'is_liked': requests.get("https://api.spotify.com/v1/me/tracks/contains?ids=" + currently_playing.json()["item"]["id"], headers=user_headers).json()[0] } elif currently_playing.json()["is_playing"] == False: return { 'name': "Not Playing", 'artist': "Not Playing", 'album': "Not Playing", 'image': "Not Playing" } else: return { 'name': "Error", 'artist': "Error", 'album': "Error", 'image': "Error" } @app.route('/control', methods=['POST']) def control(): global access_token user_headers = { "Authorization": "Bearer " + access_token, "Content-Type": "application/json" } if request.form.get('command') == "pause": requests.put("https://api.spotify.com/v1/me/player/pause", headers=user_headers) elif request.form.get('command') == "play": requests.put("https://api.spotify.com/v1/me/player/play", headers=user_headers) elif request.form.get('command') == "next": requests.post("https://api.spotify.com/v1/me/player/next", headers=user_headers) elif request.form.get('command') == "previous": requests.post("https://api.spotify.com/v1/me/player/previous", headers=user_headers) elif request.form.get('command') == "restart": requests.put("https://api.spotify.com/v1/me/player/seek?position_ms=0", headers=user_headers) elif request.form.get('command') == "like": requests.put("https://api.spotify.com/v1/me/tracks?ids=" + request.form.get('id'), headers=user_headers) elif request.form.get('command') == "unlike": requests.delete("https://api.spotify.com/v1/me/tracks?ids=" + request.form.get('id'), headers=user_headers) print(request.form.get('command')) return ('', 204) if __name__ == '__main__': app.run(port=8888, debug=True)