def albums(self, view=None, sort=None, filter=None, filter_value=None, page=None): if view is None: view = "covers" 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(Album) .join(Track, Album.id == Track.album_id) .filter(Track.scanned) .group_by(Album.id)) albums = [] if filter == "yours": remotes_user = remotes.get_user(cherrypy.request.user) album_ids = [] if remotes_user is not None and remotes_user['lastfm'] is not None: for album in remotes_user['lastfm']['top_albums_overall']: album_results = search.query_album(album['name'], exact=True) if len(album_results) > 0: album_ids.append(album_results[0].id) query = query.filter(Album.id.in_(album_ids)) elif filter == "1year": now = datetime.datetime.utcnow() query = query.filter(Album.created > now - datetime.timedelta(days=365)) elif filter == "va": query = (query.join(Artist, Artist.id == Track.artist_id) .having(func.count(distinct(Artist.id)) > 1)) elif filter == "invalid": query = query.filter("invalid is not null") elif filter == "tag": album_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 album in remotes_tag['lastfm']['albums']: album_results = search.query_album(album['name'], exact=True) if len(album_results) > 0: album_ids.append(album_results[0].id) query = query.filter(Album.id.in_(album_ids)) # count before adding order_by() for performance reasons.. total = query.count() pages = math.ceil(total / page_size) if sort == "created": query = query.order_by(Album.created.desc()) elif sort == "updated": query = query.order_by(Album.updated.desc()) elif sort == "seen": query = (query.outerjoin(UserAndAlbum, and_(Album.id == UserAndAlbum.album_id, UserAndAlbum.user_id == cherrypy.request.user.id)) .order_by(UserAndAlbum.seen.desc()) .order_by(Album.updated.desc())) elif sort == "date": query = (query .order_by(Album.date.desc()) .order_by(Album.updated.desc())) elif sort == "random": query = query.order_by(func.rand()) page = None albums = query.limit(page_size).offset(offset).all() for album in albums: remotes.update_album(album) for artist in album.artists: remotes.update_artist(artist) return { 'albums': albums, 'page': page, 'page_size': page_size, 'total': total, 'sort': sort, 'filter': filter, 'filter_value': filter_value, 'pages': pages, "view": view }
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 }