def discogs_id_by_url(url, type='uri'): discogs_id = None discogs.user_agent = "NRG Processor 0.01 http://anorg.net/" try: id = url.split('/') id = id[-1] try: return '%s' % int(id) except: if '/master/' in url: log.debug('Type is "master-release"') item = discogs.MasterRelease(int(id)) if '/release/' in url: log.debug('Type is "release"') item = discogs.Release(int(id)) if '/artist/' in url: log.debug('Type is "artist"') item = discogs.Artist(id) time.sleep(1.1) return item.data['id'] except Exception, e: log.info('Unable to get id: %s', e)
def discogs_id_by_url(url, type='uri'): log = logging.getLogger('importer.base.discogs_artist_id_by_url') discogs_id = None discogs.user_agent = "NRG Processor 0.01 http://anorg.net/" try: id = url.split('/') id = id[-1] if '/master/' in url: log.debug('Type is "master-release"') item = discogs.MasterRelease(int(id)) if '/release/' in url: log.debug('Type is "release"') item = discogs.Release(int(id)) if '/artist/' in url: log.debug('Type is "artist"') item = discogs.Artist(id) time.sleep(1.1) discogs_id = item.data['id'] except Exception, e: log.info('Unable to get id: %s', e)
def __init__(self, dummy_response): # we need a dummy client here ;-( client = discogs.Client('Dummy Client - just for unit testing') self.dummy_response = dummy_response self.content = self.convert(json.loads(dummy_response.content)) self.release = discogs.Release(client, self.content['resp']['release']) DiscogsAlbum.__init__(self, self.release)
def discogs_get_master(release_urls): for release_url in release_urls: m = re.match(r'http://www.discogs.com/release/([0-9]+)', release_url) if m: release_id = int(m.group(1)) release = discogs.Release(release_id) master = release.master if master: yield (master.title, master._id, discogs_artists_str(master.artists))
def discogs_get_tracklist(release_url): m = re.match(r'http://www.discogs.com/release/([0-9]+)', release_url) if m: release_id = int(m.group(1)) release = discogs.Release(release_id) return [ track for track in release.data['tracklist'] if track['position'] != '' ] return None
def discogs_get_primary_image(url): if url is None: return None m = re.match(r'http://www.discogs.com/release/([0-9]+)', url) if m: release_id = int(m.group(1)) release = discogs.Release(release_id) if 'images' in release.data and len(release.data['images']) >= 1: for image in release.data['images']: if image['type'] == 'primary': return image # No primary image found => return first images return release.data['images'][0] return None
def fetch_release(self, release_id, source_dir): """ fetches the metadata for the given release_id from a local file """ dummy_response = DummyResponse(release_id, source_dir) # we need a dummy client here ;-( client = discogs.Client('Dummy Client - just for testing') self.content = self.convert(json.loads(dummy_response.content)) logger.debug('content: %s' % self.content) release = discogs.Release(client, self.content) return release
def discogs_image_by_url(url, type='uri'): log = logging.getLogger('importer.base.discogs_image_by_url') image = None discogs.user_agent = "NRG Processor 0.01 http://anorg.net/" try: id = url.split('/') id = id[-1] log.debug('Lookup image for discog id: %s' % (id)) if '/master/' in url: log.debug('Type is "master-release"') item = discogs.MasterRelease(int(id)) if '/release/' in url: log.debug('Type is "release"') item = discogs.Release(int(id)) if '/artist/' in url: log.debug('Type is "artist"') item = discogs.Artist(id) time.sleep(1.1) imgs = item.data['images'] have_img = False for img in imgs: if img['type'] == 'primary': print img image = img[type] have_img = True if not have_img: for img in imgs: if img['type'] == 'secondary': print img image = img[type] except Exception, e: log.info('Unable to get image: %s', e)
def look_up_details_by_release_id(self): # FIXME : The current strategy is to use only one style and genre # for the distance calculations. """ Look up all the available details of the given release_id and affect to the current song object. Parameters ---------- release_id : int The internal Discogs ID for identifying the release. Returns ------- Returns the genre, style, tempo, year and country of the associated release. Several attributes might be set to 'None' if nothing is available for that song in the database. """ release = discogs.Release(self.release_id) if __debug__: print 'Looking up release data.' release_data = release.data genre = 'None' style = 'None' tempo = 0 year = 'None' country = 'None' if 'genres' in release_data.keys(): genre = release_data['genres'][0] if 'styles' in release_data.keys(): style = release_data['styles'][0] if 'tempo' in release_data.keys(): tempo = release_data['tempo'] if 'year' in release_data.keys(): year = release_data['year'] if 'country' in release_data.keys(): country = release_data['country'] return genre, style, tempo, year, country
def discogs_get_secondary_images(url): if url is None: return [] images = [] m = re.match(r'http://www.discogs.com/release/([0-9]+)', url) if m: release_id = int(m.group(1)) release = discogs.Release(release_id) if 'images' in release.data and len(release.data['images']) >= 2: found_primary = False for image in release.data['images']: if image['type'] == 'secondary': images.append(image) elif image['type'] == 'primary': found_primary = True # if all images are secondary, it means first one as already been considered as the primary one, # so it should be excluded if not found_primary: images = images[1:] return images
def __init__(self, releaseid): self.release = discogs.Release(releaseid) discogs.user_agent = "discogstagger +http://github.com/jesseward" logger.info("Fetching %s - %s (%s)" % (self.artist, self.title, releaseid))
'cardboard/paper sleeve': 4, 'other': 5, 'keep case': 6, 'none': 7, } for release in db.execute(query): colored_out( bcolors.OKBLUE, 'Looking up release "%s" by "%s" http://musicbrainz.org/release/%s' % (release['name'], release['ac_name'], release['gid'])) m = re.match(r'http://www.discogs.com/release/([0-9]+)', release['discogs_url']) if m: discogs_release = discogs.Release(int(m.group(1))) discogs_packaging = discogs_get_release_packaging(discogs_release) if discogs_packaging: colored_out( bcolors.OKGREEN, ' * using %s, found packaging: %s' % (release['discogs_url'], discogs_packaging)) edit_note = 'Setting release packaging from attached Discogs link (%s)' % release[ 'discogs_url'] out(' * edit note: %s' % (edit_note, )) mb.set_release_packaging( release['gid'], release['packaging'], DISCOGS_MB_PACKAGING_MAPPING[discogs_packaging], edit_note, True) else: colored_out( bcolors.NONE, ' * using %s, no matching packaging has been found' %
def main(verbose=False): d = defaultdict(dict) for r, r_gid, t_name, t_pos, m_pos, url, a, a_gid, ac in db.execute( query_missing): if a_gid in discogs_artist_problematic: continue d[a][r] = (r, r_gid, t_name, t_pos, m_pos, url, a, a_gid, ac) count = len(d) for i, k in enumerate(d): if len(d[k]) != 1: continue r1 = list(d[k])[0] r, r_gid, t_name, t_pos, m_pos, url, a, a_gid, ac = d[k][r1] if m_pos > 1: db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue artist_releases = set([ r for r, in db.execute( '''SELECT DISTINCT r.id FROM release r JOIN medium m ON m.release = r.id JOIN tracklist tl ON tl.id = m.tracklist JOIN track t ON t.tracklist = tl.id WHERE t.artist_credit = %s''', ac) ]) if len(artist_releases) > 1: db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue if verbose: out(u'%d/%d - %.2f%%' % (i + 1, count, (i + 1) * 100.0 / count)) out('http://musicbrainz.org/release/%s (%d-%d)' % (r_gid, m_pos, t_pos)) out('%s' % url) out('http://musicbrainz.org/artist/%s' % a_gid) discogs_release_id = int( re.match(r'^http://www\.discogs\.com/release/([0-9]+)', url).group(1)) discogs_release = discogs.Release(discogs_release_id) t_index = 0 discogs_track = None for t in discogs_release.tracklist: if t['type'] == 'Track': t_index += 1 if t_index == t_pos: discogs_track = t break if discogs_track is None: if verbose: out('track not found') db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue discogs_artists = discogs_track['artists'] if len(discogs_artists) == 0: discogs_artists = discogs_release.artists if len(discogs_artists) != 1: if verbose: out('skip, %d track artists' % len(discogs_artists)) db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue if not are_similar(discogs_track['title'], t_name): if verbose: out(u'not similar: %s <-> %s' % (discogs_track['title'], t_name)) db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue discogs_artist = discogs_artists[0] if discogs_artist.name == 'Various': if verbose: out(u'not linking to Various') db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue ac_name = artist_credit(ac) norm_name = discogs_artist.name m = re.match(r'(.*?) \([0-9]+\)', norm_name) if m: norm_name = m.group(1) m = re.match(r'(.*?), (The)', norm_name) if m: norm_name = '%s %s' % (m.group(2), m.group(1)) if not are_similar(norm_name, ac_name): if verbose: out(u'not similar: %s [%s] <-> %s' % (norm_name, discogs_artist.name, ac_name)) db.execute( "INSERT INTO bot_discogs_artist_problematic (gid) VALUES (%s)", a_gid) continue discogs_url = discogs_artist_url(discogs_artist.name) if (a_gid, discogs_url) in discogs_artist_set: if verbose: out(u' already linked earlier (probably got removed by some editor!' ) continue text = u'Artist appears on only one release [1] (medium %d, track %d), which is linked to discogs release [2]. Also, the track names are similar:\n' % ( m_pos, t_pos) text += u'Discogs: “%s” by %s\n' % ( discogs_track['title'], combine_names([x.name for x in discogs_artists])) text += u'MBrainz: “%s” by “%s”\n\n' % (t_name, ac_name) text += u'[1] http://musicbrainz.org/release/%s\n[2] %s' % (r_gid, url) try: out(u'http://musicbrainz.org/artist/%s -> %s' % (a_gid, discogs_url)) mb.add_url('artist', a_gid, 180, discogs_url.encode('utf-8'), text) db.execute( "INSERT INTO bot_discogs_artist_set (gid,url) VALUES (%s,%s)", (a_gid, discogs_url)) except (urllib2.HTTPError, urllib2.URLError, socket.timeout) as e: out(e)