def get_artist_images(self, artist): # import here to avoid circular import from opmuse.remotes import remotes remotes_artist = remotes.get_artist(artist) tags = self._tags(remotes_artist) query = self._query('"%s"' % artist.name, tags) return self.images(query)
def get_artist_search(self, artist): # import here to avoid circular import from opmuse.remotes import remotes remotes_artist = remotes.get_artist(artist) tags = self._tags(remotes_artist) query = self._query('"%s"' % artist.name, tags) return self.search("-discogs.com -wikipedia.org -last.fm %s" % query)
def track(self, slug): track = library_dao.get_track_by_slug(slug) if track is None: raise cherrypy.NotFound() remotes.update_track(track) remotes_album = remotes_artist = None if track.album is not None: remotes.update_album(track.album) remotes_album = remotes.get_album(track.album) if track.artist is not None: remotes.update_artist(track.artist) remotes_artist = remotes.get_artist(track.artist) remotes_track = remotes.get_track(track) if track.artist is not None: artist_listened_tuples = self.get_artist_listened_tuples(track.artist.name) if track.album is not None: album_listened_tuples = self.get_album_listened_tuples(track.artist.name, track.album.name) else: album_listened_tuples = None else: artist_listened_tuples = None album_listened_tuples = None return { 'track': track, 'remotes_artist': remotes_artist, 'remotes_album': remotes_album, 'remotes_track': remotes_track, 'album_listened_tuples': album_listened_tuples, 'artist_listened_tuples': artist_listened_tuples, }
def fetch_artist_cover(self, artist_id): try: artist = get_database().query(Artist).filter_by(id=artist_id).one() except NoResultFound: return remotes_artist = None tries = 0 # try and sleep until we get the remotes_artist. while remotes_artist is None and tries < 8: remotes_artist = remotes.get_artist(artist) tries += 1 if remotes_artist is None: # exponential backoff time.sleep(tries ** 2) lastfm_artist = None if remotes_artist is not None: lastfm_artist = remotes_artist["lastfm"] if lastfm_artist is None or lastfm_artist["cover"] is None: google_images = google.get_artist_images(artist) if google_images is not None: urls = google_images else: return else: urls = [lastfm_artist["cover"]] cover = None for url in urls: cover, resize_cover, resize_cover_large, cover_ext = self.retrieve_and_resize(url) if cover is None: continue if cover is None: return track_dirs = set() for track in artist.tracks: for path in track.paths: track_dirs.add(os.path.dirname(path.path)) for track_dir in track_dirs: if not os.path.exists(track_dir): os.makedirs(track_dir) cover_dest = os.path.join(track_dir, ("%s%s" % (artist.slug, cover_ext)).encode("utf8")) if not os.path.exists(cover_dest): with open(cover_dest, "wb") as file: file.write(cover) artist.cover_path = cover_dest import mmh3 artist.cover = resize_cover artist.cover_large = resize_cover_large artist.cover_hash = base64.b64encode(mmh3.hash_bytes(artist.cover)) try: get_database().commit() except StaleDataError: # artist was removed, ignore get_database().rollback() return ws.emit_all("covers.artist.update", artist.id)
def artist(self, slug): artist = library_dao.get_artist_by_slug(slug) if artist is None: raise cherrypy.NotFound() album_group_order = { 'default': 0, 'ep': 1, 'split': 2, 'live': 3, 'va': 4 } album_groups = {} remotes.update_artist(artist) for album in artist.albums: if re.search(r'\blive\b', album.name.lower()): if 'live' not in album_groups: album_groups['live'] = { 'title': 'Live', 'albums': [] } album_groups['live']['albums'].append(album) elif album.is_split: if 'split' not in album_groups: album_groups['split'] = { 'title': 'Splits', 'albums': [] } album_groups['split']['albums'].append(album) elif album.is_va: if 'va' not in album_groups: album_groups['va'] = { 'title': 'Various Artists', 'albums': [] } album_groups['va']['albums'].append(album) elif album.is_ep: if 'ep' not in album_groups: album_groups['ep'] = { 'title': 'Singles & EPs', 'albums': [] } album_groups['ep']['albums'].append(album) else: if 'default' not in album_groups: album_groups['default'] = { 'title': 'Albums', 'albums': [] } album_groups['default']['albums'].append(album) remotes.update_album(album) album_groups = OrderedDict(sorted(album_groups.items(), key=lambda album_group: album_group_order[album_group[0]])) remotes_artist = remotes.get_artist(artist) same_artists = set() for artist_result in search.query_artist(artist.name, exact_metaphone=True): if artist != artist_result: same_artists.add(artist_result) dir_tracks = self._dir_tracks(artist.no_album_tracks) remotes_user = remotes.get_user(cherrypy.request.user) artist_listened_tuples = self.get_artist_listened_tuples(artist.name) return { 'remotes_user': remotes_user, 'dir_tracks': dir_tracks, 'artist': artist, 'album_groups': album_groups, 'remotes_artist': remotes_artist, 'same_artists': same_artists, 'artist_listened_tuples': artist_listened_tuples }
def album(self, artist_slug, album_slug): try: album = (get_database().query(Album) # _dir_tracks() uses paths .options(joinedload(Album.tracks, Track.paths)) .filter_by(slug=album_slug).one()) except NoResultFound: raise cherrypy.NotFound() # always update seen, True means now album.seen = True remotes.update_album(album) remotes_artists = [] for artist in album.artists: remotes.update_artist(artist) remotes_artist = remotes.get_artist(artist) if remotes_artist is not None: remotes_artists.append(remotes_artist) disc_nos = {} for track in album.tracks: remotes.update_track(track) disc = '' if track.disc is None else track.disc if disc not in disc_nos: disc_nos[disc] = [] if track.number is not None: # extract max track no, '01' will become 1, '01/10' will become 10 disc_nos[disc].append(int(re.search('\d+', track.number).group())) else: disc_nos[disc].append(None) album_disc_nos = [] for disc, numbers in disc_nos.items(): max_number = max([number for number in numbers if number is not None], default=None) if max_number is not None: album_disc_nos.append((disc, len(numbers), max_number)) album_disc_nos = sorted(album_disc_nos, key=lambda album_disc_no: album_disc_no[0]) remotes_album = remotes.get_album(album) dir_tracks = self._dir_tracks(album.tracks) if len(album.artists) > 0: artist_name = album.artists[0].name else: artist_name = None album_listened_tuples = self.get_album_listened_tuples(artist_name, album.name) artist_listened_tuples = self.get_artist_listened_tuples(artist_name) return { 'album_disc_nos': album_disc_nos, 'album': album, 'dir_tracks': dir_tracks, 'remotes_artists': remotes_artists, 'remotes_album': remotes_album, 'album_listened_tuples': album_listened_tuples, 'artist_listened_tuples': artist_listened_tuples, }