Style fixes

This commit is contained in:
Adrian Sampson 2014-10-12 13:19:03 -07:00
parent a07e20eaf8
commit 490f84fc34

View file

@ -22,6 +22,7 @@ from beets import config
log = logging.getLogger('beets')
API_URL = 'http://ws.audioscrobbler.com/2.0/'
class LastImportPlugin(BeetsPlugin):
def __init__(self):
super(LastImportPlugin, self).__init__()
@ -35,8 +36,7 @@ class LastImportPlugin(BeetsPlugin):
})
def commands(self):
cmd = ui.Subcommand('lastimport',
help='import last.fm play-count')
cmd = ui.Subcommand('lastimport', help='import last.fm play-count')
def func(lib, opts, args):
import_lastfm(lib)
@ -44,6 +44,7 @@ class LastImportPlugin(BeetsPlugin):
cmd.func = func
return [cmd]
def import_lastfm(lib):
user = config['lastfm']['user']
api_key = config['lastfm']['api_key']
@ -63,43 +64,48 @@ def import_lastfm(lib):
retry_limit = config['lastimport']['retry_limit'].get(int)
# Iterate through a yet to be known page total count
while page_current < page_total:
log.info(
'lastimport: Querying page #{0}{1}...'
.format(
page_current+1,
'/'+str(page_total) if page_total > 1 else ''
)
)
log.info('lastimport: Querying page #{0}{1}...'.format(
page_current + 1,
'/' + str(page_total) if page_total else ''
))
for retry in range(0, retry_limit):
page = fetch_tracks(user, api_key, page_current+1, per_page)
page = fetch_tracks(user, api_key, page_current + 1, per_page)
if 'tracks' in page:
# Let us the reveal the holy total pages!
page_total = int(page['tracks']['@attr']['totalPages'])
if page_total < 1:
# It means nothing to us!
raise ui.UserError('No data to process, empty query from last.fm')
raise ui.UserError('Last.fm reported no data.')
found, unknown = process_tracks(lib, page['tracks']['track'])
found_total += found
unknown_total += unknown
break
else:
log.error('lastimport: ERROR: unable to read page #{0}'
.format(page_current+1))
log.error('lastimport: ERROR: unable to read page #{0}'.format(
page_current + 1
))
if retry < retry_limit:
log.info('lastimport: Retrying page #{0}... ({1}/{2} retry)'
.format(page_current+1, retry+1, retry_limit))
log.info(
'lastimport: Retrying page #{0}... ({1}/{2} retry)'
.format(page_current + 1, retry + 1, retry_limit)
)
else:
log.error('lastimport: FAIL: unable to fetch page #{0}, tried {1} times'
.format(page_current, retry+1))
log.error(
'lastimport: FAIL: unable to fetch page #{0}, '
'tried {1} times'.format(page_current, retry + 1)
)
page_current += 1
log.info('lastimport: ... done!')
log.info('lastimport: finished processing {0} song pages'.format(page_total))
log.info('lastimport: finished processing {0} song pages'.format(
page_total
))
log.info('lastimport: {0} unknown play-counts'.format(unknown_total))
log.info('lastimport: {0} play-counts imported'.format(found_total))
def fetch_tracks(user, api_key, page, limit):
return requests.get(API_URL, params={
'method': 'library.gettracks',
@ -110,33 +116,36 @@ def fetch_tracks(user, api_key, page, limit):
'format': 'json',
}).json()
def process_tracks(lib, tracks):
total = len(tracks)
total_found = 0
total_fails = 0
log.info('lastimport: Received {0} tracks in this page, processing...'
.format(total))
log.info(
'lastimport: Received {0} tracks in this page, processing...'
.format(total)
)
for num in xrange(0, total):
song = ''
song = ''
trackid = tracks[num]['mbid'].strip()
artist = tracks[num]['artist'].get('name', '').strip()
title = tracks[num]['name'].strip()
artist = tracks[num]['artist'].get('name', '').strip()
title = tracks[num]['name'].strip()
album = ''
if 'album' in tracks[num]:
album = tracks[num]['album'].get('name', '').strip()
# log.debug(u'lastimport: query: {0} - {1} ({2})'
# .format(artist, title, album))
log.debug(u'lastimport: query: {0} - {1} ({2})'
.format(artist, title, album))
# First try to query by musicbrainz's trackid
if (trackid):
song = lib.items('mb_trackid:'+trackid).get()
if trackid:
song = lib.items('mb_trackid:' + trackid).get()
# Otherwise try artist/title/album
if (not song):
# log.debug(u'lastimport: no match for mb_trackid {0}, trying by '
# 'artist/title/album'.format(trackid))
if not song:
log.debug(u'lastimport: no match for mb_trackid {0}, trying by '
u'artist/title/album'.format(trackid))
query = dbcore.AndQuery([
dbcore.query.SubstringQuery('artist', artist),
dbcore.query.SubstringQuery('title', title),
@ -145,8 +154,8 @@ def process_tracks(lib, tracks):
song = lib.items(query).get()
# If not, try just artist/title
if (not song):
# log.debug(u'lastimport: no album match, trying by artist/title')
if not song:
log.debug(u'lastimport: no album match, trying by artist/title')
query = dbcore.AndQuery([
dbcore.query.SubstringQuery('artist', artist),
dbcore.query.SubstringQuery('title', title)
@ -154,30 +163,38 @@ def process_tracks(lib, tracks):
song = lib.items(query).get()
# Last resort, try just replacing to utf-8 quote
if (not song):
if not song:
title = title.replace("'", u'\u2019')
# log.debug(u'lastimport: no title match, trying utf-8 single quote')
log.debug(u'lastimport: no title match, trying utf-8 single quote')
query = dbcore.AndQuery([
dbcore.query.SubstringQuery('artist', artist),
dbcore.query.SubstringQuery('title', title)
])
song = lib.items(query).get()
if (song):
if song:
count = int(song.get('play_count', 0))
new_count = int(tracks[num]['playcount'])
log.debug(u'lastimport: match: {0} - {1} ({2}) updating: play_count {3} => {4}'
.format(song.artist, song.title, song.album, count, new_count))
log.debug(
u'lastimport: match: {0} - {1} ({2}) '
u'updating: play_count {3} => {4}'.format(
song.artist, song.title, song.album, count, new_count
)
)
song['play_count'] = new_count
song.store()
total_found += 1
else:
total_fails += 1
log.info(u'lastimport: - No match: {0} - {1} ({2})'
.format(artist, title, album))
log.info(
u'lastimport: - No match: {0} - {1} ({2})'
.format(artist, title, album)
)
if total_fails > 0:
log.info('lastimport: Acquired {0}/{1} play-counts ({2} unknown)'
.format(total_found, total, total_fails))
log.info(
'lastimport: Acquired {0}/{1} play-counts ({2} unknown)'
.format(total_found, total, total_fails)
)
return total_found, total_fails