remove unnecessary imports
[mygpo.git] / mygpo / api / advanced / episode.py
blobc8ecc07060f6e2f46d3d3b5c150ad73e65f6a4ef
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 HttpResponseBadRequest
20 from mygpo.api.httpresponse import JsonResponse
21 from mygpo.exceptions import ParameterMissing
22 from mygpo.api.sanitizing import sanitize_url
23 from mygpo.api.models import Device, Podcast, Episode
24 from mygpo.api.models.episodes import Chapter
25 from django.utils.translation import ugettext as _
26 from datetime import datetime
27 from mygpo.utils import parse_time
28 from mygpo.decorators import allowed_methods
29 import dateutil.parser
30 from django.views.decorators.csrf import csrf_exempt
32 try:
33 #try to import the JSON module (if we are on Python 2.6)
34 import json
36 # Python 2.5 seems to have a different json module
37 if not 'dumps' in dir(json):
38 raise ImportError
40 except ImportError:
41 # No JSON module available - fallback to simplejson (Python < 2.6)
42 print "No JSON module available - fallback to simplejson (Python < 2.6)"
43 import simplejson as json
46 @csrf_exempt
47 @require_valid_user
48 @check_username
49 @allowed_methods(['POST', 'GET'])
50 def chapters(request, username):
52 now = datetime.now()
53 now_ = int(mktime(now.timetuple()))
55 if request.method == 'POST':
56 req = json.loads(request.raw_post_data)
58 if not 'podcast' in req:
59 return HttpResponseBadRequest('Podcast URL missing')
61 if not 'episode' in req:
62 return HttpResponseBadRequest('Episode URL missing')
64 podcast_url = req.get('podcast', '')
65 episode_url = req.get('episode', '')
66 update_urls = []
68 # podcast sanitizing
69 s_podcast_url = sanitize_url(podcast_url)
70 if s_podcast_url != podcast_url:
71 req['podcast'] = s_podcast_url
72 update_urls.append((podcast_url, s_podcast_url))
74 # episode sanitizing
75 s_episode_url = sanitize_url(episode_url, podcast=False, episode=True)
76 if s_episode_url != episode_url:
77 req['episode'] = s_episode_url
78 update_urls.append((episode_url, s_episode_url))
80 if (s_podcast_url != '') and (s_episode_url != ''):
81 try:
82 update_chapters(req, request.user)
83 except ParameterMissing, e:
84 return HttpResponseBadRequest(e)
86 return JsonResponse({
87 'update_url': update_url,
88 'timestamp': now_
91 elif request.method == 'GET':
92 if not 'podcast' in request.GET:
93 return HttpResponseBadRequest('podcast URL missing')
95 if not 'episode' in request.GET:
96 return HttpResponseBadRequest('Episode URL missing')
98 podcast_url = request.GET['podcast']
99 episode_url = request.GET['episode']
101 since_ = request.GET.get('since', None)
102 since = datetime.fromtimestamp(float(since_)) if since_ else None
104 podcast = Podcast.objects.get(url=sanitize_url(podcast_url))
105 episode = Episode.objects.get(url=sanitize_url(episode_url, podcast=False, episode=True), podcast=podcast)
106 chapter_q = Chapter.objects.filter(user=request.user, episode=episode).order_by('start')
108 if since:
109 chapter_q = chapter_q.filter(timestamp__gt=since)
111 chapters = []
112 for c in chapter_q:
113 chapters.append({
114 'start': c.start,
115 'end': c.end,
116 'label': c.label,
117 'advertisement': c.advertisement,
118 'timestamp': c.created,
119 'device': c.device.uid
122 return JsonResponse({
123 'chapters': chapters,
124 'timestamp': now_
128 def update_chapters(req, user):
129 podcast, c = Podcast.objects.get_or_create(url=req['podcast'])
130 episode, c = Episode.objects.get_or_create(url=req['episode'], podcast=podcast)
132 device = None
133 if 'device' in req:
134 device, c = Device.objects.get_or_create(user=user, uid=req['device'], defaults = {'type': 'other', 'name': _('New Device')})
136 timestamp = dateutil.parser.parse(req['timestamp']) if 'timestamp' in req else datetime.now()
138 for c in req.get('chapters_add', []):
139 if not 'start' in c:
140 raise ParameterMissing('start parameter missing')
141 start = parse_time(c['start'])
143 if not 'end' in c:
144 raise ParameterMissing('end parameter missing')
145 end = parse_time(c['end'])
147 label = c.get('label', '')
148 adv = c.get('advertisement', False)
151 Chapter.objects.create(
152 user=user,
153 episode=episode,
154 device=device,
155 created=timestamp,
156 start=start,
157 end=end,
158 label=label,
159 advertisement=adv)
162 for c in req.get('chapters_remove', []):
163 if not 'start' in c:
164 raise ParameterMissing('start parameter missing')
165 start = parse_time(c['start'])
167 if not 'end' in c:
168 raise ParameterMissing('end parameter missing')
169 end = parse_time(c['end'])
171 Chapter.objects.filter(
172 user=user,
173 episode=episode,
174 start=start,
175 end=end).delete()