Example #1
0
 def _fetchFromUrl(self, url):
     if not url:
         return None
     try:
         release = None
         rq = request.Request(url=url)
         rq.add_header('Referer', self.referer)
         with request.urlopen(rq) as f:
             mediaTrackCount = 0
             try:
                 s = StringIO((f.read().decode('utf-8')))
                 o = json.load(s)
                 if 'album' not in o:
                     return None
                 r = o['album']
                 tracks = r['tracks']
                 images = r['image']
                 coverUrl = None
                 if images:
                     for image in images:
                         if isEqual(image['size'], 'extralarge'):
                             coverUrl = image['#text']
                 release = Release(title=r['name'])
                 release.trackCount = len(tracks)
                 release.coverUrl = coverUrl
                 if 'id' in r and r['id']:
                     release.lastFMId = r['id']
                 tags = r['tags']
                 if tags:
                     release.tags = []
                     for tag in r['tags']['tag']:
                         tagName = tag['name']
                         if not isInList(release.tags, tagName):
                             release.tags.append(tagName)
                 if tracks:
                     media = ReleaseMedia(releaseMediaNumber=1)
                     media.tracks = []
                     for t in tracks['track']:
                         track = Track(title=t['name'])
                         track.duration = t['duration']
                         track.trackNumber = t['@attr']['rank']
                         if not ([t for t in media.tracks if t.trackNumber == track.trackNumber]):
                             media.tracks.append(track)
                             mediaTrackCount += 1
                     release.media = []
                     release.media.append(media)
                     release.mediaCount = 1
                     release.trackCount = len(media.tracks)
                 if 'wiki' in r and r['wiki']:
                     if 'summary' in r['wiki'] and r['wiki']['summary']:
                         release.lastFMSummary = r['wiki']['summary']
                 if not release.alternateNames:
                     release.alternateNames = []
                 cleanedTitle = createCleanedName(release.title)
                 if cleanedTitle not in release.alternateNames and cleanedTitle != release.title:
                     release.alternateNames.append(cleanedTitle)
                 # Not Valid
                 if mediaTrackCount < 1:
                     release = None
                 return release
             except:
                 self.logger.exception("LastFM: Error In SearchForRelease")
                 pass
         return release
     except:
         self.logger.exception("LastFM: Error In SearchForRelease")
         pass
     return None
Example #2
0
 def lookupReleaseDetails(self, amgId):
     try:
         release = None
         url = "http://api.rovicorp.com/data/v1.1/album/info?apikey=" + self.API_KEY + "&sig=" + str(
                 self._sig()) + "&include=images,releases,styles,tracks&albumid=" + amgId
         rq = request.Request(url=url)
         rq.add_header('Referer', self.referer)
         self.logger.debug("Performing All Music Guide Lookup For Release(s)")
         with request.urlopen(rq) as f:
             try:
                 s = StringIO((f.read().decode('utf-8')))
                 o = json.load(s)
                 if o:
                     if 'status' in o and o['status'] == "ok":
                         album = o['album']
                         if album:
                             release = Release(title=album['title'])
                             release.amgId = amgId
                             release.tags = []
                             if 'flags' in album and album['flags']:
                                 for flag in album['flags']:
                                     if not isInList(release.tags, flag):
                                         release.tags.append(flag)
                             rd = (album['originalReleaseDate'] or '').replace("-??", "")
                             if rd:
                                 release.releaseDate = parseDate(rd)
                             if 'genres' in album and album['genres']:
                                 release.genres = []
                                 for style in album['genres']:
                                     genreName = style['name']
                                     if not ([g for g in release.genres if isEqual(g.name, genreName)]):
                                         release.genres.append(Genre(name=genreName))
                             if 'styles' in album and album['styles']:
                                 release.genres = release.genres or []
                                 for style in album['styles']:
                                     genreName = style['name']
                                     if not ([g for g in release.genres if isEqual(g.name, genreName)]):
                                         release.genres.append(Genre(name=genreName))
                             if 'tracks' in album and album['tracks']:
                                 trackCount = 0
                                 currentTrack = 0
                                 releaseMedia = []
                                 for disc in set(map(lambda x: x['disc'], album['tracks'])):
                                     media = ReleaseMedia(releaseMediaNumber=disc)
                                     media.tracks = []
                                     for amgTrack in (
                                             [t for t in album['tracks'] if isEqual(str(t['disc']), str(disc))]):
                                         currentTrack += 1
                                         track = Track(title=amgTrack['title'])
                                         track.duration = amgTrack['duration']
                                         track.trackNumber = currentTrack
                                         track.releaseMediaNumber = disc
                                         track.amgId = amgTrack['ids']['trackId']
                                         if not [t for t in media.tracks if isEqual(t.title, amgTrack['title'])]:
                                             media.tracks.append(track)
                                     trackCount += len(media.tracks)
                                     releaseMedia.append(media)
                                 release.media = releaseMedia
                                 release.mediaCount = len(releaseMedia)
                             if not release.alternateNames:
                                 release.alternateNames = []
                             cleanedTitle = createCleanedName(release.title)
                             if cleanedTitle not in release.alternateNames and cleanedTitle != release.title:
                                 release.alternateNames.append(cleanedTitle)
             except:
                 self.logger.exception("AllMusicGuide: Error In lookupArtist")
                 pass
         return release
     except:
         self.logger.exception("AllMusicGuide: Error In lookupArtist")
         pass
     return None