Skip to content

Commit

Permalink
Merge pull request #20 from jesseward/issue-19-scrobble-album-data
Browse files Browse the repository at this point in the history
Issue #19 - support the ability to scrobble album data
  • Loading branch information
jesseward committed Oct 14, 2014
2 parents 9c9b66e + 8e2b4b4 commit 1a1a267
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
8 changes: 4 additions & 4 deletions plex_scrobble/lastfm.py
Expand Up @@ -120,17 +120,17 @@ def _get_auth_token(self):
return token['token'][0]


def scrobble(self, artist, track):
def scrobble(self, artist, track, album):

session = self.get_session()
ts = '%d' % (time.time() - 100)

self.logger.info(u'submitting {artist} - {track} to last.fm.'.format(
artist=artist, track=track))
self.logger.info(u'submitting {artist} - {track} ({album}) to last.fm.'.format(
artist=artist, track=track, album=album))

try:
self._do_lastfm_query('POST', 'track.scrobble', timestamp=ts,
artist=artist, track=track, sk=session)
artist=artist, track=track, album=album, sk=session)
except:
return False

Expand Down
17 changes: 13 additions & 4 deletions plex_scrobble/plex_monitor.py
Expand Up @@ -58,7 +58,7 @@ def fetch_metadata(l_id, config):
tree = ET.fromstring(metadata.read())
track = tree.find('Track')

# BUG: https://github.com/jesseward/plex-lastfm-scrobbler/issues/7
# BUGFIX: https://github.com/jesseward/plex-lastfm-scrobbler/issues/7
if track is None:
logger.info('Ignoring played item library-id={l_id}, could not determine audio library information.'.
format(l_id=l_id))
Expand All @@ -72,12 +72,20 @@ def fetch_metadata(l_id, config):

song = track.get('title')

# BUGFIX : https://github.com/jesseward/plex-lastfm-scrobbler/issues/19
# add support for fetching album metadata from the track object.
album = track.get('parentTitle')
if not album:
logger.warn('unable to locate album name for ibary-id={l_id}'.format(
l_id=l_id))
album = None

if not all((artist, song)):
logger.warn('unable to retrieve meatadata keys for libary-id={l_id}'.
format(l_id=l_id))
return False

return {'track': song, 'artist': artist}
return {'track': song, 'artist': artist, 'album': album}


def monitor_log(config):
Expand Down Expand Up @@ -140,12 +148,13 @@ def monitor_log(config):

# submit to last.fm
lastfm = LastFm(config)
a = lastfm.scrobble(metadata['artist'], metadata['track'])
a = lastfm.scrobble(metadata['artist'], metadata['track'],
metadata['album'])

# scrobble was not successful , add to our retry queue
if not a:
cache = ScrobbleCache(config)
cache.add(metadata['artist'], metadata['track'])
cache.add(metadata['artist'], metadata['track'], metadata['album'])
cache.close

last_played = played
16 changes: 9 additions & 7 deletions plex_scrobble/scrobble_cache.py
Expand Up @@ -19,19 +19,20 @@ def __init__(self, config):
def length(self):
return len(self.cache)

def add(self, key, value, cache_hit=1):
def add(self, key, value, album, cache_hit=1):

self.logger.info('adding \'{key}\' \'{value}\' to retry cache.'.format(
key=key, value=value))
self.logger.info('adding \'{key}\' \'{value}\' ({album}) to retry cache.'.format(
key=key, value=value, album=album))

self.cache[str(time.time())] = [key, value, cache_hit]
self.cache[str(time.time())] = [key, value, cache_hit, album]
self.cache.sync()

def remove(self, key):
''' remove an existing entry from cache file. '''

self.logger.info(u'removing \'{key}\': \'{artist}\' - \'{track}\' from retry cache.'.format(
key=key, artist=self.cache[key][0], track=self.cache[key][1]))
self.logger.info(u'removing \'{key}\': \'{artist}\' - \'{track}\' ({album})from retry cache.'.format(
key=key, artist=self.cache[key][0], track=self.cache[key][1],
album=elf.cache[key][3]))
del self.cache[key]
self.cache.sync()

Expand All @@ -57,7 +58,8 @@ def retry_queue(self):
for key in self.cache:
# do submissions retry
try:
a = lastfm.scrobble(self.cache[key][0], self.cache[key][1])
a = lastfm.scrobble(self.cache[key][0], self.cache[key][1],
self.cache[key][3])
self.cache[key][2] += 1
except:
pass
Expand Down

0 comments on commit 1a1a267

Please sign in to comment.