Browse Source

Fix issue with threading

pull/3/head
Wesley Kerfoot 6 years ago
parent
commit
9a7afcba8e
  1. 30
      spotify_mpd_sync/msplaylist/authenticate.py
  2. 6
      spotify_mpd_sync/msplaylist/spotify.py

30
spotify_mpd_sync/msplaylist/authenticate.py

@ -1,27 +1,41 @@
# shows a user's playlists (need to be authenticated via oauth) # shows a user's playlists (need to be authenticated via oauth)
from __future__ import print_function import threading
import os
import spotipy.oauth2 as oauth2 import spotipy.oauth2 as oauth2
import spotipy import spotipy
import queue import queue
import threading
from bottle import route, run, response, request from bottle import route, run, response, request
from sys import exit
auth_token_queue = queue.Queue() auth_token_queue = queue.Queue()
event_queue = queue.Queue()
@route('/') @route('/')
def index(): def index():
auth_code = request.query.code auth_code = request.query.code
if auth_code: if auth_code:
print("putting auth code in queue")
auth_token_queue.put(auth_code) auth_token_queue.put(auth_code)
return "It worked! You may close this tab now" return "It worked! You may close this tab now"
return "Oops! Something went wrong. Please file a bug report" return "Oops! Something went wrong. Please file a bug report"
def wait_for_done(task):
server = threading.Thread(target=task)
server.daemon = True
server.start()
while True:
print("waiting for done token")
event = event_queue.get()
if event == "done":
print("Server being killed")
break
exit(1)
def run_server(): def run_server():
task = threading.Thread(target=lambda : run(host='localhost', port=8080)) threading.Thread(target= lambda: wait_for_done(lambda: run(host='localhost', port=8080))).start()
task.start() print("server started")
def prompt_for_user_token(username, scope=None, client_id = None, def prompt_for_user_token(username, scope=None, client_id = None,
client_secret = None, redirect_uri = None, cache_path = None): client_secret = None, redirect_uri = None, cache_path = None):
@ -74,6 +88,7 @@ def prompt_for_user_token(username, scope=None, client_id = None,
token_info = sp_oauth.get_cached_token() token_info = sp_oauth.get_cached_token()
if not token_info: if not token_info:
run_server()
auth_url = sp_oauth.get_authorize_url() auth_url = sp_oauth.get_authorize_url()
try: try:
import webbrowser import webbrowser
@ -83,7 +98,10 @@ def prompt_for_user_token(username, scope=None, client_id = None,
print("Please navigate here: %s" % auth_url) print("Please navigate here: %s" % auth_url)
response = "localhost:8080?code=%s" % auth_token_queue.get() response = "localhost:8080?code=%s" % auth_token_queue.get()
auth_token_queue.task_done() print("Got auth token!")
print(response)
print("putting done token")
event_queue.put("done")
code = sp_oauth.parse_response_code(response) code = sp_oauth.parse_response_code(response)
token_info = sp_oauth.get_access_token(code) token_info = sp_oauth.get_access_token(code)

6
spotify_mpd_sync/msplaylist/spotify.py

@ -1,4 +1,6 @@
#! /usr/bin/env python #! /usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import spotipy import spotipy
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
@ -10,11 +12,10 @@ from re import sub
from os import environ from os import environ
import spotipy.util as util import spotipy.util as util
from spotify_mpd_sync.msplaylist.authenticate import run_server, prompt_for_user_token from spotify_mpd_sync.msplaylist.authenticate import prompt_for_user_token
class Spotify(): class Spotify():
def __init__(self): def __init__(self):
run_server()
self.username = environ.get("SPOTIFY_USERNAME") self.username = environ.get("SPOTIFY_USERNAME")
scope = "playlist-read-private" scope = "playlist-read-private"
@ -83,6 +84,7 @@ class Spotify():
# Now it should be safe to add any new playlist items # Now it should be safe to add any new playlist items
for track_id in new_playlist: for track_id in new_playlist:
print(track_id)
try: try:
self.mpd_client.playlistadd(playlist, track_id) self.mpd_client.playlistadd(playlist, track_id)
except CommandError as e: except CommandError as e:

Loading…
Cancel
Save