47a5c859f18147ef8c3c940c44462d5d1667bebc
[mygpo.git] / mygpo / api / advanced / directory.py
blob47a5c859f18147ef8c3c940c44462d5d1667bebc
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
20 from mygpo.api.httpresponse import JsonResponse
21 from mygpo.exceptions import ParameterMissing
22 from django.shortcuts import get_object_or_404
23 from mygpo.api.sanitizing import sanitize_url
24 from mygpo.api.models import Device, Podcast, Episode, ToplistEntry
25 from mygpo.api.models.episodes import Chapter
26 from mygpo.data.models import PodcastTag
27 from django.utils.translation import ugettext as _
28 from datetime import datetime, timedelta
29 from mygpo.log import log
30 from mygpo.utils import parse_time
31 import dateutil.parser
32 from django.contrib.sites.models import Site
33 from django.views.decorators.csrf import csrf_exempt
35 try:
36 #try to import the JSON module (if we are on Python 2.6)
37 import json
39 # Python 2.5 seems to have a different json module
40 if not 'dumps' in dir(json):
41 raise ImportError
43 except ImportError:
44 # No JSON module available - fallback to simplejson (Python < 2.6)
45 print "No JSON module available - fallback to simplejson (Python < 2.6)"
46 import simplejson as json
49 @csrf_exempt
50 def top_tags(request, count):
51 tags = DirectoryEntry.objects.top_tags(int(count))
52 resp = []
53 for t in tags:
54 resp.append( {
55 'tag': t.tag,
56 'usage': t.entries
57 } )
58 return JsonResponse(resp)
61 @csrf_exempt
62 def tag_podcasts(request, tag, count):
63 resp = []
64 for p in DirectoryEntry.objects.podcasts_for_tag(tag)[:int(count)]:
65 resp.append( podcast_data(p.get_podcast()) )
67 return JsonResponse(resp)
70 def podcast_info(request):
71 url = sanitize_url(request.GET.get('url', ''))
72 podcast = get_object_or_404(Podcast, url=url)
73 resp = podcast_data(podcast)
75 return JsonResponse(resp)
78 def episode_info(request):
79 podcast_url = sanitize_url(request.GET.get('podcast', ''))
80 episode_url = sanitize_url(request.GET.get('url', ''), podcast=False, episode=True)
81 episode = get_object_or_404(Episode, url=episode_url, podcast__url=podcast_url)
83 resp = episode_data(episode)
84 return JsonResponse(resp)
87 def podcast_data(podcast):
88 site = Site.objects.get_current()
89 if podcast.group:
90 try:
91 e = ToplistEntry.objects.get(podcast_group=podcast.group)
93 # no toplist entry has been created for the group yet
94 except ToplistEntry.DoesNotExist:
95 e = ToplistEntry.objects.get(podcast=podcast)
96 else:
97 e = ToplistEntry.objects.get(podcast=podcast)
99 return {
100 "url": podcast.url,
101 "title": podcast.title,
102 "description": podcast.description,
103 "subscribers": e.subscriptions,
104 "logo_url": podcast.logo_url,
105 "website": podcast.link,
106 "mygpo_link": 'http://%s/podcast/%s' % (site.domain, podcast.id),
109 def episode_data(episode):
110 site = Site.objects.get_current()
112 return {
113 "title": episode.title,
114 "url": episode.url,
115 "podcast_title": episode.podcast.title,
116 "podcast_url": episode.podcast.url,
117 "description": episode.description,
118 "website": episode.link,
119 "mygpo_link": 'http://%s/episode/%s' % (site.domain, episode.id),