def get_artist(self, artist_name): try: network = self.get_network() artist = network.get_artist_info(artist_name) similars = [] count = 0 for similar in network.get_artist_similar(artist_name, 100): name = similar["name"] results = search.query_artist(name, exact=True) if len(results) > 0: similars.append({"name": results[0].name, "slug": results[0].slug}) count += 1 if count >= 20: break tags = network.get_artist_top_tags(artist_name) return { "url": artist["url"], "bio": artist["bio"], "cover": artist["cover"], "listeners": artist["listeners"], "tags": tags[0:20], "similar": similars, } except LastfmError as error: log('Failed to get artist "%s": %s' % (artist_name, error))
def api(self, type, query): if type == "artist": entities = search.query_artist(query) elif type == "album": entities = search.query_album(query) elif type == "track": entities = search.query_track(query) else: raise cherrypy.NotFound() return [{"name": entity.name} for entity in entities]
def _get_top_artists(self, user_id, start_date, end_date): top_artists = [] for artist_name, count in library_dao.get_listened_artist_name_count(user_id, start_date, end_date, limit=50): artists = search.query_artist(artist_name, exact=True) if len(artists) > 0: artist = artists[0] else: artist = None top_artists.append((artist, artist_name, count)) return top_artists
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 artists(self, sort=None, filter=None, filter_value=None, page=None): if sort is None: sort = "created" if filter is None: filter = "none" if filter_value is None: filter_value = "" if page is None: page = 1 page = int(page) page_size = 24 offset = page_size * (page - 1) query = (get_database() .query(Artist) .join(Track, Artist.id == Track.artist_id) .filter(Track.scanned) .group_by(Artist.id)) if sort == "created": query = query.order_by(Artist.created.desc()) elif sort == "updated": query = query.order_by(Artist.updated.desc()) elif sort == "random": query = query.order_by(func.rand()) page = None if filter == "yours": remotes_user = remotes.get_user(cherrypy.request.user) artist_ids = [] if remotes_user is not None and remotes_user['lastfm'] is not None: for artist in remotes_user['lastfm']['top_artists_overall']: artist_results = search.query_artist(artist['name'], exact=True) if len(artist_results) > 0: artist_ids.append(artist_results[0].id) query = query.filter(Artist.id.in_(artist_ids)) elif filter == "invalid": query = query.filter("invalid is not null") elif filter == "tag": artist_ids = [] if filter_value != "": remotes.update_tag(filter_value) remotes_tag = remotes.get_tag(filter_value) if remotes_tag is not None and remotes_tag['lastfm'] is not None: for artist in remotes_tag['lastfm']['artists']: artist_results = search.query_artist(artist['name'], exact=True) if len(artist_results) > 0: artist_ids.append(artist_results[0].id) query = query.filter(Artist.id.in_(artist_ids)) total = query.count() pages = math.ceil(total / page_size) artists = query.limit(page_size).offset(offset).all() for artist in artists: remotes.update_artist(artist) return { 'artists': artists, 'page': page, 'page_size': page_size, 'total': total, 'sort': sort, 'filter': filter, 'filter_value': filter_value, 'pages': pages }
def default(self, query=None, type=None): artists = [] albums = [] tracks = [] track_ids = [] hierarchy = None album_track_ids = set() recent_searches = [] if query is not None: albums = None tracks = None # only search for artists if type == 'artist': artists = search.query_artist(query, exact=True) albums = [] tracks = [] else: artists = search.query_artist(query) if albums is None: albums = search.query_album(query) if tracks is None: tracks = search.query_track(query) for artist in artists: remotes.update_artist(artist) for album in albums: remotes.update_album(album) for track in tracks: track_ids.append(track.id) remotes.update_track(track) entities = artists + albums + tracks if len(entities) == 1: for artist in artists: raise HTTPRedirect('/%s' % artist.slug) for album in albums: raise HTTPRedirect('/%s/%s' % (album.artists[0].slug, album.slug)) for track in tracks: raise HTTPRedirect('/library/track/%s' % track.slug) if cache.has(Search.CACHE_RECENT_KEY): recent_searches = cache.get(Search.CACHE_RECENT_KEY) else: cache.set(Search.CACHE_RECENT_KEY, recent_searches) if type is None and len(entities) > 0: if len(recent_searches) == 0 or query != recent_searches[0][0]: recent_searches.insert(0, (query, datetime.datetime.now(), cherrypy.request.user.login)) if len(recent_searches) > Search.MAX_RECENT: recent_searches.pop() entities = sorted(entities, key=lambda entity: entity._SEARCH_SCORE, reverse=True) hierarchy = Library._produce_track_hierarchy(entities) for key, result_artist in hierarchy['artists'].items(): for key, result_album in result_artist['albums'].items(): for track_id in library_dao.get_track_ids_by_album_id(result_album['entity'].id): album_track_ids.add(track_id) return { 'query': query, 'hierarchy': hierarchy, 'tracks': tracks, 'albums': albums, 'artists': artists, 'track_ids': track_ids, 'album_track_ids': list(album_track_ids), 'recent_searches': recent_searches }