Skip to content

Instantly share code, notes, and snippets.

@BtbN
Last active June 4, 2020 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BtbN/3e07e60a6160ad772dddcf3f3f93fd40 to your computer and use it in GitHub Desktop.
Save BtbN/3e07e60a6160ad772dddcf3f3f93fd40 to your computer and use it in GitHub Desktop.
get_cur_song.py
#!/usr/bin/env python3
import os
import sys
import spotipy
import spotipy.util as util
import webbrowser
from flask import Flask
def fake_open(url):
raise webbrowser.Error("")
webbrowser.open = fake_open
os.chdir(os.path.dirname(os.path.abspath(__file__)))
scope = 'user-library-read user-read-currently-playing user-read-playback-state'
client_id = 'SETME'
client_secret = 'SETMETOO'
redirect_uri = 'https://set.de/me'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
username = "default"
token = util.prompt_for_user_token(username, scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
if not token:
print("No Token :(")
sys.exit(-1)
application = app = Flask(__name__)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def get_song(path):
token = util.prompt_for_user_token(username, scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(auth=token)
results = sp.currently_playing()
if not results:
return "Not playing"
artists = ""
for art in results['item']['artists']:
if artists:
artists = artists + ", "
artists = artists + art['name']
album = results['item']['album']['name']
song = results['item']['name']
song_url = results['item']['external_urls']['spotify']
playlist = "Unknown"
if results['context'] and results['context']['type'] == 'playlist':
playlist = results['context']['external_urls']['spotify']
return "%s - %s - %s | Song: %s | Playlist: %s" % (artists, album, song, song_url, playlist)
if __name__ == "__main__":
print(get_song("/"))
[uwsgi]
plugins=python37
venv=venv
socket = uwsgi.sock
chmod-socket = 700
chdir = /var/www/htscripts/spoti_song
wsgi-file = get_cur_song.py
master = true
processes = 1
threads = 2
single-interpreter = true
enable-threads = true
stats = 127.0.0.1:9191
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment