2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from functools
import partial
19 from itertools
import imap
, chain
20 from collections
import defaultdict
, namedtuple
21 from datetime
import datetime
22 from importlib
import import_module
24 import dateutil
.parser
31 from django
.http
import HttpResponse
, HttpResponseBadRequest
, Http404
, HttpResponseNotFound
32 from django
.contrib
.sites
.models
import RequestSite
33 from django
.views
.decorators
.csrf
import csrf_exempt
34 from django
.views
.decorators
.cache
import never_cache
35 from django
.utils
.decorators
import method_decorator
36 from django
.views
.generic
.base
import View
37 from django
.conf
import settings
as dsettings
39 from mygpo
.api
.constants
import EPISODE_ACTION_TYPES
, DEVICE_TYPES
40 from mygpo
.api
.httpresponse
import JsonResponse
41 from mygpo
.api
.sanitizing
import sanitize_url
, sanitize_urls
42 from mygpo
.api
.advanced
.directory
import episode_data
, podcast_data
43 from mygpo
.api
.backend
import get_device
, BulkSubscribe
44 from mygpo
.utils
import parse_time
, format_time
, parse_bool
, get_timestamp
, \
46 from mygpo
.decorators
import allowed_methods
, repeat_on_conflict
47 from mygpo
.core
import models
48 from mygpo
.core
.models
import SanitizingRule
, Podcast
49 from mygpo
.core
.tasks
import auto_flattr_episode
50 from mygpo
.users
.models
import PodcastUserState
, EpisodeAction
, \
51 EpisodeUserState
, DeviceDoesNotExist
, DeviceUIDException
, \
52 InvalidEpisodeActionAttributes
53 from mygpo
.users
.settings
import FLATTR_AUTO
54 from mygpo
.core
.json
import JSONDecodeError
55 from mygpo
.api
.basic_auth
import require_valid_user
, check_username
56 from mygpo
.db
.couchdb
import BulkException
, bulk_save_retry
57 from mygpo
.db
.couchdb
.episode
import episode_by_id
, \
58 favorite_episodes_for_user
, episodes_for_podcast
59 from mygpo
.db
.couchdb
.podcast
import podcast_for_url
60 from mygpo
.db
.couchdb
.podcast_state
import subscribed_podcast_ids_by_device
61 from mygpo
.db
.couchdb
.episode_state
import get_podcasts_episode_states
, \
62 episode_state_for_ref_urls
, get_episode_actions
66 logger
= logging
.getLogger(__name__
)
69 # keys that are allowed in episode actions
70 EPISODE_ACTION_KEYS
= ('position', 'episode', 'action', 'device', 'timestamp',
71 'started', 'total', 'podcast')
78 @allowed_methods(['GET', 'POST'])
79 def subscriptions(request
, username
, device_uid
):
82 now_
= get_timestamp(now
)
84 if request
.method
== 'GET':
87 device
= request
.user
.get_device_by_uid(device_uid
)
88 except DeviceDoesNotExist
as e
:
89 return HttpResponseNotFound(str(e
))
91 since_
= request
.GET
.get('since', None)
93 return HttpResponseBadRequest('parameter since missing')
95 since
= datetime
.fromtimestamp(float(since_
))
97 return HttpResponseBadRequest('since-value is not a valid timestamp')
99 changes
= get_subscription_changes(request
.user
, device
, since
, now
)
101 return JsonResponse(changes
)
103 elif request
.method
== 'POST':
104 d
= get_device(request
.user
, device_uid
,
105 request
.META
.get('HTTP_USER_AGENT', ''))
108 return HttpResponseBadRequest('POST data must not be empty')
111 actions
= parse_request_body(request
)
112 except (JSONDecodeError
, UnicodeDecodeError, ValueError) as e
:
113 msg
= (u
'Could not decode subscription update POST data for ' +
114 'user %s: %s') % (username
,
115 request
.body
.decode('ascii', errors
='replace'))
116 logger
.exception(msg
)
117 return HttpResponseBadRequest(msg
)
119 add
= actions
['add'] if 'add' in actions
else []
120 rem
= actions
['remove'] if 'remove' in actions
else []
122 add
= filter(None, add
)
123 rem
= filter(None, rem
)
126 update_urls
= update_subscriptions(request
.user
, d
, add
, rem
)
127 except ValueError, e
:
128 return HttpResponseBadRequest(e
)
130 return JsonResponse({
132 'update_urls': update_urls
,
136 def update_subscriptions(user
, device
, add
, remove
):
140 raise ValueError('can not add and remove %s at the same time' % a
)
142 add_s
= list(sanitize_urls(add
, 'podcast'))
143 rem_s
= list(sanitize_urls(remove
, 'podcast'))
145 assert len(add
) == len(add_s
) and len(remove
) == len(rem_s
)
147 updated_urls
= filter(lambda (a
, b
): a
!= b
, zip(add
+ remove
, add_s
+ rem_s
))
149 add_s
= filter(None, add_s
)
150 rem_s
= filter(None, rem_s
)
152 # If two different URLs (in add and remove) have
153 # been sanitized to the same, we ignore the removal
154 rem_s
= filter(lambda x
: x
not in add_s
, rem_s
)
156 subscriber
= BulkSubscribe(user
, device
)
159 subscriber
.add_action(a
, 'subscribe')
162 subscriber
.add_action(r
, 'unsubscribe')
166 except BulkException
as be
:
167 for err
in be
.errors
:
168 loger
.error('Advanced API: %(username)s: Updating subscription for '
169 '%(podcast_url)s on %(device_uid)s failed: '
170 '%(rerror)s (%(reason)s)'.format(username
=user
.username
,
171 podcast_url
=err
.doc
, device_uid
=device
.uid
,
172 error
=err
.error
, reason
=err
.reason
)
178 def get_subscription_changes(user
, device
, since
, until
):
179 add_urls
, rem_urls
= device
.get_subscription_changes(since
, until
)
180 until_
= get_timestamp(until
)
181 return {'add': add_urls
, 'remove': rem_urls
, 'timestamp': until_
}
188 @allowed_methods(['GET', 'POST'])
189 def episodes(request
, username
, version
=1):
191 version
= int(version
)
193 now_
= get_timestamp(now
)
194 ua_string
= request
.META
.get('HTTP_USER_AGENT', '')
196 if request
.method
== 'POST':
198 actions
= parse_request_body(request
)
199 except (JSONDecodeError
, UnicodeDecodeError, ValueError) as e
:
200 msg
= ('Could not decode episode update POST data for ' +
201 'user %s: %s') % (username
,
202 request
.body
.decode('ascii', errors
='replace'))
203 logger
.exception(msg
)
204 return HttpResponseBadRequest(msg
)
206 logger
.info('start: user %s: %d actions from %s' % (request
.user
._id
, len(actions
), ua_string
))
208 # handle in background
209 if len(actions
) > dsettings
.API_ACTIONS_MAX_NONBG
:
210 bg_handler
= dsettings
.API_ACTIONS_BG_HANDLER
211 if bg_handler
is not None:
213 modname
, funname
= bg_handler
.rsplit('.', 1)
214 mod
= import_module(modname
)
215 fun
= getattr(mod
, funname
)
217 fun(request
.user
, actions
, now
, ua_string
)
219 # TODO: return 202 Accepted
220 return JsonResponse({'timestamp': now_
, 'update_urls': []})
224 update_urls
= update_episodes(request
.user
, actions
, now
, ua_string
)
225 except DeviceUIDException
as e
:
226 logger
.warn('invalid device UID while uploading episode actions for user %s', username
)
227 return HttpResponseBadRequest(str(e
))
229 except InvalidEpisodeActionAttributes
as e
:
230 logger
.exception('invalid episode action attributes while uploading episode actions for user %s: %s' % (username
,))
231 return HttpResponseBadRequest(str(e
))
233 logger
.info('done: user %s: %d actions from %s' % (request
.user
._id
, len(actions
), ua_string
))
234 return JsonResponse({'timestamp': now_
, 'update_urls': update_urls
})
236 elif request
.method
== 'GET':
237 podcast_url
= request
.GET
.get('podcast', None)
238 device_uid
= request
.GET
.get('device', None)
239 since_
= request
.GET
.get('since', None)
240 aggregated
= parse_bool(request
.GET
.get('aggregated', False))
243 since
= int(since_
) if since_
else None
245 return HttpResponseBadRequest('since-value is not a valid timestamp')
248 podcast
= podcast_for_url(podcast_url
)
257 device
= request
.user
.get_device_by_uid(device_uid
)
258 except DeviceDoesNotExist
as e
:
259 return HttpResponseNotFound(str(e
))
264 changes
= get_episode_changes(request
.user
, podcast
, device
, since
,
265 now_
, aggregated
, version
)
267 return JsonResponse(changes
)
271 def convert_position(action
):
272 """ convert position parameter for API 1 compatibility """
273 pos
= getattr(action
, 'position', None)
275 action
.position
= format_time(pos
)
280 def get_episode_changes(user
, podcast
, device
, since
, until
, aggregated
, version
):
282 devices
= dict( (dev
.id, dev
.uid
) for dev
in user
.devices
)
285 if podcast
is not None:
286 args
['podcast_id'] = podcast
.get_id()
288 if device
is not None:
289 args
['device_id'] = device
.id
291 actions
= get_episode_actions(user
._id
, since
, until
, **args
)
294 actions
= imap(convert_position
, actions
)
296 clean_data
= partial(clean_episode_action_data
,
297 user
=user
, devices
=devices
)
299 actions
= map(clean_data
, actions
)
300 actions
= filter(None, actions
)
303 actions
= dict( (a
['episode'], a
) for a
in actions
).values()
305 return {'actions': actions
, 'timestamp': until
}
310 def clean_episode_action_data(action
, user
, devices
):
312 if None in (action
.get('podcast', None), action
.get('episode', None)):
315 if 'device_id' in action
:
316 device_id
= action
['device_id']
317 device_uid
= devices
.get(device_id
)
319 action
['device'] = device_uid
321 del action
['device_id']
323 # remove superfluous keys
324 for x
in action
.keys():
325 if x
not in EPISODE_ACTION_KEYS
:
328 # set missing keys to None
329 for x
in EPISODE_ACTION_KEYS
:
333 if action
['action'] != 'play':
334 if 'position' in action
:
335 del action
['position']
337 if 'total' in action
:
340 if 'started' in action
:
341 del action
['started']
343 if 'playmark' in action
:
344 del action
['playmark']
347 action
['position'] = action
.get('position', False) or 0
355 def update_episodes(user
, actions
, now
, ua_string
):
358 grouped_actions
= defaultdict(list)
360 # group all actions by their episode
361 for action
in actions
:
363 podcast_url
= action
['podcast']
364 podcast_url
= sanitize_append(podcast_url
, 'podcast', update_urls
)
365 if podcast_url
== '':
368 episode_url
= action
['episode']
369 episode_url
= sanitize_append(episode_url
, 'episode', update_urls
)
370 if episode_url
== '':
373 act
= parse_episode_action(action
, user
, update_urls
, now
, ua_string
)
374 grouped_actions
[ (podcast_url
, episode_url
) ].append(act
)
377 auto_flattr_episodes
= []
379 # Prepare the updates for each episode state
382 for (p_url
, e_url
), action_list
in grouped_actions
.iteritems():
383 episode_state
= episode_state_for_ref_urls(user
, p_url
, e_url
)
385 if any(a
['action'] == 'play' for a
in actions
):
386 auto_flattr_episodes
.append(episode_state
.episode
)
388 fun
= partial(update_episode_actions
, action_list
=action_list
)
389 obj_funs
.append( (episode_state
, fun
) )
391 bulk_save_retry(obj_funs
)
393 if user
.get_wksetting(FLATTR_AUTO
):
394 for episode_id
in auto_flattr_episodes
:
395 auto_flattr_episode
.delay(user
, episode_id
)
400 def update_episode_actions(episode_state
, action_list
):
401 """ Adds actions to the episode state and saves if necessary """
403 len1
= len(episode_state
.actions
)
404 episode_state
.add_actions(action_list
)
406 if len(episode_state
.actions
) == len1
:
413 def parse_episode_action(action
, user
, update_urls
, now
, ua_string
):
414 action_str
= action
.get('action', None)
415 if not valid_episodeaction(action_str
):
416 raise Exception('invalid action %s' % action_str
)
418 new_action
= EpisodeAction()
420 new_action
.action
= action
['action']
422 if action
.get('device', False):
423 device
= get_device(user
, action
['device'], ua_string
)
424 new_action
.device
= device
.id
426 if action
.get('timestamp', False):
427 new_action
.timestamp
= dateutil
.parser
.parse(action
['timestamp'])
429 new_action
.timestamp
= now
430 new_action
.timestamp
= new_action
.timestamp
.replace(microsecond
=0)
432 new_action
.upload_timestamp
= get_timestamp(now
)
434 new_action
.started
= action
.get('started', None)
435 new_action
.playmark
= action
.get('position', None)
436 new_action
.total
= action
.get('total', None)
445 # Workaround for mygpoclient 1.0: It uses "PUT" requests
446 # instead of "POST" requests for uploading device settings
447 @allowed_methods(['POST', 'PUT'])
448 def device(request
, username
, device_uid
):
449 d
= get_device(request
.user
, device_uid
,
450 request
.META
.get('HTTP_USER_AGENT', ''))
453 data
= parse_request_body(request
)
454 except (JSONDecodeError
, UnicodeDecodeError, ValueError) as e
:
455 msg
= ('Could not decode device update POST data for ' +
456 'user %s: %s') % (username
,
457 request
.body
.decode('ascii', errors
='replace'))
458 logger
.exception(msg
)
459 return HttpResponseBadRequest(msg
)
461 if 'caption' in data
:
462 if not data
['caption']:
463 return HttpResponseBadRequest('caption must not be empty')
464 d
.name
= data
['caption']
467 if not valid_devicetype(data
['type']):
468 return HttpResponseBadRequest('invalid device type %s' % data
['type'])
469 d
.type = data
['type']
472 request
.user
.update_device(d
)
474 return HttpResponse()
477 def valid_devicetype(type):
478 for t
in DEVICE_TYPES
:
483 def valid_episodeaction(type):
484 for t
in EPISODE_ACTION_TYPES
:
494 @allowed_methods(['GET'])
495 def devices(request
, username
):
496 devices
= filter(lambda d
: not d
.deleted
, request
.user
.devices
)
497 devices
= map(device_data
, devices
)
498 return JsonResponse(devices
)
501 def device_data(device
):
504 caption
= device
.name
,
506 subscriptions
= len(subscribed_podcast_ids_by_device(device
)),
511 def get_podcast_data(podcasts
, domain
, url
):
512 """ Gets podcast data for a URL from a dict of podcasts """
513 podcast
= podcasts
.get(url
)
514 return podcast_data(podcast
, domain
)
517 def get_episode_data(podcasts
, domain
, clean_action_data
, include_actions
, episode_status
):
518 """ Get episode data for an episode status object """
519 podcast_id
= episode_status
.episode
.podcast
520 podcast
= podcasts
.get(podcast_id
, None)
521 t
= episode_data(episode_status
.episode
, domain
, podcast
)
522 t
['status'] = episode_status
.status
524 # include latest action (bug 1419)
525 if include_actions
and episode_status
.action
:
526 t
['action'] = clean_action_data(episode_status
.action
)
532 class DeviceUpdates(View
):
534 @method_decorator(csrf_exempt
)
535 @method_decorator(require_valid_user
)
536 @method_decorator(check_username
)
537 @method_decorator(never_cache
)
538 def get(self
, request
, username
, device_uid
):
540 now_
= get_timestamp(now
)
543 device
= request
.user
.get_device_by_uid(device_uid
)
544 except DeviceDoesNotExist
as e
:
545 return HttpResponseNotFound(str(e
))
547 since_
= request
.GET
.get('since', None)
549 return HttpResponseBadRequest('parameter since missing')
551 since
= datetime
.fromtimestamp(float(since_
))
553 return HttpResponseBadRequest("'since' is not a valid timestamp")
555 include_actions
= parse_bool(request
.GET
.get('include_actions', False))
557 ret
= get_subscription_changes(request
.user
, device
, since
, now
)
558 domain
= RequestSite(request
).domain
560 subscriptions
= list(device
.get_subscribed_podcasts())
562 podcasts
= dict( (p
.url
, p
) for p
in subscriptions
)
563 prepare_podcast_data
= partial(get_podcast_data
, podcasts
, domain
)
565 ret
['add'] = map(prepare_podcast_data
, ret
['add'])
567 devices
= dict( (dev
.id, dev
.uid
) for dev
in request
.user
.devices
)
568 clean_action_data
= partial(clean_episode_action_data
,
569 user
=request
.user
, devices
=devices
)
571 # index subscribed podcasts by their Id for fast access
572 podcasts
= dict( (p
.get_id(), p
) for p
in subscriptions
)
573 prepare_episode_data
= partial(get_episode_data
, podcasts
, domain
,
574 clean_action_data
, include_actions
)
576 episode_updates
= self
.get_episode_updates(request
.user
,
577 subscriptions
, since
)
578 ret
['updates'] = map(prepare_episode_data
, episode_updates
)
580 return JsonResponse(ret
)
583 def get_episode_updates(self
, user
, subscribed_podcasts
, since
,
585 """ Returns the episode updates since the timestamp """
587 EpisodeStatus
= namedtuple('EpisodeStatus', 'episode status action')
593 episode_jobs
= [gevent
.spawn(episodes_for_podcast
, p
, since
,
594 limit
=max_per_podcast
) for p
in subscribed_podcasts
]
595 gevent
.joinall(episode_jobs
)
596 episodes
= chain
.from_iterable(job
.get() for job
in episode_jobs
)
599 episodes
= chain
.from_iterable(episodes_for_podcast(p
, since
,
600 limit
=max_per_podcast
) for p
in subscribed_podcasts
)
603 for episode
in episodes
:
604 episode_status
[episode
._id
] = EpisodeStatus(episode
, 'new', None)
609 e_action_jobs
= [gevent
.spawn(get_podcasts_episode_states
, p
,
610 user
._id
) for p
in subscribed_podcasts
]
611 gevent
.joinall(e_action_jobs
)
612 e_actions
= chain
.from_iterable(job
.get() for job
in e_action_jobs
)
615 e_actions
= chain
.from_iterable(get_podcasts_episode_states(p
,
616 user
._id
) for p
in subscribed_podcasts
)
619 for action
in e_actions
:
620 e_id
= action
['episode_id']
622 if e_id
in episode_status
:
623 episode
= episode_status
[e_id
].episode
625 episode
= episode_by_id(e_id
)
627 episode_status
[e_id
] = EpisodeStatus(episode
, action
['action'],
630 return episode_status
.itervalues()
636 def favorites(request
, username
):
637 favorites
= favorite_episodes_for_user(request
.user
)
638 domain
= RequestSite(request
).domain
639 e_data
= lambda e
: episode_data(e
, domain
)
640 ret
= map(e_data
, favorites
)
641 return JsonResponse(ret
)
644 def sanitize_append(url
, obj_type
, sanitized_list
):
645 urls
= sanitize_url(url
, obj_type
)
647 sanitized_list
.append( (url
, urls
) )