From 88271a4b5681290804820b229c5ff0476cf8d621 Mon Sep 17 00:00:00 2001 From: Alfie King Date: Sun, 1 Sep 2024 18:05:11 +0100 Subject: [PATCH] main --- .gitignore | 2 ++ Dockerfile | 19 +++++++++++ requirements.txt | 3 ++ src/main.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 src/main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58b6652 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +cache.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fc2c782 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Set the base image +FROM python:alpine3.19 + +# Copy the files to the container +COPY src /app +COPY requirements.txt /app +#COPY .env /app + +# Set the working directory +WORKDIR /app + +# Install dependencies +RUN pip install -r requirements.txt + +# Expose the port +EXPOSE 5000 + +# Run the application +CMD ["python", "main.py"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..07acf07 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-dotenv +flask +requests \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..096ce42 --- /dev/null +++ b/src/main.py @@ -0,0 +1,84 @@ +from flask import Flask, request, jsonify, render_template +import requests, os, dotenv, json, base64, time + +# Initialize Flask app + +app = Flask(__name__) + +if os.path.exists('.env'): + dotenv.load_dotenv('.env') + +# Helper functions + +def querystring(params:dict): + return '&'.join([f'{key}={value}' for key, value in params.items()]) + +def get_cache(): + if os.path.exists('cache.json'): + with open('cache.json', 'r') as f: + return json.load(f) + return {} + +def save_cache(cache): + with open('cache.json', 'w') as f: + json.dump(cache, f) + +# Get access token + +def get_auth(): + cache = get_cache() + if 'access_token' in cache: return True + query = querystring({ + 'response_type': 'code', + 'client_id': os.getenv('CLIENT_ID'), + 'scope': 'user-read-currently-playing', + 'redirect_uri': os.getenv('REDIRECT_URI'), + 'show_dialog': 'true' + }) + redirect = requests.get(f"https://accounts.spotify.com/authorize?{query}") + print(redirect.url) + code = input('Enter code: ') + data = requests.post('https://accounts.spotify.com/api/token', data={ + 'grant_type': 'authorization_code', + 'code': code, + 'redirect_uri': os.getenv('REDIRECT_URI'), + }, headers={ + 'content-type': 'application/x-www-form-urlencoded', + 'Authorization': 'Basic ' + base64.b64encode(f"{os.getenv('CLIENT_ID')}:{os.getenv('CLIENT_SECRET')}".encode()).decode() + }).json() + save_cache(data) + return True + +def update_token(): + cache = get_cache() + data = requests.post('https://accounts.spotify.com/api/token', data={ + 'grant_type': 'refresh_token', + 'refresh_token': cache['refresh_token'] + }, headers={ + 'content-type': 'application/x-www-form-urlencoded', + 'Authorization': 'Basic ' + base64.b64encode(f"{os.getenv('CLIENT_ID')}:{os.getenv('CLIENT_SECRET')}".encode()).decode() + }).json() + cache.update(data) + save_cache(cache) + return True + +# Routes + +@app.route('/api/nowplaying', methods=['GET']) +def nowplaying(): + cache = get_cache() + data = requests.get('https://api.spotify.com/v1/me/player/currently-playing', headers={ + 'Authorization': f"Bearer {cache['access_token']}" + }) + if data.status_code == 204: + return jsonify({}) + if data.json().get('error'): + update_token() + return nowplaying() + return jsonify(data.json()) + +# Run app + +if __name__ == '__main__': + get_auth() + app.run(host='0.0.0.0', port=5000) \ No newline at end of file