introduced icons to replace buttons, loading indicators, cut down unessesary data traffic
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from flask import Flask, render_template, request, url_for, redirect
|
||||
from flask import Flask, render_template, request, url_for, redirect, send_from_directory
|
||||
import time
|
||||
import requests
|
||||
from urllib.parse import urlencode
|
||||
@@ -28,11 +28,11 @@ token_headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
|
||||
song_info = {
|
||||
'name': "None",
|
||||
'artist': "None",
|
||||
'album': "None",
|
||||
'image': "None"
|
||||
data = {
|
||||
'name': "Loading...",
|
||||
'artist': "",
|
||||
'album': "",
|
||||
'image': "https://cdn.pixabay.com/animation/2023/05/02/04/29/04-29-03-511_512.gif"
|
||||
}
|
||||
|
||||
@app.route('/')
|
||||
@@ -75,7 +75,7 @@ def webapp():
|
||||
return redirect(url_for('index'))
|
||||
access_token = access_object.json()["access_token"]
|
||||
|
||||
return render_template('webapp.html', song_info=song_info)
|
||||
return render_template('webapp.html', data=data)
|
||||
|
||||
@app.route('/callback', methods=['GET'])
|
||||
def callback():
|
||||
@@ -94,30 +94,33 @@ def appdata():
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
if currently_playing.content:
|
||||
if currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] == request.args.get('id'):
|
||||
return { 'progress_ms': currently_playing.json()["progress_ms"]
|
||||
}
|
||||
elif currently_playing.json()["is_playing"] is True and currently_playing.json()["item"]["id"] != request.args.get('id'):
|
||||
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"] is False:
|
||||
return { 'name': "Not Playing",
|
||||
'is_playing': False
|
||||
}
|
||||
else:
|
||||
return { 'name': "Error",
|
||||
'artist': "Error"
|
||||
}
|
||||
else:
|
||||
return { 'name': "Error",
|
||||
'artist': "Error",
|
||||
'album': "Error",
|
||||
'image': "Error"
|
||||
return { 'name': "Not Playing",
|
||||
'image': "https://cdn.psychologytoday.com/sites/default/files/styles/article-inline-half-caption/public/field_blog_entry_images/2022-02/pause.png",
|
||||
'is_playing': False
|
||||
}
|
||||
|
||||
@app.route('/control', methods=['POST'])
|
||||
@@ -130,8 +133,10 @@ def control():
|
||||
|
||||
if request.form.get('command') == "pause":
|
||||
requests.put("https://api.spotify.com/v1/me/player/pause", headers=user_headers)
|
||||
return { 'is_playing': False }
|
||||
elif request.form.get('command') == "play":
|
||||
requests.put("https://api.spotify.com/v1/me/player/play", headers=user_headers)
|
||||
return { 'is_playing': True }
|
||||
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":
|
||||
@@ -140,11 +145,19 @@ def control():
|
||||
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)
|
||||
return { 'is_liked': True }
|
||||
elif request.form.get('command') == "unlike":
|
||||
requests.delete("https://api.spotify.com/v1/me/tracks?ids=" + request.form.get('id'), headers=user_headers)
|
||||
return { 'is_liked': False }
|
||||
|
||||
print(request.form.get('command'))
|
||||
return ('', 204)
|
||||
|
||||
@app.route('/icons/<path:filename>')
|
||||
def icons(filename):
|
||||
return send_from_directory('icons', filename)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(port=8888, debug=True)
|
||||
Reference in New Issue
Block a user