Commit 0d4d6e2a authored by Gerion Entrup's avatar Gerion Entrup
Browse files

collector/fetcher: improve pep8 compability

parent cebf0090
......@@ -141,7 +141,7 @@ class Collector(threading.Thread):
# find track in release,
# this is clearly a workaround and only works efficient because of caching.
# correct way would be to fetch all tracks directly, but the musicbrainz api
# offer no way to do this.
# offers no way to do this.
mediumlist = self.fetcher.get_table_by_id(releasedata['id'], 'release')['medium-list']
for medium in mediumlist:
for track in medium['track-list']:
......
import os, sys, pprint
import mutagen
import musicbrainzngs
import itertools
import time
import logging
class Fetcher:
"""
Fetches the musicdata and caches them.
......@@ -12,41 +10,46 @@ class Fetcher:
def __init__(self):
self.logger = logging.getLogger('collector.fetcher')
musicbrainzngs.set_useragent("brainzfs", "0.1-alpha", "https://git.finf.uni-hannover.de/Chrysops/brainzfs")
musicbrainzngs.set_useragent("brainzfs", "0.1-alpha",
"https://git.finf.uni-hannover.de/Chrysops/brainzfs")
self.cache = {'recording': {},
'release-group': {},
'artist': {},
'release': {}}
self.time = 0
#for debugging
#from offline import CACHE
#self.cache = CACHE
# for debugging
# from offline import CACHE
# self.cache = CACHE
def _delay(self):
timems = lambda: int(time.time() * 1000) #time in ms
diff = timems() - self.time
def time_ms():
return int(time.time() * 1000)
diff = time_ms() - self.time
if (diff < 1000):
time.sleep((1000 - diff) / 1000)
self.time = timems()
self.time = time_ms()
def get_table_by_id(self, mbid, tablename):
"""fetch the data from musicbrainz. Asked the cache in transparent way, if data already available and handles the musicbrainz timing rules.
Throws an musicbrainzngs.WebServiceError if anything goes wrong."""
"""fetch the data from musicbrainz. Asked the cache in transparent
way, if data already available and handles the musicbrainz timing
rules. Throws an musicbrainzngs.WebServiceError if anything goes
wrong.
"""
methods = {
'recording' : lambda mbid : musicbrainzngs.get_recording_by_id(mbid, includes=['releases', 'artists'])['recording'],
'release-group': lambda mbid : musicbrainzngs.get_release_group_by_id(mbid, includes=['artist-credits'])['release-group'],
'artist' : lambda mbid : musicbrainzngs.get_artist_by_id(mbid)['artist'],
'release' : lambda mbid : musicbrainzngs.get_release_by_id(mbid, includes=['artists',
'media',
'recordings',
'release-groups'])['release']
}
'recording': lambda mbid: musicbrainzngs.get_recording_by_id(mbid, includes=['releases', 'artists'])['recording'],
'release-group': lambda mbid: musicbrainzngs.get_release_group_by_id(mbid, includes=['artist-credits'])['release-group'],
'artist': lambda mbid: musicbrainzngs.get_artist_by_id(mbid)['artist'],
'release': lambda mbid: musicbrainzngs.get_release_by_id(mbid, includes=['artists',
'media',
'recordings',
'release-groups'])['release']
}
if mbid not in self.cache[tablename]:
#delay to follow musicbrainz rules
# delay to follow musicbrainz rules
self._delay()
#request actual data
# request actual data
result = methods[tablename](mbid)
self.cache[tablename][mbid] = result
self.logger.debug("asked web for {}".format(tablename))
......@@ -54,7 +57,4 @@ class Fetcher:
result = self.cache[tablename][mbid]
self.logger.debug("asked fetcher for {}".format(tablename))
#pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(result)
return result
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment