[API] handle empty URLs in episode action upload
[mygpo.git] / mygpo / api / advanced / __init__.py
blobdd4d83655e93e73a8251e613c3a9ebd45d85bb11
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
20 from collections import defaultdict
21 from datetime import datetime
22 from importlib import import_module
24 import dateutil.parser
26 from django.http import (HttpResponse, HttpResponseBadRequest, Http404,
27 HttpResponseNotFound, )
28 from django.core.exceptions import ValidationError
29 from django.contrib.sites.requests import RequestSite
30 from django.views.decorators.csrf import csrf_exempt
31 from django.views.decorators.cache import never_cache
32 from django.conf import settings as dsettings
33 from django.shortcuts import get_object_or_404
35 from mygpo.podcasts.models import Podcast, Episode
36 from mygpo.subscriptions.models import Subscription
37 from mygpo.api.constants import EPISODE_ACTION_TYPES
38 from mygpo.api.httpresponse import JsonResponse
39 from mygpo.api.advanced.directory import episode_data
40 from mygpo.api.backend import get_device
41 from mygpo.utils import format_time, parse_bool, get_timestamp, \
42 parse_request_body, normalize_feed_url
43 from mygpo.decorators import allowed_methods, cors_origin
44 from mygpo.history.models import EpisodeHistoryEntry
45 from mygpo.core.tasks import auto_flattr_episode
46 from mygpo.users.models import Client, InvalidEpisodeActionAttributes
47 from mygpo.users.settings import FLATTR_AUTO
48 from mygpo.favorites.models import FavoriteEpisode
49 from mygpo.api.basic_auth import require_valid_user, check_username
52 import logging
53 logger = logging.getLogger(__name__)
56 # keys that are allowed in episode actions
57 EPISODE_ACTION_KEYS = ('position', 'episode', 'action', 'device', 'timestamp',
58 'started', 'total', 'podcast')
61 @csrf_exempt
62 @require_valid_user
63 @check_username
64 @never_cache
65 @allowed_methods(['GET', 'POST'])
66 @cors_origin()
67 def episodes(request, username, version=1):
69 version = int(version)
70 now = datetime.utcnow()
71 now_ = get_timestamp(now)
72 ua_string = request.META.get('HTTP_USER_AGENT', '')
74 if request.method == 'POST':
75 try:
76 actions = parse_request_body(request)
77 except (UnicodeDecodeError, ValueError) as e:
78 msg = ('Could not decode episode update POST data for ' +
79 'user %s: %s') % (username,
80 request.body.decode('ascii', errors='replace'))
81 logger.warn(msg, exc_info=True)
82 return HttpResponseBadRequest(msg)
84 logger.info('start: user %s: %d actions from %s' % (request.user, len(actions), ua_string))
86 # handle in background
87 if len(actions) > dsettings.API_ACTIONS_MAX_NONBG:
88 bg_handler = dsettings.API_ACTIONS_BG_HANDLER
89 if bg_handler is not None:
91 modname, funname = bg_handler.rsplit('.', 1)
92 mod = import_module(modname)
93 fun = getattr(mod, funname)
95 fun(request.user, actions, now, ua_string)
97 # TODO: return 202 Accepted
98 return JsonResponse({'timestamp': now_, 'update_urls': []})
101 try:
102 update_urls = update_episodes(request.user, actions, now, ua_string)
103 except ValidationError as e:
104 logger.warn('Validation Error while uploading episode actions for user %s: %s', username, str(e))
105 return HttpResponseBadRequest(str(e))
107 except InvalidEpisodeActionAttributes as e:
108 msg = 'invalid episode action attributes while uploading episode actions for user %s' % (username,)
109 logger.warn(msg, exc_info=True)
110 return HttpResponseBadRequest(str(e))
112 logger.info('done: user %s: %d actions from %s' % (request.user, len(actions), ua_string))
113 return JsonResponse({'timestamp': now_, 'update_urls': update_urls})
115 elif request.method == 'GET':
116 podcast_url= request.GET.get('podcast', None)
117 device_uid = request.GET.get('device', None)
118 since_ = request.GET.get('since', None)
119 aggregated = parse_bool(request.GET.get('aggregated', False))
121 try:
122 since = int(since_) if since_ else None
123 if since is not None:
124 since = datetime.utcfromtimestamp(since)
125 except ValueError:
126 return HttpResponseBadRequest('since-value is not a valid timestamp')
128 if podcast_url:
129 podcast = get_object_or_404(Podcast, urls__url=podcast_url)
130 else:
131 podcast = None
133 if device_uid:
135 try:
136 user = request.user
137 device = user.client_set.get(uid=device_uid)
138 except Client.DoesNotExist as e:
139 return HttpResponseNotFound(str(e))
141 else:
142 device = None
144 changes = get_episode_changes(request.user, podcast, device, since,
145 now, aggregated, version)
147 return JsonResponse(changes)
151 def convert_position(action):
152 """ convert position parameter for API 1 compatibility """
153 pos = getattr(action, 'position', None)
154 if pos is not None:
155 action.position = format_time(pos)
156 return action
160 def get_episode_changes(user, podcast, device, since, until, aggregated, version):
162 history = EpisodeHistoryEntry.objects.filter(user=user,
163 timestamp__lt=until)
165 if since:
166 history = history.filter(timestamp__gte=since)
168 if podcast is not None:
169 history = history.filter(episode__podcast=podcast)
171 if device is not None:
172 history = history.filter(client=device)
174 if version == 1:
175 history = map(convert_position, history)
177 actions = [episode_action_json(a, user) for a in history]
179 if aggregated:
180 actions = list(dict( (a['episode'], a) for a in actions ).values())
182 return {'actions': actions, 'timestamp': get_timestamp(until)}
185 def episode_action_json(history, user):
187 action = {
188 'podcast': history.podcast_ref_url or history.episode.podcast.url,
189 'episode': history.episode_ref_url or history.episode.url,
190 'action': history.action,
191 'timestamp': history.timestamp.isoformat(),
194 if history.client:
195 action['device'] = history.client.uid
197 if history.action == EpisodeHistoryEntry.PLAY:
198 action['started'] = history.started
199 action['position'] = history.stopped # TODO: check "playmark"
200 action['total'] = history.total
202 return action
205 def update_episodes(user, actions, now, ua_string):
206 update_urls = []
207 auto_flattr = user.profile.settings.get_wksetting(FLATTR_AUTO)
209 # group all actions by their episode
210 for action in actions:
212 podcast_url = action.get('podcast', '')
213 podcast_url = sanitize_append(podcast_url, update_urls)
214 if not podcast_url:
215 continue
217 episode_url = action.get('episode', '')
218 episode_url = sanitize_append(episode_url, update_urls)
219 if not episode_url:
220 continue
222 podcast = Podcast.objects.get_or_create_for_url(podcast_url)
223 episode = Episode.objects.get_or_create_for_url(podcast, episode_url)
225 # parse_episode_action returns a EpisodeHistoryEntry obj
226 history = parse_episode_action(action, user, update_urls, now,
227 ua_string)
229 EpisodeHistoryEntry.create_entry(user, episode, history.action,
230 history.client, history.timestamp,
231 history.started, history.stopped,
232 history.total, podcast_url,
233 episode_url)
235 if history.action == EpisodeHistoryEntry.PLAY and auto_flattr:
236 auto_flattr_episode.delay(user.pk, episode.pk)
238 return update_urls
241 def parse_episode_action(action, user, update_urls, now, ua_string):
242 action_str = action.get('action', None)
243 if not valid_episodeaction(action_str):
244 raise Exception('invalid action %s' % action_str)
246 history = EpisodeHistoryEntry()
248 history.action = action['action']
250 if action.get('device', False):
251 client = get_device(user, action['device'], ua_string)
252 history.client = client
254 if action.get('timestamp', False):
255 history.timestamp = dateutil.parser.parse(action['timestamp'])
256 else:
257 history.timestamp = now
259 history.started = action.get('started', None)
260 history.stopped = action.get('position', None)
261 history.total = action.get('total', None)
263 return history
266 @csrf_exempt
267 @require_valid_user
268 @check_username
269 @never_cache
270 # Workaround for mygpoclient 1.0: It uses "PUT" requests
271 # instead of "POST" requests for uploading device settings
272 @allowed_methods(['POST', 'PUT'])
273 @cors_origin()
274 def device(request, username, device_uid):
275 d = get_device(request.user, device_uid,
276 request.META.get('HTTP_USER_AGENT', ''))
278 try:
279 data = parse_request_body(request)
280 except (UnicodeDecodeError, ValueError) as e:
281 msg = ('Could not decode device update POST data for ' +
282 'user %s: %s') % (username,
283 request.body.decode('ascii', errors='replace'))
284 logger.warn(msg, exc_info=True)
285 return HttpResponseBadRequest(msg)
287 if 'caption' in data:
288 if not data['caption']:
289 return HttpResponseBadRequest('caption must not be empty')
290 d.name = data['caption']
292 if 'type' in data:
293 if not valid_devicetype(data['type']):
294 return HttpResponseBadRequest('invalid device type %s' % data['type'])
295 d.type = data['type']
297 d.save()
298 return HttpResponse()
301 def valid_devicetype(type):
302 for t in Client.TYPES:
303 if t[0] == type:
304 return True
305 return False
307 def valid_episodeaction(type):
308 for t in EPISODE_ACTION_TYPES:
309 if t[0] == type:
310 return True
311 return False
314 @csrf_exempt
315 @require_valid_user
316 @check_username
317 @never_cache
318 @allowed_methods(['GET'])
319 @cors_origin()
320 def devices(request, username):
321 user = request.user
322 clients = user.client_set.filter(deleted=False)
323 client_data = [get_client_data(user, client) for client in clients]
324 return JsonResponse(client_data)
327 def get_client_data(user, client):
328 return dict(
329 id = client.uid,
330 caption = client.name,
331 type = client.type,
332 subscriptions= Subscription.objects.filter(user=user, client=client)\
333 .count(),
337 @require_valid_user
338 @check_username
339 @never_cache
340 @cors_origin()
341 def favorites(request, username):
342 favorites = FavoriteEpisode.episodes_for_user(request.user)
343 domain = RequestSite(request).domain
344 e_data = lambda e: episode_data(e, domain)
345 ret = map(e_data, favorites)
346 return JsonResponse(ret)
349 def sanitize_append(url, sanitized_list):
350 urls = normalize_feed_url(url)
351 if url != urls:
352 sanitized_list.append( (url, urls or '') )
353 return urls