def getReleases(self, artistResult): q = Query() offset = 0 limit = 100 releaseResults = set() while True: try: f = ReleaseFilter(artistId=artistResult.getKey( MusicbrainzSource._sourceName), offset=offset, limit=limit) results = q.getReleases(f) except WebServiceError, e: raise SourceError count = len(results) results = filter(lambda res: res.getScore() == 100, results) for r in results: rr = ReleaseResult( title=r.getRelease().getTitle(), date=r.getRelease().getEarliestReleaseDate(), tracksCount=r.getRelease().getTracksCount()) rr.setKey(MusicbrainzSource._sourceName, r.getRelease().getId()) releaseResults.add(rr) if count < limit: break offset += count
def getReleases(self, artistResult): q = Query() offset = 0 limit = 100 releaseResults = set() while True: try: f = ReleaseFilter(artistId = artistResult.getKey(MusicbrainzSource._sourceName), offset = offset, limit = limit) results = q.getReleases(f) except WebServiceError, e: raise SourceError count = len(results) results = filter(lambda res: res.getScore() == 100, results) for r in results: rr = ReleaseResult(title = r.getRelease().getTitle(), date = r.getRelease().getEarliestReleaseDate(), tracksCount = r.getRelease().getTracksCount()) rr.setKey(MusicbrainzSource._sourceName, r.getRelease().getId()) releaseResults.add(rr) if count < limit: break offset += count
def FetchTrackListByArtistRelease(self, string_artist_name, string_release_name): musicbrainz_query = Query() try: # Search for all releases matching the given name. Limit the results # to the first best matches. string_release_id = "" string_release_title = "" musicbrainz_release_filter = ReleaseFilter(title=string_release_name, artistName=string_artist_name, limit=1) musicbrainz_release_results = musicbrainz_query.getReleases(musicbrainz_release_filter) if musicbrainz_release_results is not None: if len(musicbrainz_release_results) > 0: string_release_id = musicbrainz_release_results[0].getRelease().getId() string_release_title = musicbrainz_release_results[0].getRelease().getTitle() include_info = ReleaseIncludes(artist=True, tracks=True) release = self.__musicbrainz_query__.getReleaseById(string_release_id, include_info) track_list = [] for track in release.getTracks(): track_title = track.getTitle() for escape_char in self.__escape_list__: track_title = track_title.replace(escape_char, "") track_list.append(track_title) return string_release_title, track_list except WebServiceError, e: # print 'Error:', e return []
def populate_album_set(self): """ Find and create models for all albums released by this artist. Only albums with an Amazon ASIN are imported, to try and stop the database getting clogged up with b-sides, remixes, and bonus material. """ # We can't do anything without the MusicBrainz and Last.fm libraries. try: ReleaseFilter except NameError: return False # Find any official album release held by MusicBrainz for this artist. filter = ReleaseFilter(artistName=self.name, releaseTypes=(Release.TYPE_ALBUM, Release.TYPE_OFFICIAL)) query = Query() releases = query.getReleases(filter) for release in releases: album = release.release # Only import albums with an Amazon ASIN. That allows for some # quality-control as Music Brainz lists every B-side and bonus # material you can think of. if album.asin: # First try and find an already-existing album with this ASIN # As an ASIN is unique it means we'll find it even if the fields # have been changed since creation. try: db_album = Album.objects.get(asin=album.asin) except Album.DoesNotExist: db_album = Album(artist=self, title=album.title, asin=album.asin, mbid=album.id.rsplit("/", 1)[1]) # MusicBrainz stores releases dates for as many countries as # it can. I'm only interested in Britain though, so look # for that first. As a fallback, us the world wide release # date (XE) or the US release date. release_dates = dict( (r.country, r.date) for r in album.releaseEvents) if release_dates: # GB = United Kingdom, XE = world, US = United States. for country in ('GB', 'XE', 'US'): if release_dates.has_key(country): db_album.released_in = country # The release date can be in the format "2010", # "2010-02", or "2010-02-18", so make up the # missing month and/or day so a proper release # date object can be created. release_date = release_dates[country] date_list = map(int, release_date.split('-')) try: db_album.release_date = datetime.date( *date_list + [1] * (3 - len(date_list))) except ValueError: pass # Date couldn't be parsed. break db_album.save()
def find_artist_and_title(puid): q = Query() try: f = TrackFilter(puid=sys.argv[1]) results = q.getTracks(f) except WebServiceError, e: print 'Error:', e sys.exit(1)
def __init__(self, wait=1): if not os.path.isdir('cache'): os.mkdir('cache') os.mkdir(os.path.join('cache', 'artist_q')) os.mkdir(os.path.join('cache', 'release_q')) os.mkdir(os.path.join('cache', 'track_q')) self.wait = wait self.last_called = time.time() Query.__init__(self)
def __init__(self,wait=1) : if not os.path.isdir(CACHE_DIR) : os.mkdir(CACHE_DIR) os.mkdir(os.path.join(CACHE_DIR,'artist_q')) os.mkdir(os.path.join(CACHE_DIR,'release_q')) os.mkdir(os.path.join(CACHE_DIR,'track_q')) self.wait = wait self.last_called = time.time() Query.__init__(self)
def populate_album_set(self): """ Find and create models for all albums released by this artist. Only albums with an Amazon ASIN are imported, to try and stop the database getting clogged up with b-sides, remixes, and bonus material. """ # We can't do anything without the MusicBrainz and Last.fm libraries. try: ReleaseFilter except NameError: return False # Find any official album release held by MusicBrainz for this artist. filter = ReleaseFilter(artistName=self.name, releaseTypes=(Release.TYPE_ALBUM, Release.TYPE_OFFICIAL)) query = Query() releases = query.getReleases(filter) for release in releases: album = release.release # Only import albums with an Amazon ASIN. That allows for some # quality-control as Music Brainz lists every B-side and bonus # material you can think of. if album.asin: # First try and find an already-existing album with this ASIN # As an ASIN is unique it means we'll find it even if the fields # have been changed since creation. try: db_album = Album.objects.get(asin=album.asin) except Album.DoesNotExist: db_album = Album(artist=self, title=album.title, asin=album.asin, mbid=album.id.rsplit("/", 1)[1]) # MusicBrainz stores releases dates for as many countries as # it can. I'm only interested in Britain though, so look # for that first. As a fallback, us the world wide release # date (XE) or the US release date. release_dates = dict((r.country, r.date) for r in album.releaseEvents) if release_dates: # GB = United Kingdom, XE = world, US = United States. for country in ('GB', 'XE', 'US'): if release_dates.has_key(country): db_album.released_in = country # The release date can be in the format "2010", # "2010-02", or "2010-02-18", so make up the # missing month and/or day so a proper release # date object can be created. release_date = release_dates[country] date_list = map(int, release_date.split('-')) try: db_album.release_date = datetime.date( *date_list + [1] * (3 - len(date_list))) except ValueError: pass # Date couldn't be parsed. break db_album.save()
def lookup_puid(self,_puid): q = Query() f = TrackFilter(puid=_puid) try : r = q.getTracks(f) track = r[0].track return track except: print 'MusicBrainz::lookup_puid: Error finding track metadata.' # raise AssertionError('MusicBrainz::lookup_puid: Error finding track metadata.') return None
def lookup_puid(self, _puid): q = Query() f = TrackFilter(puid=_puid) try: r = q.getTracks(f) track = r[0].track return track except: print 'MusicBrainz::lookup_puid: Error finding track metadata.' # raise AssertionError('MusicBrainz::lookup_puid: Error finding track metadata.') return None
def testRemoveFromUserCollection(self): ws = FakeWebService() q = Query(ws) r1 = "9e186398-9ae2-45bf-a9f6-d26bc350221e" r2 = "http://musicbrainz.org/release/6b050dcf-7ab1-456d-9e1b-c3c41c18eed2" r3 = Release("d3cc336e-1010-4252-9091-7923f0429824") q.removeFromUserCollection([r1, r2, r3]) self.assertEquals(len(ws.data), 1) res = ws.data[0] self.assertEquals(res[0], "collection") self.assertEquals(res[2], "remove=9e186398-9ae2-45bf-a9f6-d26bc350221e%2C6b050dcf-7ab1-456d-9e1b-c3c41c18eed2%2Cd3cc336e-1010-4252-9091-7923f0429824")
def testSubmitPuid(self): tracks2puids = { '6a47088b-d9e0-4088-868a-394ee3c6cd33':'c2a2cee5-a8ca-4f89-a092-c3e1e65ab7e6', 'b547acbc-58c6-4a31-9806-e2348db3a167':'c2a2cee5-a8ca-4f89-a092-c3e1e65ab7e6' } ws = FakeWebService() q = Query(ws, clientId='test-1') q.submitPuids(tracks2puids) self.assertEquals(len(ws.data), 1) req = ws.data[0] qstring = 'client=test-1&puid=6a47088b-d9e0-4088-868a-394ee3c6cd33+c2a2cee5-a8ca-4f89-a092-c3e1e65ab7e6&puid=b547acbc-58c6-4a31-9806-e2348db3a167+c2a2cee5-a8ca-4f89-a092-c3e1e65ab7e6' self.assertEquals(req[0], 'track') self.assertEquals(req[2], qstring)
def testSubmitIsrc(self): tracks2isrcs = { '6a47088b-d9e0-4088-868a-394ee3c6cd33':'NZABC0800001', 'b547acbc-58c6-4a31-9806-e2348db3a167':'NZABC0800002' } ws = FakeWebService() q = Query(ws) q.submitISRCs(tracks2isrcs) self.assertEquals(len(ws.data), 1) req = ws.data[0] qstring = 'isrc=6a47088b-d9e0-4088-868a-394ee3c6cd33+NZABC0800001&isrc=b547acbc-58c6-4a31-9806-e2348db3a167+NZABC0800002' self.assertEquals(req[0], 'track') self.assertEquals(req[2], qstring)
def find_releases(puid): """Given a track's puid, return a list of (track-no, track, release) for each release that the song appeared on on.""" query = Query() trackfilter = TrackFilter(puid=puid) results = query.getTracks(trackfilter) out = [] for result in results: track = result.track rels = track.getReleases() assert len(rels) == 1 rel = rels[0] out.append((rel.getTracksOffset()+1, track, rel)) return out
def __init__(self, username, password, server='https://musicbrainz.org'): self.user_agent = 'zeroinch-bot/1.0 ( %s/user/%s )' % (server, username) self.ws = WebService(userAgent=self.user_agent, host=re.sub(r'^http://', '', server), username=username, password=password) self.q = Query(self.ws)
def find_releases(puid): """ Given a track's puid, return a list of (track-no, track, release) for each release that the song appeared on on. """ service = get_service() q = Query(service) f = TrackFilter(puid=puid) results = None while results == None: try: results = q.getTracks(f) except WebServiceError, e: print "!! WebServiceError: ", e print "!! Retry in 5 seconds..." time.sleep(5)
def print_release(mbid): q = Query() i = ReleaseIncludes( artist=True, tracks=True, urlRelations=True ) rel = q.getReleaseById(mbid, i) if not rel: print 'Not found :/' return print 'Title: %s' % rel.title print 'Artist: %s' % rel.artist.name print '' for no, trk in enumerate(rel.tracks): print ' %d. %s' % ((no+1), trk.title)
def guess_release(folder, files): nfiles = [] cache = musicdns.cache.MusicDNSCache() print "Fingerprinting..." for fn in files: try: puid, _ = cache.getpuid(fn, MUSICDNS_KEY) except IOError: puid = None track = filename_track_number(fn) nfiles += [(track, fn, puid)] nfiles.sort() matchrel = defaultdict(int) for i, (no, fn, puid) in enumerate(nfiles): print 'Asking MusicBrainz about ' + basename(fn) if puid is None: continue for tno, track, release in find_releases(puid): included = 0 if no == tno: matchrel[release.id] += 1 included = 1 print u' %d - %s - %s %s' % (tno, track.title, release.title, ' <=' if included else '') q = Query() i = ReleaseIncludes( artist=True, tracks=True, urlRelations=True ) releaseids = sorted(matchrel.iteritems(), key=itemgetter(1), reverse=1) if not releaseids: return None releases = None while not releases: try: releases = [(q.getReleaseById(rid, i), freq) for rid, freq in releaseids] except WebServiceError, e: print '!! WebServiceError: ', e print '!! Retry in 5 seconds...' time.sleep(5)
def show_release(): from musicbrainz2.webservice import Query, ArtistFilter, WebServiceError import string import musicbrainz2.webservice as ws import musicbrainz2.model as m import musicbrainz2.utils as u q = Query() inc = ws.ReleaseIncludes(artist=True, releaseEvents=True, labels=True, discs=True, tracks=True) rid = u'http://musicbrainz.org/release/52ec1af7-28a1-42e3-896a-0eddc8e338d5.html' release = q.getReleaseById(rid, inc) tracks = release.tracks for t in tracks: #print dir(t) minutes,seconds = divmod(t.duration/1000,60) print t.title, '-', string.join([str(minutes),':',str(seconds)],'')
def main(argv=None): if argv==None: argv=sys.argv q = Query() conn = sqlite3.connect('db/mu_trumps.sqlite3') c = conn.cursor() insert_cursor = conn.cursor() c.execute('select * from artists') for artist_id, created, modified, artist_name, artist_url in c: try: # Search for all artists matching the given name. Limit the results # to the best match. f = ArtistFilter(name=artist_name, limit=5) artistResults = q.getArtists(f) except WebServiceError, e: print 'Error:', e if "HTTP Error 503" in str(e): print "taking a rest..." sleep(SLEEP_TIME*10) continue try: mbz_id = artistResults[0].artist.id except IndexError: print "Could not find a musicbrainz id for the artist", artist_name, "moving on..." continue if VERBOSE: print "For artist", artist_name, "found id", artist_id try: # The result should include all official albums. # inc = ws.ArtistIncludes( releases=(m.Release.TYPE_OFFICIAL, m.Release.TYPE_ALBUM), tags=True, releaseGroups=True) artist = q.getArtistById(mbz_id, inc) except ws.WebServiceError, e: print 'Error:', e if "HTTP Error 503" in str(e): print "taking a rest..." sleep(SLEEP_TIME*10) continue
def find_album_name( track_details ): """Find album name via Musicbrainz API Arguments: track_details -- dict containing 'Title' and 'Artist' Returns album name, empty string if unsuccessful. """ album = "" # Musicbrainz limits API calls to one per second from a specific IP time.sleep(1.1) query = Query() # Loop through at most 9 times as the webservice is occasionally busy. # Results will not be None if is successful i = 1 results = None while (results == None) and (i < 10): try: tracks = TrackFilter(title=track_details['Title'], artistName=track_details['Artist']) results = query.getTracks(tracks) except (AuthenticationError, ConnectionError, RequestError, ResponseError, ResourceNotFoundError, WebServiceError) as error: print('Error:', error, 'waiting', i*10, 'seconds') time.sleep(i*10) i += 1 results = None if (results != None) and (len(results) != 0): album = find_preferred_album( results ) return album
def get_musicbrainz_id(self): """ Retrieve the MusicBrainz id for this artist and save it on the model. """ # Make sure the useful bits of the musicbrainz2 package have been # imported. try: Query, ArtistFilter except NameError: return False # Query MusicBrainz. artist_filter = ArtistFilter(name=self.name) query = Query() try: artist = query.getArtists(artist_filter)[0].artist self.mbid = artist.id.rsplit("/", 1)[1] self.save() except (IndexError, AttributeError): return False
def sanitize_with_musicBrainz(self): titleName = self['title'] artistName = self['artist'] albumName = self['album'] q = Query() try: logging.debug("Querying with title:{},artist:{},album:{}" .format(titleName, artistName, albumName)) f = TrackFilter(title=titleName, artistName=artistName, releaseTitle=albumName, limit=2) results = q.getTracks(f) logging.debug("results are " + str(results)) except WebServiceError as e: logging.error("Failed to contact musicbrainz server.") if str(e)[:15]=="HTTP Error 503:": logging.info("Pausing for a moment, \ the musicbrainz server doesn't handle too much request") sleep(60) return self.sanitize_with_musicBrainz() self.treat_musicbrainz_result(results)
def get_puid_tags(puid): """Returns a list of `TagGroup` elements, matching the song's puid""" global _lastServerCall # wait at least a second while True: currentTime = time.time() if currentTime >= _lastServerCall+1: _lastServerCall = currentTime break time.sleep(0.05) q = Query() res = [] try: f = TrackFilter(puid=puid) results = q.getTracks(f) except WebServiceError, e: print e return None
class mbzAPI: def __init__(self): self.q = Query() def findArtists(self, artist, limit = 20): try: aFilter = ArtistFilter(artist, limit) return self.q.getArtists(aFilter) except WebServiceError, e: # Logging Need return None
def getArtists(self, name, date=None, disambiguation=None): q = Query() if disambiguation: disPattern = re.compile(disambiguation, re.I) else: disPattern = None offset = 0 limit = 100 artistResults = set() while True: try: f = ArtistFilter(name=name, offset=offset, limit=limit) results = q.getArtists(f) except WebServiceError, e: raise SourceError results = filter(lambda res: res.getScore() == 100, results) # use filtered count because resulsts are score-ordered count = len(results) results = filter( lambda res: (not date or res.getArtist().getBeginDate() == date) and (not disPattern or disPattern.search(res.getArtist( ).getDisambiguation())), results) for r in results: ar = ArtistResult(name=r.getArtist().getName(), date=r.getArtist().getBeginDate(), disambiguation=disambiguation) # use original disambiguation cause it'll be used in # artist's comparation ar.setKey(MusicbrainzSource._sourceName, r.getArtist().getId()) artistResults.add(ar) if count < limit: break offset += count
def getArtists(self, name, date = None, disambiguation = None): q = Query() if disambiguation: disPattern = re.compile(disambiguation, re.I) else: disPattern = None offset = 0 limit = 100 artistResults = set() while True: try: f = ArtistFilter(name = name, offset = offset, limit = limit) results = q.getArtists(f) except WebServiceError, e: raise SourceError results = filter(lambda res: res.getScore() == 100, results) # use filtered count because resulsts are score-ordered count = len(results) results = filter(lambda res: (not date or res.getArtist().getBeginDate() == date) and ( not disPattern or disPattern.search(res.getArtist().getDisambiguation()) ), results) for r in results: ar = ArtistResult(name = r.getArtist().getName(), date = r.getArtist().getBeginDate(), disambiguation = disambiguation) # use original disambiguation cause it'll be used in # artist's comparation ar.setKey(MusicbrainzSource._sourceName, r.getArtist().getId()) artistResults.add(ar) if count < limit: break offset += count
def guess_release(self, folder, files): nfiles = [] print "Fingerprinting..." for fn in files: try: puid, _ = self.cache.getpuid(fn, self.musicdns_key) except IOError: puid = None track = filename_track_number(fn) nfiles += [(track, fn, puid)] nfiles.sort() matchrel = defaultdict(int) for i, (no, fn, puid) in enumerate(nfiles): print "Asking MusicBrainz about " + basename(fn) if puid is None: continue for tno, track, release in find_releases(puid): included = 0 if no == tno: matchrel[release.id] += 1 included = 1 print u" %d - %s - %s %s" % (tno, track.title, release.title, " <=" if included else "") service = get_service() q = Query(service) i = ReleaseIncludes(artist=True, tracks=True, urlRelations=True) releaseids = sorted(matchrel.iteritems(), key=itemgetter(1), reverse=1) if not releaseids: return None releases = None while not releases: try: releases = [(q.getReleaseById(rid, i), freq) for rid, freq in releaseids] except WebServiceError, e: print "!! WebServiceError: ", e print "!! Retry in 5 seconds..." time.sleep(5)
def getTracks(self,filter=filter) : p = md5.new(pickle.dumps(filter)).hexdigest() cache_file = os.path.join(CACHE_DIR,'track_q',p) if os.path.exists(cache_file) : instream = open(cache_file,"r") toret = pickle.loads(instream.read()) instream.close() return toret else : self.throttle() toret = Query.getTracks(self,filter=filter) outstream = open(cache_file,"w") outstream.write(pickle.dumps(toret)) outstream.close() return toret
def getTracks(self, filter=filter): p = md5.new(pickle.dumps(filter)).hexdigest() cache_file = os.path.join('cache', 'track_q', p) if os.path.exists(cache_file): instream = open(cache_file, "r") toret = pickle.loads(instream.read()) instream.close() return toret else: self.throttle() toret = Query.getTracks(self, filter=filter) outstream = open(cache_file, "w") outstream.write(pickle.dumps(toret)) outstream.close() return toret
def testSubmitCdStub(self): from musicbrainz2.model import Disc, CDStub ws = FakeWebService() q = Query(ws, clientId='test-1') discid = "6EmGGSLhuDYz2lNXtqrCiCCqO0o-" disc = Disc(discid) disc.firstTrackNum = 1 disc.lastTrackNum = 4 disc.sectors = 89150 disc.addTrack( (150, 20551) ) disc.addTrack( (20701, 26074) ) disc.addTrack( (46775, 19438) ) disc.addTrack( (66213, 22937) ) cdstub = CDStub(disc) cdstub.artist = "artist" cdstub.title = "title" cdstub.addTrack("trackname1") cdstub.addTrack("trackname2") cdstub.addTrack("trackname3") cdstub.addTrack("trackname4") q.submitCDStub(cdstub) cdstub.barcode = "12345" cdstub.comment = "acomment" q.submitCDStub(cdstub) self.assertEquals(len(ws.data), 2) req = ws.data[0] qstring = 'client=test-1&discid=6EmGGSLhuDYz2lNXtqrCiCCqO0o-&title=title&artist=artist&track0=trackname1&track1=trackname2&track2=trackname3&track3=trackname4&toc=1+4+89150+150+20701+46775+66213' self.assertEquals(req[0], 'release') self.assertEquals(req[2], qstring) req = ws.data[1] qstring = 'client=test-1&discid=6EmGGSLhuDYz2lNXtqrCiCCqO0o-&title=title&artist=artist&barcode=12345&comment=acomment&track0=trackname1&track1=trackname2&track2=trackname3&track3=trackname4&toc=1+4+89150+150+20701+46775+66213' self.assertEquals(req[2], qstring) cdstub._tracks = [] cdstub.addTrack("tname1", "artist1") cdstub.addTrack("tname2", "artist2") cdstub.addTrack("tname3", "artist3") cdstub.addTrack("tname4", "artist4") q.submitCDStub(cdstub) self.assertEquals(len(ws.data), 3) req = ws.data[2] qstring = 'client=test-1&discid=6EmGGSLhuDYz2lNXtqrCiCCqO0o-&title=title&artist=artist&barcode=12345&comment=acomment&track0=tname1&artist0=artist1&track1=tname2&artist1=artist2&track2=tname3&artist2=artist3&track3=tname4&artist3=artist4&toc=1+4+89150+150+20701+46775+66213' self.assertEquals(req[2], qstring)
def testSubmitUserTags(self): ws = FakeWebService() q = Query(ws) t1 = [u"foo", u"bar", u"f\u014do"] t2 = [Tag(u"foo"), Tag(u"bar"), Tag(u"f\u014do")] prefix = 'http://musicbrainz.org/artist/' uri = prefix + 'c0b2500e-0cef-4130-869d-732b23ed9df5' q.submitUserTags(uri, t1) q.submitUserTags(uri, t2) self.assertEquals(len(ws.data), 2) self.assertEquals(ws.data[0], ws.data[1]) q.submitUserRating(uri, Rating(5)) q.submitUserRating(uri, 5) self.assertEquals(len(ws.data), 4) self.assertEquals(ws.data[2], ws.data[3])
class MusicBrainzTrackSearch: """ Searches MusicBrainz database for Suggestions based on a Track """ def __init__(self): self.query = Query() def _build_musicbrainz_query(self, track): """ Build a query string in Lucene Format. track: The Track object wich values will be used to build the query """ tags = { 'release': 'album', 'artist': 'artist', 'tnum': 'tracknumber', 'track': 'title' } query_params = [] for musicbrainz_tag, exaile_tag in tags.iteritems(): tag_value = track.get_tag(exaile_tag) if tag_value: query_params.append(musicbrainz_tag + ":("+ tag_value[0] +")") query = ' '.join(query_params) return query def get_tracks_suggestions(self, track): """ Fetch track suggestions from Musicbrainz WebService track: The Track object wich values will be used in the query """ query = self._build_musicbrainz_query(track) filter = TrackFilter(query = query) tracks_result = self.query.getTracks(filter) return tracks_result
def main(self): if len(sys.argv) < 2: print "Usage:\n\t%s <archive|folder>" % sys.argv[0] sys.exit() s = os.path.abspath(sys.argv[1]) try: shutil.rmtree(".brainwash-work") except: pass try: os.mkdir(".brainwash-work") except: pass os.chdir(".brainwash-work") if os.path.isdir(s): print "Creating working copy..." shutil.copytree(s, "work") else: subprocess.call(["unp", s]) folders = find_music_folders(".") for folder in folders: music_files = myglob(folder[0], "*" + folder[1]) if len(music_files) < 2: cue_files = myglob(folder[0], "*.cue") if len(cue_files) < 1: # ask for mbid # need to write cuesheet generation code!!! # print "There is no cue file. To generate one I need the Release MBID." # mbid = raw_input('Enter MBID: ').strip() print "There is no cuesheet. please generate one yourself :P" continue else: cue_file = cue_files[0] wav_file = self.decode_to_wav(music_files[0]) subprocess.call(["bchunk", "-w", wav_file, cue_file, folder[0] + "/tmp-brainwash-"]) wav_files = myglob(folder[0], "tmp-brainwash-*.wav") music_files = [] for wav_file in wav_files: music_files += [self.encode_to_flac(wav_file)] folder[1] = "flac" # encode into a nice format if folder[1] not in formats_final: wav_files = [] if folder[1] == ".wav": wav_files = music_files[:] else: for music_file in music_files: wav_files += [self.decode_to_wav(music_file)] music_files = [] for wav_file in wav_files: music_files += [self.encode_to_flac(wav_file)] folder[1] = "flac" # take over the tags? print "Found: " + folder[0] try: mbid = file(folder[0] + "/.mbid").read().strip() print "Using existing mbid" except: release = self.guess_release(folder[0], music_files) if release is None: # ask for mbid print "Could not guess!" mbid = raw_input("Enter MBID: ").strip() else: # ask if guess is correct tmp_mbid = release.id.split("/")[-1:][0] print "Selected %s" % tmp_mbid answer = raw_input("Correct? [Y|n]").strip() if answer == "" or answer.lower() == "y": mbid = tmp_mbid else: print str(answer.lower()) mbid = raw_input("Enter MBID: ").strip() file(join(folder[0], ".mbid"), "w").write(str(mbid)) service = get_service() q = Query(service) i = ReleaseIncludes(artist=True, tracks=True, urlRelations=True, releaseEvents=True, discs=True) try: release = q.getReleaseById(mbid, i) except WebServiceError, e: print "Error: ", e continue dst = self.config["destination_dir"] year = 9999 for event in release.releaseEvents: year = min(year, int(event.date[:4])) if year == 9999: year = int(raw_input("Enter Release Year: ").strip()) release_title = self.clean_filename(release.title) if release.TYPE_SOUNDTRACK in release.getTypes(): dst += "/_soundtracks/(%s) %s/" % (year, release_title) elif not release.artist: dst += "/_va/(%s) %s/" % (year, release_title) else: sort_char = release.artist.sortName[:1].lower() sort_char = unicodedata.normalize("NFKD", sort_char).encode("ASCII", "ignore") dst += "/%s/%s/(%s) %s/" % ( sort_char, self.clean_filename(release.artist.sortName), year, release_title, ) try: os.makedirs(dst) except: raw_input("Failed creating %s! Press Anykey" % dst) file(join(dst, ".mbid"), "w").write(str(mbid)) for music_file in music_files: # fix permissions for broken archives os.chmod(music_file, (stat.S_IRUSR + stat.S_IWUSR + stat.S_IRGRP + stat.S_IROTH)) track_number = filename_track_number(music_file) track = release.tracks[track_number - 1] if folder[1] == ".mp3": # meta_file = MP3(music_file, ID3=EasyID3) meta_file = EasyID3() elif folder[1] == ".ogg": meta_file = OggVorbis(music_file) elif folder[1] == "flac": meta_file = FLAC(music_file) else: print "Uh oh, no meta-file for " + music_file # if not meta_file.tags: # meta_file.add_tags() artist = track.artist.name if track.artist else release.artist.name meta_file["title"] = track.title meta_file["artist"] = artist meta_file["album"] = release.title meta_file["tracknumber"] = str(track_number) + "/" + str(len(music_files)) meta_file["genre"] = self.get_lastfm_tags(meta_file["artist"][0]) meta_file.save(music_file) file_dst = dst + self.clean_filename( "%02d-%s - %s%s%s" % (track_number, artist, track.title, "." if folder[1] == "flac" else "", folder[1]) ) os.rename(music_file, file_dst) # cover art image_files = myglob(folder[0], "*.[jpg|png]") if len(image_files) > 1: for image_file in image_files: os.rename(image_file, dst + basename(image_file)) elif len(image_files) == 1: os.rename(image_files[0], dst + "cover." + image_files[0][-3:]) else: # try downlaod from amazon if release.asin: urllib.urlretrieve( "http://images.amazon.com/images/P/" + release.asin + ".01.LZZZZZZZ.jpg", dst + "cover.jpg" ) if os.path.getsize(dst + "cover.jpg") < 1000L: os.remove(dst + "cover.jpg") print "Done: " + dst
def get_release(self, gid): q = Query(self.ws) inc = ReleaseIncludes(tracks=True, isrcs=True) return q.getReleaseById(gid, include=inc)
import logging from musicbrainz2.webservice import Query, ArtistFilter, WebServiceError, ReleaseFilter, ArtistIncludes, ReleaseIncludes import musicbrainz2.model as m from time import sleep import codecs logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.ERROR) sys.stdout = codecs.getwriter('utf8')(sys.stdout) # workaround for pipes if len(sys.argv) < 2: print "Usage:", os.path.basename(sys.argv[0]), "'artist name' [offset]" sys.exit(1) q = Query() try: # Search for all artists matching the given name. Limit the results # to the 5 best matches. The offset parameter could be used to page # through the results. # f = ArtistFilter(name=sys.argv[1], limit=10) artistResults = q.getArtists(f) except WebServiceError, e: print 'Error:', e sys.exit(1) if len(sys.argv) > 2: artistResults = [artistResults[int(sys.argv[2])]] else:
for arg in sys.argv[1:]: if os.path.isdir(arg): for root, dirs, files2 in walk(arg): files.extend([os.path.join(root, file) for file in files2]) elif os.path.isfile(arg): files.append(arg) toSubmit = {} for file in files: file = file.decode(encoding) fileId = tp.addFile(file, 0) print ("Adding %s (%s)" % (file, fileId)).encode('ascii', 'ignore') ws = WebService(host=server, username=username, password=password) q = Query(ws, clientId='puid_submit.py-%s' % (__version__,)) analyzed = {} while tp.getNumFiles(): ret, type, fileId, status = tp.getNotification() if not ret: continue print ret,type,fileId,status tr = tp.getTrack(fileId) tr.lock() fileName = tr.getFileName() mdata = tr.getLocalMetadata() tr.unlock()
# $Id: findartist.py 8779 2007-01-07 10:01:52Z matt $ # import sys import logging from musicbrainz2.webservice import Query, ArtistFilter, WebServiceError logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.DEBUG) if len(sys.argv) < 2: print "Usage: findartist.py 'artist name'" sys.exit(1) q = Query() try: # Search for all artists matching the given name. Limit the results # to the 5 best matches. The offset parameter could be used to page # through the results. # f = ArtistFilter(name=sys.argv[1], limit=5) artistResults = q.getArtists(f) except WebServiceError, e: print 'Error:', e sys.exit(1) # No error occurred, so display the results of the search. It consists of # ArtistResult objects, where each contains an artist.
def __init__(self, releaseId): #self.ignoreMissing(True) super(MusicBrainz2AlbumInfo, self).__init__() if isinstance(releaseId, basestring): self.__releaseId = releaseId query = Query() include = ReleaseIncludes(artist=True, tracks=True, releaseEvents=True) release = query.getReleaseById(releaseId, include) elif isinstance(releaseId, Release): release = releaseId self.__releaseId = release.id else: raise Exception( "incorrect type parameter for MusicBrainz2AlbumInfo %s" % releaseId) self.title = release.title # XXX: musicbrainz doesn't have genre info. what to do? #self.genre = 'Musicbrainz' self.artist = release.artist.name date = release.getEarliestReleaseDate() if not date: self.year = 0 else: self.year = int(date[0:4]) self.genre = '' tracks = [] number = 0 self.isVarious = False for track in release.tracks: number += 1 # XXX: get rid of the overcomplicated validation scheme that makes # for these ugly hacks class MBTrackInfo(TrackInfo): '''Represent musicbrainz track information.''' def getPlayLength(self): 'getter' return self.__playLength def setPlayLength(self, playLength): 'setter' self.__playLength = playLength def __init__(self): super(MBTrackInfo, self).__init__() playLength = property(getPlayLength, setPlayLength) track_info = MBTrackInfo() del track_info.validateProps[ track_info.validateProps.index('playLength')] track_info.number = number if track.duration: track_info.playLength = track.duration / 1000.0 else: # XXX: make sure we don't use namebinder relying on track length track_info.playLength = 0 if track.artist: track_info.artist = track.artist.name self.isVarious = True else: track_info.artist = release.artist.name track_info.title = track.title tracks.append(track_info) self.tracks = tracks
import pylast, sys, sqlite3, os, re from musicbrainz2.webservice import Query, ArtistFilter, WebServiceError import musicbrainz2.webservice as ws import musicbrainz2.model as model # Notifo API settings USERNAME = "" API_KEY = "" # NZBMatrix Account Details #API_URL = "http://api.nzbmatrix.com/v1.1/search.php?search=" + searchTerm + #"&catid=22&age=800&username=burningfire&apikey=75390295cf99a1db49c9314c86061405" connection = sqlite3.connect('musicSearch.db') cursor = connection.cursor() q = Query() query = ArtistFilter("Streetlight Manifesto", limit = 2) artistResults = q.getArtists(query) path = "C:\Users\Mongo\Music\iTunes\iTunes Media\Music" unknown = ".+unknown.+" itunesArtist = os.listdir(path) artistExists = 0 def findArtist(): artistName = cursor.execute('SELECT ArtistName FROM Artists') for i in itunesArtist: try: filter = ArtistFilter(i, limit = 2)
import pylast, sys, sqlite3, os, re from musicbrainz2.webservice import Query, ArtistFilter, WebServiceError import musicbrainz2.webservice as ws import musicbrainz2.model as model # Notifo API settings USERNAME = "" API_KEY = "" # NZBMatrix Account Details #API_URL = "http://api.nzbmatrix.com/v1.1/search.php?search=" + searchTerm + #"&catid=22&age=800&username=burningfire&apikey=75390295cf99a1db49c9314c86061405" connection = sqlite3.connect('musicSearch.db') cursor = connection.cursor() q = Query() query = ArtistFilter("Streetlight Manifesto", limit=2) artistResults = q.getArtists(query) path = "C:\Users\Mongo\Music\iTunes\iTunes Media\Music" unknown = ".+unknown.+" itunesArtist = os.listdir(path) artistExists = 0 def findArtist(): artistName = cursor.execute('SELECT ArtistName FROM Artists') for i in itunesArtist: try:
def submit_isrcs(self, tracks2isrcs): q = Query(self.ws) q.submitISRCs(tracks2isrcs)
def __init__(self): self.q = Query()
# $Id$ # import sys import logging from musicbrainz2.webservice import Query, TrackFilter, WebServiceError logging.basicConfig() logger = logging.getLogger() logger.setLevel(logging.DEBUG) if len(sys.argv) < 1: print "Usage: findtrack.py 'puid'" sys.exit(1) q = Query() try: f = TrackFilter(puid=sys.argv[1]) results = q.getTracks(f) except WebServiceError, e: print 'Error:', e sys.exit(1) for result in results: track = result.track print "Score :", result.score print "Id :", track.id print "Title :", track.title print "Artist :", track.artist.name print