902399d6f8857670279051dad6f1dc62884e04f2
[mygpo.git] / mygpo / api / advanced / __init__.py
blob902399d6f8857670279051dad6f1dc62884e04f2
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 mygpo.api.basic_auth import require_valid_user, check_username
19 from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404
20 from mygpo.api.models import Device, Podcast, SubscriptionAction, Episode, EpisodeAction, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, EPISODE_ACTION_TYPES, DEVICE_TYPES, Subscription
21 from mygpo.api.models.users import EpisodeFavorite
22 from mygpo.api.httpresponse import JsonResponse
23 from mygpo.api.sanitizing import sanitize_url
24 from mygpo.api.advanced.directory import episode_data, podcast_data
25 from mygpo.api.backend import get_all_subscriptions
26 from django.core import serializers
27 from django.shortcuts import get_object_or_404
28 from time import mktime, gmtime, strftime
29 from datetime import datetime, timedelta
30 import dateutil.parser
31 from mygpo.log import log
32 from mygpo.utils import parse_time, parse_bool
33 from mygpo.decorators import allowed_methods
34 from django.db import IntegrityError
35 import re
36 from django.views.decorators.csrf import csrf_exempt
38 try:
39 #try to import the JSON module (if we are on Python 2.6)
40 import json
42 # Python 2.5 seems to have a different json module
43 if not 'dumps' in dir(json):
44 raise ImportError
46 except ImportError:
47 # No JSON module available - fallback to simplejson (Python < 2.6)
48 print "No JSON module available - fallback to simplejson (Python < 2.6)"
49 import simplejson as json
52 @csrf_exempt
53 @require_valid_user
54 @check_username
55 @allowed_methods(['GET', 'POST'])
56 def subscriptions(request, username, device_uid):
58 now = datetime.now()
59 now_ = int(mktime(now.timetuple()))
61 if request.method == 'GET':
62 try:
63 d = Device.objects.get(user=request.user, uid=device_uid, deleted=False)
64 except Device.DoesNotExist:
65 raise Http404('device %s does not exist' % device_uid)
67 try:
68 since_ = request.GET['since']
69 except KeyError:
70 return HttpResponseBadRequest('parameter since missing')
72 since = datetime.fromtimestamp(float(since_))
74 changes = get_subscription_changes(request.user, d, since, now)
76 return JsonResponse(changes)
78 elif request.method == 'POST':
79 d, created = Device.objects.get_or_create(user=request.user, uid=device_uid, defaults = {'type': 'other', 'name': 'New Device'})
81 if d.deleted:
82 d.deleted = False
83 d.save()
85 actions = json.loads(request.raw_post_data)
86 add = actions['add'] if 'add' in actions else []
87 rem = actions['remove'] if 'remove' in actions else []
89 try:
90 update_urls = update_subscriptions(request.user, d, add, rem)
91 except IntegrityError, e:
92 return HttpResponseBadRequest(e)
94 return JsonResponse({
95 'timestamp': now_,
96 'update_urls': update_urls,
100 def update_subscriptions(user, device, add, remove):
101 updated_urls = []
102 add_sanitized = []
103 rem_sanitized = []
105 for a in add:
106 if a in remove:
107 raise IntegrityError('can not add and remove %s at the same time' % a)
109 for u in add:
110 us = sanitize_url(u)
111 if u != us: updated_urls.append( (u, us) )
112 if us != '': add_sanitized.append(us)
114 for u in remove:
115 us = sanitize_url(u)
116 if u != us: updated_urls.append( (u, us) )
117 if us != '' and us not in add_sanitized:
118 rem_sanitized.append(us)
120 for a in add_sanitized:
121 p, p_created = Podcast.objects.get_or_create(url=a)
122 try:
123 s = SubscriptionAction.objects.create(podcast=p,device=device,action=SUBSCRIBE_ACTION)
124 except IntegrityError, e:
125 log('can\'t add subscription %s for user %s: %s' % (a, user, e))
127 for r in rem_sanitized:
128 p, p_created = Podcast.objects.get_or_create(url=r)
129 try:
130 s = SubscriptionAction.objects.create(podcast=p,device=device,action=UNSUBSCRIBE_ACTION)
131 except IntegrityError, e:
132 log('can\'t remove subscription %s for user %s: %s' % (r, user, e))
134 return updated_urls
136 def get_subscription_changes(user, device, since, until):
137 actions = {}
138 for a in SubscriptionAction.objects.filter(device=device, timestamp__gt=since, timestamp__lte=until).order_by('timestamp'):
139 #ordered by ascending date; newer entries overwriter older ones
140 actions[a.podcast] = a
142 add = []
143 remove = []
145 for a in actions.values():
146 if a.action == SUBSCRIBE_ACTION:
147 add.append(a.podcast.url)
148 elif a.action == UNSUBSCRIBE_ACTION:
149 remove.append(a.podcast.url)
151 until_ = int(mktime(until.timetuple()))
152 return {'add': add, 'remove': remove, 'timestamp': until_}
155 @csrf_exempt
156 @require_valid_user
157 @check_username
158 @allowed_methods(['GET', 'POST'])
159 def episodes(request, username, version=1):
161 version = int(version)
162 now = datetime.now()
163 now_ = int(mktime(now.timetuple()))
165 if request.method == 'POST':
166 try:
167 actions = json.loads(request.raw_post_data)
168 except KeyError, e:
169 log('could not parse episode update info for user %s: %s' % (username, e))
170 return HttpResponseBadRequest()
172 try:
173 update_urls = update_episodes(request.user, actions)
174 except Exception, e:
175 log('could not update episodes for user %s: %s' % (username, e))
176 return HttpResponseBadRequest(e)
178 return JsonResponse({'timestamp': now_, 'update_urls': update_urls})
180 elif request.method == 'GET':
181 podcast_url= request.GET.get('podcast', None)
182 device_uid = request.GET.get('device', None)
183 since_ = request.GET.get('since', None)
184 aggregated = parse_bool(request.GET.get('aggregated', False))
186 since = datetime.fromtimestamp(float(since_)) if since_ else None
188 try:
189 podcast = Podcast.objects.get(url=podcast_url) if podcast_url else None
190 device = Device.objects.get(user=request.user,uid=device_uid, deleted=False) if device_uid else None
191 except:
192 raise Http404
194 return JsonResponse(get_episode_changes(request.user, podcast, device, since, now, aggregated, version))
197 def get_episode_changes(user, podcast, device, since, until, aggregated, version):
198 if aggregated:
199 actions = {}
200 else:
201 actions = []
202 eactions = EpisodeAction.objects.filter(user=user, timestamp__lte=until)
204 if podcast:
205 eactions = eactions.filter(episode__podcast=podcast)
207 if device:
208 eactions = eactions.filter(device=device)
210 if since: # we can't use None with __gt
211 eactions = eactions.filter(timestamp__gt=since)
213 if aggregated:
214 eactions = eactions.order_by('timestamp')
216 for a in eactions:
217 action = {
218 'podcast': a.episode.podcast.url,
219 'episode': a.episode.url,
220 'action': a.action,
221 'timestamp': a.timestamp.strftime('%Y-%m-%dT%H:%M:%S') #2009-12-12T09:00:00
224 if a.action == 'play' and a.playmark:
225 if version == 1:
226 t = gmtime(a.playmark)
227 action['position'] = strftime('%H:%M:%S', t)
228 elif None in (a.playmark, a.started, a.total):
229 log('Ignoring broken episode action in DB: %r' % (a,))
230 continue
231 else:
232 action['position'] = int(a.playmark)
233 action['started'] = int(a.started)
234 action['total'] = int(a.total)
236 if aggregated:
237 actions[a.episode] = action
238 else:
239 actions.append(action)
241 until_ = int(mktime(until.timetuple()))
243 if aggregated:
244 actions = list(actions.itervalues())
246 return {'actions': actions, 'timestamp': until_}
249 def update_episodes(user, actions):
250 update_urls = []
252 for e in actions:
253 u = e['podcast']
254 us = sanitize_url(u)
255 if u != us: update_urls.append( (u, us) )
256 if us == '': continue
258 podcast, p_created = Podcast.objects.get_or_create(url=us)
260 eu = e['episode']
261 eus = sanitize_url(eu, podcast=False, episode=True)
262 if eu != eus: update_urls.append( (eu, eus) )
263 if eus == '': continue
265 episode, e_created = Episode.objects.get_or_create(podcast=podcast, url=eus)
266 action = e['action']
267 if not valid_episodeaction(action):
268 raise Exception('invalid action %s' % action)
270 if 'device' in e:
271 device, created = Device.objects.get_or_create(user=user, uid=e['device'], defaults={'name': 'Unknown', 'type': 'other'})
273 # undelete a previously deleted device
274 if device.deleted:
275 device.deleted = False
276 device.save()
278 else:
279 device, created = None, False
281 timestamp = dateutil.parser.parse(e['timestamp']) if 'timestamp' in e and e['timestamp'] else datetime.now()
283 # Time values for play actions and their keys in JSON
284 PLAY_ACTION_KEYS = ('position', 'started', 'total')
285 time_values = {}
287 for key in PLAY_ACTION_KEYS:
288 if key in e:
289 if action != 'play':
290 # Key found, but must not be supplied (no play action!)
291 return HttpResponseBadRequest('%s only allowed in play actions' % key)
293 try:
294 time_values[key] = parse_time(e[key])
295 except ValueError:
296 log('could not parse %s parameter (value: %s) for user %s' % (key, repr(e[key]), user))
297 return HttpResponseBadRequest('Wrong format for %s: %s' % (key, repr(e[key])))
298 else:
299 # Value not supplied by client
300 time_values[key] = None
302 # Sanity check: If started or total are given, require position
303 if (time_values['started'] is not None or \
304 time_values['total'] is not None) and \
305 time_values['position'] is None:
306 return HttpResponseBadRequest('started and total require position')
308 # Sanity check: total and position can only appear together
309 if (time_values['total'] or time_values['started']) and \
310 not (time_values['total'] and time_values['started']):
311 return HttpResponseBadRequest('total and started parameters can only appear together')
313 try:
314 EpisodeAction.objects.create(user=user, episode=episode, device=device, action=action, timestamp=timestamp,
315 playmark=time_values['position'], started=time_values['started'], total=time_values['total'])
316 except Exception, e:
317 log('error while adding episode action (user %s, episode %s, device %s, action %s, timestamp %s): %s' % (user, episode, device, action, timestamp, e))
319 return update_urls
322 @csrf_exempt
323 @require_valid_user
324 @check_username
325 # Workaround for mygpoclient 1.0: It uses "PUT" requests
326 # instead of "POST" requests for uploading device settings
327 @allowed_methods(['POST', 'PUT'])
328 def device(request, username, device_uid):
330 d, created = Device.objects.get_or_create(user=request.user, uid=device_uid)
332 #undelete a previously deleted device
333 if d.deleted:
334 d.deleted = False
335 d.save()
337 data = json.loads(request.raw_post_data)
339 if 'caption' in data:
340 d.name = data['caption']
342 if 'type' in data:
343 if not valid_devicetype(data['type']):
344 return HttpResponseBadRequest('invalid device type %s' % data['type'])
345 d.type = data['type']
347 d.save()
349 return HttpResponse()
352 def valid_devicetype(type):
353 for t in DEVICE_TYPES:
354 if t[0] == type:
355 return True
356 return False
358 def valid_episodeaction(type):
359 for t in EPISODE_ACTION_TYPES:
360 if t[0] == type:
361 return True
362 return False
365 @csrf_exempt
366 @require_valid_user
367 @check_username
368 @allowed_methods(['GET'])
369 def devices(request, username):
370 devices = []
371 for d in Device.objects.filter(user=request.user, deleted=False):
372 devices.append({
373 'id': d.uid,
374 'caption': d.name,
375 'type': d.type,
376 'subscriptions': Subscription.objects.filter(device=d).count()
379 return JsonResponse(devices)
382 @csrf_exempt
383 @require_valid_user
384 @check_username
385 def updates(request, username, device_uid):
386 now = datetime.now()
387 now_ = int(mktime(now.timetuple()))
389 device = get_object_or_404(Device, user=request.user, uid=device_uid)
391 try:
392 since_ = request.GET['since']
393 except KeyError:
394 return HttpResponseBadRequest('parameter since missing')
396 since = datetime.fromtimestamp(float(since_))
398 ret = get_subscription_changes(request.user, device, since, now)
400 # replace added urls with details
401 podcast_details = []
402 for url in ret['add']:
403 podcast = Podcast.objects.get(url=url)
404 podcast_details.append(podcast_data(podcast))
406 ret['add'] = podcast_details
409 # add episode details
410 subscriptions = get_all_subscriptions(request.user)
411 episode_status = {}
412 for e in Episode.objects.filter(podcast__in=subscriptions, timestamp__gte=since).order_by('timestamp'):
413 episode_status[e] = 'new'
414 for a in EpisodeAction.objects.filter(user=request.user, episode__podcast__in=subscriptions, timestamp__gte=since).order_by('timestamp'):
415 episode_status[a.episode] = a.action
417 updates = []
418 for episode, status in episode_status.iteritems():
419 t = episode_data(episode)
420 t['released'] = e.timestamp.strftime('%Y-%m-%dT%H:%M:%S')
421 t['status'] = status
422 updates.append(t)
424 ret['updates'] = updates
426 return JsonResponse(ret)
429 @require_valid_user
430 @check_username
431 def favorites(request, username):
432 favorites = [x.episode for x in EpisodeFavorite.objects.filter(user=request.user).order_by('-created')]
433 ret = map(episode_data, favorites)
434 return JsonResponse(ret)