[Migration] remove / rewrite get_num_played_episodes
[mygpo.git] / mygpo / db / couchdb / user.py
blob9bab46b9a113e28f11b5fdcbef89267b5ba8a7e3
1 from mygpo.cache import cache_result
2 from mygpo.db.couchdb import get_userdata_database, get_single_result
3 from mygpo.db import QueryParameterMissing
6 @cache_result(timeout=60)
7 def get_latest_episode_ids(user, count=10):
8 """ Returns the latest episodes that the user has accessed """
10 if not user:
11 raise QueryParameterMissing('user')
13 startkey = [user.profile.uuid.hex, {}]
14 endkey = [user.profile.uuid.hex, None]
16 udb = get_userdata_database()
17 res = udb.view('listeners/by_user',
18 startkey = startkey,
19 endkey = endkey,
20 include_docs = True,
21 descending = True,
22 limit = count,
23 reduce = False,
24 stale = 'update_after',
27 return [r['value'] for r in res]
31 @cache_result(timeout=60)
32 def get_seconds_played(user, since=None, until={}):
33 """ Returns the number of seconds that the user has listened
35 Can be selected by timespan, podcast and episode """
37 if not user:
38 raise QueryParameterMissing('user')
40 since_str = since.strftime('%Y-%m-%dT%H:%M:%S') if since else None
41 until_str = until.strftime('%Y-%m-%dT%H:%M:%S') if until else {}
43 startkey = [user.profile.uuid.hex, since_str]
44 endkey = [user.profile.uuid.hex, until_str]
46 udb = get_userdata_database()
47 val = get_single_result(udb, 'listeners/times_played_by_user',
48 startkey = startkey,
49 endkey = endkey,
50 reduce = True,
51 stale = 'update_after',
54 return val['value'] if val else 0