Track elapsed play time behind the scenes when play state is changed

This commit is contained in:
Stein Magnus Jodal 2009-12-26 15:50:59 +01:00
parent fbbac0c635
commit 0fcdd3cddb
2 changed files with 51 additions and 32 deletions

View File

@ -1,21 +1,58 @@
import logging
import time
from mopidy.exceptions import MpdNotImplemented
logger = logging.getLogger('backends.base')
class BaseBackend(object):
PLAY = u'play'
PAUSE = u'pause'
STOP = u'stop'
def __init__(self):
self.state = self.STOP
@property
def state(self):
if not hasattr(self, '_state'):
self._state = self.STOP
return self._state
@state.setter
def state(self, new_state):
(old_state, self._state) = (self._state, new_state)
logger.debug(u'Changing state: %s -> %s', old_state, new_state)
if old_state in (self.PLAY, self.STOP) and new_state == self.PLAY:
self._play_time_start()
elif old_state == self.PLAY and new_state == self.PAUSE:
self._play_time_pause()
elif old_state == self.PAUSE and new_state == self.PLAY:
self._play_time_resume()
@property
def _play_time_elapsed(self):
if self.state == self.PLAY:
time_since_started = int(time.time()) - self._play_time_started
return self._play_time_accumulated + time_since_started
elif self.state == self.PAUSE:
return self._play_time_accumulated
elif self.state == self.STOP:
return 0
def _play_time_start(self):
self._play_time_accumulated = 0
self._play_start = False
self._play_time_started = int(time.time())
def _play_time_pause(self):
time_since_started = int(time.time()) - self._play_time_started
self._play_time_accumulated += time_since_started
def _play_time_resume(self):
self._play_time_started = int(time.time())
# Status methods
def current_song(self):
return None
# Status methods
def status_bitrate(self):
return 0
@ -47,17 +84,7 @@ class BaseBackend(object):
return self.state
def status_time(self):
return u'%s:%s' % (
self.status_time_position(), self.status_time_total())
def status_time_position(self):
if self.state == self.PAUSE:
return self._play_time_accumulated
elif self.state == self.PLAY and self._play_start:
return self._play_time_accumulated + (
int(time.time()) - self._play_start)
else:
return 0
return u'%s:%s' % (self._play_time_elapsed, self.status_time_total())
def status_time_total(self):
return 0
@ -66,12 +93,11 @@ class BaseBackend(object):
return 0
# Control methods
def next(self):
self.stop()
if self._next():
self.state = self.PLAY
self._play_time_accumulated = 0
self._play_start = int(time.time())
def _next(self):
raise MpdNotImplemented
@ -79,8 +105,6 @@ class BaseBackend(object):
def pause(self):
if self.state == self.PLAY and self._pause():
self.state = self.PAUSE
self._play_time_accumulated += (
int(time.time()) - self._play_start)
def _pause(self):
raise MpdNotImplemented
@ -89,16 +113,12 @@ class BaseBackend(object):
if self.state == self.PAUSE and songpos is None and songid is None:
return self.resume()
self.stop()
if songpos is not None:
result = self._play_pos(songpos)
elif songid is not None:
result = self._play_id(songid)
else:
result = self._play()
if result:
if songpos is not None and self._play_pos(songpos):
self.state = self.PLAY
elif songid is not None and self._play_id(songid):
self.state = self.PLAY
elif self._play():
self.state = self.PLAY
self._play_time_accumulated = 0
self._play_start = int(time.time())
def _play(self):
raise MpdNotImplemented
@ -113,8 +133,6 @@ class BaseBackend(object):
self.stop()
if self._previous():
self.state = self.PLAY
self._play_time_accumulated = 0
self._play_start = int(time.time())
def _previous(self):
raise MpdNotImplemented
@ -122,7 +140,6 @@ class BaseBackend(object):
def resume(self):
if self.state == self.PAUSE and self._resume():
self.state = self.PLAY
self._play_start = int(time.time())
def _resume(self):
raise MpdNotImplemented
@ -135,6 +152,7 @@ class BaseBackend(object):
raise MpdNotImplemented
# Current/single playlist methods
def playlist_changes_since(self, version):
return None
@ -145,9 +163,11 @@ class BaseBackend(object):
return None
# Stored playlist methods
def playlists_list(self):
return None
# Music database methods
def search(self, type, what):
return None

View File

@ -16,7 +16,6 @@ def decode(string):
class SpotifyBackend(BaseBackend):
def __init__(self, *args, **kwargs):
super(SpotifyBackend, self).__init__(*args, **kwargs)
logger.info(u'Connecting to Spotify')
self.spotify = spytify.Spytify(self._username, self._password)
logger.info(u'Preloading data')