1 from mygpo
.cache
import cache_result
2 from mygpo
.counter
import Counter
3 from mygpo
.couch
import get_main_database
4 from mygpo
.db
import QueryParameterMissing
5 from mygpo
.db
.couchdb
.episode
import episodes_by_id
8 @cache_result(timeout
=60)
9 def get_num_listened_episodes(user
):
12 raise QueryParameterMissing('user')
14 db
= get_main_database()
15 r
= db
.view('listeners/by_user_podcast',
16 startkey
= [user
._id
, None],
17 endkey
= [user
._id
, {}],
22 return map(_wrap_num_listened
, r
)
25 def _wrap_num_listened(obj
):
27 podcast
= obj
['key'][1]
28 return (podcast
, count
)
31 @cache_result(timeout
=60)
32 def get_num_played_episodes(user
, since
=None, until
={}):
33 """ Number of played episodes in interval """
36 raise QueryParameterMissing('user')
38 since_str
= since
.strftime('%Y-%m-%d') if since
else None
39 until_str
= until
.strftime('%Y-%m-%d') if until
else {}
41 startkey
= [user
._id
, since_str
]
42 endkey
= [user
._id
, until_str
]
44 db
= get_main_database()
45 res
= db
.view('listeners/by_user',
52 return val
['value'] if val
else 0
57 @cache_result(timeout
=60)
58 def get_latest_episodes(user
, count
=10):
59 """ Returns the latest episodes that the user has accessed """
62 raise QueryParameterMissing('user')
64 startkey
= [user
._id
, {}]
65 endkey
= [user
._id
, None]
67 db
= get_main_database()
68 res
= db
.view('listeners/by_user',
77 keys
= [r
['value'] for r
in res
]
78 return episodes_by_id(keys
)
82 @cache_result(timeout
=60)
83 def get_seconds_played(user
, since
=None, until
={}):
84 """ Returns the number of seconds that the user has listened
86 Can be selected by timespan, podcast and episode """
89 raise QueryParameterMissing('user')
91 since_str
= since
.strftime('%Y-%m-%dT%H:%M:%S') if since
else None
92 until_str
= until
.strftime('%Y-%m-%dT%H:%M:%S') if until
else {}
94 startkey
= [user
._id
, since_str
]
95 endkey
= [user
._id
, until_str
]
97 db
= get_main_database()
98 res
= db
.view('listeners/times_played_by_user',
105 return val
['value'] if val
else 0
109 @cache_result(timeout
=60*60)
110 def suggestions_for_user(user
):
113 raise QueryParameterMissing('user')
115 from mygpo
.users
.models
import Suggestions
116 r
= Suggestions
.view('suggestions/by_user',
130 @cache_result(timeout
=60*60)
131 def user_agent_stats():
132 from mygpo
.users
.models
import User
133 res
= User
.view('clients/by_ua_string',
136 stale
= 'update_after',
139 return Counter(dict((r
['key'], r
['value']) for r
in res
))
143 from mygpo
.users
.models
import User
144 users
= User
.view('users/deleted',
151 def deleted_user_count():
152 total
= User
.view('users/deleted',
155 return list(total
)[0]['value'] if total
else 0
159 @cache_result(timeout
=60)
160 def user_history(user
, start
, length
):
163 raise QueryParameterMissing('user')
168 db
= get_main_database()
169 res
= db
.view('history/by_user',
171 startkey
= [user
._id
, {}],
172 endkey
= [user
._id
, None],
177 return map(_wrap_historyentry
, res
)
180 @cache_result(timeout
=60)
181 def device_history(user
, device
, start
, length
):
184 raise QueryParameterMissing('user')
187 raise QueryParameterMissing('device')
192 db
= get_main_database()
194 res
= db
.view('history/by_device',
196 startkey
= [user
._id
, device
.id, {}],
197 endkey
= [user
._id
, device
.id, None],
202 return map(_wrap_historyentry
, res
)
205 def _wrap_historyentry(action
):
206 from mygpo
.users
.models
import HistoryEntry
207 return HistoryEntry
.from_action_dict(action
['value'])