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

add "most_played" method to the API

parent d503d522
No related branches found
No related tags found
No related merge requests found
......@@ -147,3 +147,11 @@ def more_like_these_json():
results = svcs['searcher'].more_like_these(hashes)
return jsonify(results=results)
@app.route('/json/most_played', methods=['GET'])
@require_auth
def most_played_json():
most_played = [{'sha1': sha1, 'count': count}
for sha1, count in PlayLog.most_played(20)]
return jsonify(results=most_played)
from sqlalchemy import *
from datetime import datetime
from datetime import datetime, timedelta
from djrandom.database import Base
......@@ -47,6 +47,13 @@ class PlayLog(Base):
stamp = Column(DateTime())
prev = Column(Text())
@classmethod
def most_played(cls, n=10):
"""Return the N most played songs."""
one_month_ago = datetime.now() - timedelta(30)
return Session.query(cls.sha1, func.count(cls.sha1).label('count')
).group_by(cls.sha1).order_by('count desc').limit(n)
@classmethod
def generate_tuples(cls, n=2):
"""Yield all the transitions in the playlog.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment