1 from mygpo
.cache
import cache_result
2 from mygpo
.counter
import Counter
5 @cache_result(timeout
=60)
6 def get_num_listened_episodes(user
):
7 db
= get_main_database()
8 r
= db
.view('listeners/by_user_podcast',
9 startkey
= [user
._id
, None],
10 endkey
= [user
._id
, {}],
15 return map(_wrap_num_listened
, r
)
18 def _wrap_num_listened(obj
):
20 podcast
= obj
['key'][1]
21 return (podcast
, count
)
24 @cache_result(timeout
=60)
25 def get_num_played_episodes(self
, since
=None, until
={}):
26 """ Number of played episodes in interval """
28 since_str
= since
.strftime('%Y-%m-%d') if since
else None
29 until_str
= until
.strftime('%Y-%m-%d') if until
else {}
31 startkey
= [self
._id
, since_str
]
32 endkey
= [self
._id
, until_str
]
34 db
= get_main_database()
35 res
= db
.view('listeners/by_user',
42 return val
['value'] if val
else 0
47 @cache_result(timeout
=60)
48 def get_latest_episodes(self
, count
=10):
49 """ Returns the latest episodes that the user has accessed """
51 startkey
= [self
._id
, {}]
52 endkey
= [self
._id
, None]
54 db
= get_main_database()
55 res
= db
.view('listeners/by_user',
64 keys
= [r
['value'] for r
in res
]
65 return list(episodes_by_id(keys
))
69 @cache_result(timeout
=60)
70 def get_seconds_played(self
, since
=None, until
={}):
71 """ Returns the number of seconds that the user has listened
73 Can be selected by timespan, podcast and episode """
75 since_str
= since
.strftime('%Y-%m-%dT%H:%M:%S') if since
else None
76 until_str
= until
.strftime('%Y-%m-%dT%H:%M:%S') if until
else {}
78 startkey
= [self
._id
, since_str
]
79 endkey
= [self
._id
, until_str
]
81 db
= get_main_database()
82 res
= db
.view('listeners/times_played_by_user',
89 return val
['value'] if val
else 0
93 @cache_result(timeout
=60*60)
94 def suggestions_for_user(user
):
95 r
= Suggestions
.view('suggestions/by_user',
109 @cache_result(timeout
=60*60)
110 def user_agent_stats():
111 res
= User
.view('clients/by_ua_string',
114 stale
= 'update_after',
117 return Counter(dict((r
['key'], r
['value']) for r
in res
))
121 users
= User
.view('users/deleted',
128 def deleted_user_count():
129 total
= User
.view('users/deleted',
132 return list(total
)[0]['value'] if total
else 0
136 @cache_result(timeout
=60)
137 def user_history(user
, start
, length
):
138 db
= get_main_database()
139 res
= db
.view('history/by_user',
141 startkey
= [user
._id
, None],
142 endkey
= [user
._id
, {}],
147 return map(_wrap_historyentry
, res
)
150 @cache_result(timeout
=60)
151 def device_history(user
, device
, start
, length
):
152 db
= get_main_database()
154 res
= self
._db
.view('history/by_device',
156 startkey
= [user
._id
, device
.id, None],
157 endkey
= [user
._id
, device
.id, {}],
162 return map(_wrap_historyentry
, res
)
165 def _wrap_historyentry(action
):
166 return HistoryEntry
.from_action_dict(action
['action'])