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
45 from mygpo
.decorators
import allowed_methods
, repeat_on_conflict
46 from mygpo
.core
import models
47 from mygpo
.core
.models
import SanitizingRule
, Podcast
48 from mygpo
.core
.tasks
import auto_flattr_episode
49 from mygpo
.users
.models
import PodcastUserState
, EpisodeAction
, \
50 EpisodeUserState
, DeviceDoesNotExist
, DeviceUIDException
, \
51 InvalidEpisodeActionAttributes
52 from mygpo
.users
.settings
import FLATTR_AUTO
53 from mygpo
.core
.json
import json
, JSONDecodeError
54 from mygpo
.api
.basic_auth
import require_valid_user
, check_username
55 from mygpo
.db
.couchdb
import BulkException
, bulk_save_retry
56 from mygpo
.db
.couchdb
.episode
import episode_by_id
, \
57 favorite_episodes_for_user
, episodes_for_podcast
58 from mygpo
.db
.couchdb
.podcast
import podcast_for_url
59 from mygpo
.db
.couchdb
.podcast_state
import subscribed_podcast_ids_by_device
60 from mygpo
.db
.couchdb
.episode_state
import get_podcasts_episode_states
, \
61 episode_state_for_ref_urls
, get_episode_actions
65 logger
= logging
.getLogger(__name__
)
68 # keys that are allowed in episode actions
69 EPISODE_ACTION_KEYS
= ('position', 'episode', 'action', 'device', 'timestamp',
70 'started', 'total', 'podcast')
77 @allowed_methods(['GET', 'POST'])
78 def subscriptions(request
, username
, device_uid
):
81 now_
= get_timestamp(now
)
83 if request
.method
== 'GET':
86 device
= request
.user
.get_device_by_uid(device_uid
)
87 except DeviceDoesNotExist
as e
:
88 return HttpResponseNotFound(str(e
))
90 since_
= request
.GET
.get('since', None)
92 return HttpResponseBadRequest('parameter since missing')
94 since
= datetime
.fromtimestamp(float(since_
))
96 return HttpResponseBadRequest('since-value is not a valid timestamp')
98 changes
= get_subscription_changes(request
.user
, device
, since
, now
)
100 return JsonResponse(changes
)
102 elif request
.method
== 'POST':
103 d
= get_device(request
.user
, device_uid
,
104 request
.META
.get('HTTP_USER_AGENT', ''))
107 return HttpResponseBadRequest('POST data must not be empty')
109 actions
= json
.loads(request
.body
)
110 add
= actions
['add'] if 'add' in actions
else []
111 rem
= actions
['remove'] if 'remove' in actions
else []
113 add
= filter(None, add
)
114 rem
= filter(None, rem
)
117 update_urls
= update_subscriptions(request
.user
, d
, add
, rem
)
118 except ValueError, e
:
119 return HttpResponseBadRequest(e
)
121 return JsonResponse({
123 'update_urls': update_urls
,
127 def update_subscriptions(user
, device
, add
, remove
):
131 raise ValueError('can not add and remove %s at the same time' % a
)
133 add_s
= list(sanitize_urls(add
, 'podcast'))
134 rem_s
= list(sanitize_urls(remove
, 'podcast'))
136 assert len(add
) == len(add_s
) and len(remove
) == len(rem_s
)
138 updated_urls
= filter(lambda (a
, b
): a
!= b
, zip(add
+ remove
, add_s
+ rem_s
))
140 add_s
= filter(None, add_s
)
141 rem_s
= filter(None, rem_s
)
143 # If two different URLs (in add and remove) have
144 # been sanitized to the same, we ignore the removal
145 rem_s
= filter(lambda x
: x
not in add_s
, rem_s
)
147 subscriber
= BulkSubscribe(user
, device
)
150 subscriber
.add_action(a
, 'subscribe')
153 subscriber
.add_action(r
, 'unsubscribe')
157 except BulkException
as be
:
158 for err
in be
.errors
:
159 loger
.error('Advanced API: %(username)s: Updating subscription for '
160 '%(podcast_url)s on %(device_uid)s failed: '
161 '%(rerror)s (%(reason)s)'.format(username
=user
.username
,
162 podcast_url
=err
.doc
, device_uid
=device
.uid
,
163 error
=err
.error
, reason
=err
.reason
)
169 def get_subscription_changes(user
, device
, since
, until
):
170 add_urls
, rem_urls
= device
.get_subscription_changes(since
, until
)
171 until_
= get_timestamp(until
)
172 return {'add': add_urls
, 'remove': rem_urls
, 'timestamp': until_
}
179 @allowed_methods(['GET', 'POST'])
180 def episodes(request
, username
, version
=1):
182 version
= int(version
)
184 now_
= get_timestamp(now
)
185 ua_string
= request
.META
.get('HTTP_USER_AGENT', '')
187 if request
.method
== 'POST':
189 actions
= json
.loads(request
.body
)
190 except (JSONDecodeError
, UnicodeDecodeError) as e
:
191 msg
= 'Advanced API: could not decode episode update POST data for user %s: %s' % (username
, e
)
193 return HttpResponseBadRequest(msg
)
195 logger
.info('start: user %s: %d actions from %s' % (request
.user
._id
, len(actions
), ua_string
))
197 # handle in background
198 if len(actions
) > dsettings
.API_ACTIONS_MAX_NONBG
:
199 bg_handler
= dsettings
.API_ACTIONS_BG_HANDLER
200 if bg_handler
is not None:
202 modname
, funname
= bg_handler
.rsplit('.', 1)
203 mod
= import_module(modname
)
204 fun
= getattr(mod
, funname
)
206 fun(request
.user
, actions
, now
, ua_string
)
208 # TODO: return 202 Accepted
209 return JsonResponse({'timestamp': now_
, 'update_urls': []})
213 update_urls
= update_episodes(request
.user
, actions
, now
, ua_string
)
214 except DeviceUIDException
as e
:
215 logger
.warn('invalid device UID while uploading episode actions for user %s: %s' % (username
, e
))
216 return HttpResponseBadRequest(str(e
))
218 except InvalidEpisodeActionAttributes
as e
:
219 logger
.warn('invalid episode action attributes while uploading episode actions for user %s: %s' % (username
, e
))
220 return HttpResponseBadRequest(str(e
))
222 logger
.info('done: user %s: %d actions from %s' % (request
.user
._id
, len(actions
), ua_string
))
223 return JsonResponse({'timestamp': now_
, 'update_urls': update_urls
})
225 elif request
.method
== 'GET':
226 podcast_url
= request
.GET
.get('podcast', None)
227 device_uid
= request
.GET
.get('device', None)
228 since_
= request
.GET
.get('since', None)
229 aggregated
= parse_bool(request
.GET
.get('aggregated', False))
232 since
= int(since_
) if since_
else None
234 return HttpResponseBadRequest('since-value is not a valid timestamp')
237 podcast
= podcast_for_url(podcast_url
)
246 device
= request
.user
.get_device_by_uid(device_uid
)
247 except DeviceDoesNotExist
as e
:
248 return HttpResponseNotFound(str(e
))
253 changes
= get_episode_changes(request
.user
, podcast
, device
, since
,
254 now_
, aggregated
, version
)
256 return JsonResponse(changes
)
260 def convert_position(action
):
261 """ convert position parameter for API 1 compatibility """
262 pos
= getattr(action
, 'position', None)
264 action
.position
= format_time(pos
)
269 def get_episode_changes(user
, podcast
, device
, since
, until
, aggregated
, version
):
271 devices
= dict( (dev
.id, dev
.uid
) for dev
in user
.devices
)
274 if podcast
is not None:
275 args
['podcast_id'] = podcast
.get_id()
277 if device
is not None:
278 args
['device_id'] = device
.id
280 actions
= get_episode_actions(user
._id
, since
, until
, **args
)
283 actions
= imap(convert_position
, actions
)
285 clean_data
= partial(clean_episode_action_data
,
286 user
=user
, devices
=devices
)
288 actions
= map(clean_data
, actions
)
289 actions
= filter(None, actions
)
292 actions
= dict( (a
['episode'], a
) for a
in actions
).values()
294 return {'actions': actions
, 'timestamp': until
}
299 def clean_episode_action_data(action
, user
, devices
):
301 if None in (action
.get('podcast', None), action
.get('episode', None)):
304 if 'device_id' in action
:
305 device_id
= action
['device_id']
306 device_uid
= devices
.get(device_id
)
308 action
['device'] = device_uid
310 del action
['device_id']
312 # remove superfluous keys
313 for x
in action
.keys():
314 if x
not in EPISODE_ACTION_KEYS
:
317 # set missing keys to None
318 for x
in EPISODE_ACTION_KEYS
:
322 if action
['action'] != 'play':
323 if 'position' in action
:
324 del action
['position']
326 if 'total' in action
:
329 if 'started' in action
:
330 del action
['started']
332 if 'playmark' in action
:
333 del action
['playmark']
336 action
['position'] = action
.get('position', False) or 0
344 def update_episodes(user
, actions
, now
, ua_string
):
347 grouped_actions
= defaultdict(list)
349 # group all actions by their episode
350 for action
in actions
:
352 podcast_url
= action
['podcast']
353 podcast_url
= sanitize_append(podcast_url
, 'podcast', update_urls
)
354 if podcast_url
== '':
357 episode_url
= action
['episode']
358 episode_url
= sanitize_append(episode_url
, 'episode', update_urls
)
359 if episode_url
== '':
362 act
= parse_episode_action(action
, user
, update_urls
, now
, ua_string
)
363 grouped_actions
[ (podcast_url
, episode_url
) ].append(act
)
366 auto_flattr_episodes
= []
368 # Prepare the updates for each episode state
371 for (p_url
, e_url
), action_list
in grouped_actions
.iteritems():
372 episode_state
= episode_state_for_ref_urls(user
, p_url
, e_url
)
374 if any(a
['action'] == 'play' for a
in actions
):
375 auto_flattr_episodes
.append(episode_state
.episode
)
377 fun
= partial(update_episode_actions
, action_list
=action_list
)
378 obj_funs
.append( (episode_state
, fun
) )
380 bulk_save_retry(obj_funs
)
382 if user
.get_wksetting(FLATTR_AUTO
):
383 for episode_id
in auto_flattr_episodes
:
384 auto_flattr_episode
.delay(user
, episode_id
)
389 def update_episode_actions(episode_state
, action_list
):
390 """ Adds actions to the episode state and saves if necessary """
392 len1
= len(episode_state
.actions
)
393 episode_state
.add_actions(action_list
)
395 if len(episode_state
.actions
) == len1
:
402 def parse_episode_action(action
, user
, update_urls
, now
, ua_string
):
403 action_str
= action
.get('action', None)
404 if not valid_episodeaction(action_str
):
405 raise Exception('invalid action %s' % action_str
)
407 new_action
= EpisodeAction()
409 new_action
.action
= action
['action']
411 if action
.get('device', False):
412 device
= get_device(user
, action
['device'], ua_string
)
413 new_action
.device
= device
.id
415 if action
.get('timestamp', False):
416 new_action
.timestamp
= dateutil
.parser
.parse(action
['timestamp'])
418 new_action
.timestamp
= now
419 new_action
.timestamp
= new_action
.timestamp
.replace(microsecond
=0)
421 new_action
.upload_timestamp
= get_timestamp(now
)
423 new_action
.started
= action
.get('started', None)
424 new_action
.playmark
= action
.get('position', None)
425 new_action
.total
= action
.get('total', None)
434 # Workaround for mygpoclient 1.0: It uses "PUT" requests
435 # instead of "POST" requests for uploading device settings
436 @allowed_methods(['POST', 'PUT'])
437 def device(request
, username
, device_uid
):
438 d
= get_device(request
.user
, device_uid
,
439 request
.META
.get('HTTP_USER_AGENT', ''))
441 data
= json
.loads(request
.body
)
443 if 'caption' in data
:
444 if not data
['caption']:
445 return HttpResponseBadRequest('caption must not be empty')
446 d
.name
= data
['caption']
449 if not valid_devicetype(data
['type']):
450 return HttpResponseBadRequest('invalid device type %s' % data
['type'])
451 d
.type = data
['type']
454 request
.user
.update_device(d
)
456 return HttpResponse()
459 def valid_devicetype(type):
460 for t
in DEVICE_TYPES
:
465 def valid_episodeaction(type):
466 for t
in EPISODE_ACTION_TYPES
:
476 @allowed_methods(['GET'])
477 def devices(request
, username
):
478 devices
= filter(lambda d
: not d
.deleted
, request
.user
.devices
)
479 devices
= map(device_data
, devices
)
480 return JsonResponse(devices
)
483 def device_data(device
):
486 caption
= device
.name
,
488 subscriptions
= len(subscribed_podcast_ids_by_device(device
)),
493 def get_podcast_data(podcasts
, domain
, url
):
494 """ Gets podcast data for a URL from a dict of podcasts """
495 podcast
= podcasts
.get(url
)
496 return podcast_data(podcast
, domain
)
499 def get_episode_data(podcasts
, domain
, clean_action_data
, include_actions
, episode_status
):
500 """ Get episode data for an episode status object """
501 podcast_id
= episode_status
.episode
.podcast
502 podcast
= podcasts
.get(podcast_id
, None)
503 t
= episode_data(episode_status
.episode
, domain
, podcast
)
504 t
['status'] = episode_status
.status
506 # include latest action (bug 1419)
507 if include_actions
and episode_status
.action
:
508 t
['action'] = clean_action_data(episode_status
.action
)
514 class DeviceUpdates(View
):
516 @method_decorator(csrf_exempt
)
517 @method_decorator(require_valid_user
)
518 @method_decorator(check_username
)
519 @method_decorator(never_cache
)
520 def get(self
, request
, username
, device_uid
):
522 now_
= get_timestamp(now
)
525 device
= request
.user
.get_device_by_uid(device_uid
)
526 except DeviceDoesNotExist
as e
:
527 return HttpResponseNotFound(str(e
))
529 since_
= request
.GET
.get('since', None)
531 return HttpResponseBadRequest('parameter since missing')
533 since
= datetime
.fromtimestamp(float(since_
))
535 return HttpResponseBadRequest("'since' is not a valid timestamp")
537 include_actions
= parse_bool(request
.GET
.get('include_actions', False))
539 ret
= get_subscription_changes(request
.user
, device
, since
, now
)
540 domain
= RequestSite(request
).domain
542 subscriptions
= list(device
.get_subscribed_podcasts())
544 podcasts
= dict( (p
.url
, p
) for p
in subscriptions
)
545 prepare_podcast_data
= partial(get_podcast_data
, podcasts
, domain
)
547 ret
['add'] = map(prepare_podcast_data
, ret
['add'])
549 devices
= dict( (dev
.id, dev
.uid
) for dev
in request
.user
.devices
)
550 clean_action_data
= partial(clean_episode_action_data
,
551 user
=request
.user
, devices
=devices
)
553 # index subscribed podcasts by their Id for fast access
554 podcasts
= dict( (p
.get_id(), p
) for p
in subscriptions
)
555 prepare_episode_data
= partial(get_episode_data
, podcasts
, domain
,
556 clean_action_data
, include_actions
)
558 episode_updates
= self
.get_episode_updates(request
.user
,
559 subscriptions
, since
)
560 ret
['updates'] = map(prepare_episode_data
, episode_updates
)
562 return JsonResponse(ret
)
565 def get_episode_updates(self
, user
, subscribed_podcasts
, since
,
567 """ Returns the episode updates since the timestamp """
569 EpisodeStatus
= namedtuple('EpisodeStatus', 'episode status action')
575 episode_jobs
= [gevent
.spawn(episodes_for_podcast
, p
, since
,
576 limit
=max_per_podcast
) for p
in subscribed_podcasts
]
577 gevent
.joinall(episode_jobs
)
578 episodes
= chain
.from_iterable(job
.get() for job
in episode_jobs
)
581 episodes
= chain
.from_iterable(episodes_for_podcast(p
, since
,
582 limit
=max_per_podcast
) for p
in subscribed_podcasts
)
585 for episode
in episodes
:
586 episode_status
[episode
._id
] = EpisodeStatus(episode
, 'new', None)
591 e_action_jobs
= [gevent
.spawn(get_podcasts_episode_states
, p
,
592 user
._id
) for p
in subscribed_podcasts
]
593 gevent
.joinall(e_action_jobs
)
594 e_actions
= chain
.from_iterable(job
.get() for job
in e_action_jobs
)
597 e_actions
= chain
.from_iterable(get_podcasts_episode_states(p
,
598 user
._id
) for p
in subscribed_podcasts
)
601 for action
in e_actions
:
602 e_id
= action
['episode_id']
604 if e_id
in episode_status
:
605 episode
= episode_status
[e_id
].episode
607 episode
= episode_by_id(e_id
)
609 episode_status
[e_id
] = EpisodeStatus(episode
, action
['action'],
612 return episode_status
.itervalues()
618 def favorites(request
, username
):
619 favorites
= favorite_episodes_for_user(request
.user
)
620 domain
= RequestSite(request
).domain
621 e_data
= lambda e
: episode_data(e
, domain
)
622 ret
= map(e_data
, favorites
)
623 return JsonResponse(ret
)
626 def sanitize_append(url
, obj_type
, sanitized_list
):
627 urls
= sanitize_url(url
, obj_type
)
629 sanitized_list
.append( (url
, urls
) )