main
This commit is contained in:
parent
904be29c8f
commit
88271a4b56
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.env
|
||||||
|
cache.json
|
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@ -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"]
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
python-dotenv
|
||||||
|
flask
|
||||||
|
requests
|
84
src/main.py
Normal file
84
src/main.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user