Actorify LastfmFrontend
This commit is contained in:
parent
18e252a9d3
commit
43cb8d077c
@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
import multiprocessing
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -8,6 +7,8 @@ except ImportError as import_error:
|
|||||||
from mopidy import OptionalDependencyError
|
from mopidy import OptionalDependencyError
|
||||||
raise OptionalDependencyError(import_error)
|
raise OptionalDependencyError(import_error)
|
||||||
|
|
||||||
|
from pykka.actor import ThreadingActor
|
||||||
|
|
||||||
from mopidy import settings, SettingsError
|
from mopidy import settings, SettingsError
|
||||||
from mopidy.frontends.base import BaseFrontend
|
from mopidy.frontends.base import BaseFrontend
|
||||||
from mopidy.utils.process import BaseThread
|
from mopidy.utils.process import BaseThread
|
||||||
@ -17,7 +18,7 @@ logger = logging.getLogger('mopidy.frontends.lastfm')
|
|||||||
API_KEY = '2236babefa8ebb3d93ea467560d00d04'
|
API_KEY = '2236babefa8ebb3d93ea467560d00d04'
|
||||||
API_SECRET = '94d9a09c0cd5be955c4afaeaffcaefcd'
|
API_SECRET = '94d9a09c0cd5be955c4afaeaffcaefcd'
|
||||||
|
|
||||||
class LastfmFrontend(BaseFrontend):
|
class LastfmFrontend(ThreadingActor, BaseFrontend):
|
||||||
"""
|
"""
|
||||||
Frontend which scrobbles the music you play to your `Last.fm
|
Frontend which scrobbles the music you play to your `Last.fm
|
||||||
<http://www.last.fm>`_ profile.
|
<http://www.last.fm>`_ profile.
|
||||||
@ -36,38 +37,14 @@ class LastfmFrontend(BaseFrontend):
|
|||||||
- :attr:`mopidy.settings.LASTFM_PASSWORD`
|
- :attr:`mopidy.settings.LASTFM_PASSWORD`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self):
|
||||||
super(LastfmFrontend, self).__init__(*args, **kwargs)
|
|
||||||
(self.connection, other_end) = multiprocessing.Pipe()
|
|
||||||
self.thread = LastfmFrontendThread(self.core_queue, other_end)
|
|
||||||
|
|
||||||
def start(self):
|
|
||||||
self.thread.start()
|
|
||||||
|
|
||||||
def destroy(self):
|
|
||||||
self.thread.destroy()
|
|
||||||
|
|
||||||
def process_message(self, message):
|
|
||||||
if self.thread.is_alive():
|
|
||||||
self.connection.send(message)
|
|
||||||
|
|
||||||
|
|
||||||
class LastfmFrontendThread(BaseThread):
|
|
||||||
def __init__(self, core_queue, connection):
|
|
||||||
super(LastfmFrontendThread, self).__init__(core_queue)
|
|
||||||
self.name = u'LastfmFrontendThread'
|
|
||||||
self.connection = connection
|
|
||||||
self.lastfm = None
|
self.lastfm = None
|
||||||
self.last_start_time = None
|
self.last_start_time = None
|
||||||
|
|
||||||
def run_inside_try(self):
|
# TODO-PYKKA: Do setup after actor starts
|
||||||
self.setup()
|
self._setup()
|
||||||
while self.lastfm is not None:
|
|
||||||
self.connection.poll(None)
|
|
||||||
message = self.connection.recv()
|
|
||||||
self.process_message(message)
|
|
||||||
|
|
||||||
def setup(self):
|
def _setup(self):
|
||||||
try:
|
try:
|
||||||
username = settings.LASTFM_USERNAME
|
username = settings.LASTFM_USERNAME
|
||||||
password_hash = pylast.md5(settings.LASTFM_PASSWORD)
|
password_hash = pylast.md5(settings.LASTFM_PASSWORD)
|
||||||
@ -78,17 +55,11 @@ class LastfmFrontendThread(BaseThread):
|
|||||||
except SettingsError as e:
|
except SettingsError as e:
|
||||||
logger.info(u'Last.fm scrobbler not started')
|
logger.info(u'Last.fm scrobbler not started')
|
||||||
logger.debug(u'Last.fm settings error: %s', e)
|
logger.debug(u'Last.fm settings error: %s', e)
|
||||||
|
self.stop()
|
||||||
except (pylast.NetworkError, pylast.MalformedResponseError,
|
except (pylast.NetworkError, pylast.MalformedResponseError,
|
||||||
pylast.WSError) as e:
|
pylast.WSError) as e:
|
||||||
logger.error(u'Error during Last.fm setup: %s', e)
|
logger.error(u'Error during Last.fm setup: %s', e)
|
||||||
|
self.stop()
|
||||||
def process_message(self, message):
|
|
||||||
if message['command'] == 'started_playing':
|
|
||||||
self.started_playing(message['track'])
|
|
||||||
elif message['command'] == 'stopped_playing':
|
|
||||||
self.stopped_playing(message['track'], message['stop_position'])
|
|
||||||
else:
|
|
||||||
pass # Ignore commands for other frontends
|
|
||||||
|
|
||||||
def started_playing(self, track):
|
def started_playing(self, track):
|
||||||
artists = ', '.join([a.name for a in track.artists])
|
artists = ', '.join([a.name for a in track.artists])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user