Skip to content
Snippets Groups Projects
Commit a87951a8 authored by ale's avatar ale
Browse files

add a never_played API method

parent 525b041e
No related branches found
No related tags found
No related merge requests found
...@@ -156,6 +156,14 @@ def most_played_json(): ...@@ -156,6 +156,14 @@ def most_played_json():
return jsonify(results=most_played) return jsonify(results=most_played)
@app.route('/json/never_played', methods=['GET'])
@require_auth
def never_played_json():
n = int(request.args.get('n', 20))
never_played = MP3.never_played(n)
return jsonify(results=never_played)
@app.route('/json/last_uploaded', methods=['GET']) @app.route('/json/last_uploaded', methods=['GET'])
@require_auth @require_auth
def last_uploaded_json(): def last_uploaded_json():
......
...@@ -54,13 +54,16 @@ class MP3(Base): ...@@ -54,13 +54,16 @@ class MP3(Base):
desc(cls.uploaded_at)).limit(n) desc(cls.uploaded_at)).limit(n)
@classmethod @classmethod
def get_random_songs(cls, n=10): def get_random_songs(cls, n=10, where_clause=None):
"""Return N completely random songs.""" """Return N completely random songs."""
results = [] results = []
num_songs = cls.query.filter_by(state=cls.READY).count() if where_clause is None:
where_clause = (cls.state == cls.READY)
num_songs = cls.query.filter(where_clause).count()
fraction = float(n) / num_songs fraction = float(n) / num_songs
where_clause = where_clause & (func.rand() < fraction)
while len(results) < n: while len(results) < n:
tmprows = Session.query(cls.sha1).filter(func.rand() < fraction).limit(n) tmprows = Session.query(cls.sha1).filter(where_clause).limit(n)
for row in tmprows: for row in tmprows:
results.append(row[0]) results.append(row[0])
return results return results
...@@ -70,6 +73,11 @@ class MP3(Base): ...@@ -70,6 +73,11 @@ class MP3(Base):
return cls.query.filter_by( return cls.query.filter_by(
state=cls.READY, artist=artist, album=album) state=cls.READY, artist=artist, album=album)
@classmethod
def never_played(cls, n=10):
"""Return N random songs that were never played."""
return cls.get_random_songs(n, where_clause=(cls.play_count == 0))
class PlayLog(Base): class PlayLog(Base):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment