remove unnecessary imports
[mygpo.git] / mygpo / api / advanced / directory.py
blob4706ffb80cea8998fdde43ad7aa8509868cb7a43
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.httpresponse import JsonResponse
19 from django.shortcuts import get_object_or_404
20 from mygpo.api.sanitizing import sanitize_url
21 from mygpo.api.models import Podcast, Episode, ToplistEntry
22 from django.contrib.sites.models import Site
23 from django.views.decorators.csrf import csrf_exempt
26 @csrf_exempt
27 def top_tags(request, count):
28 tags = DirectoryEntry.objects.top_tags(int(count))
29 resp = []
30 for t in tags:
31 resp.append( {
32 'tag': t.tag,
33 'usage': t.entries
34 } )
35 return JsonResponse(resp)
38 @csrf_exempt
39 def tag_podcasts(request, tag, count):
40 resp = []
41 for p in DirectoryEntry.objects.podcasts_for_tag(tag)[:int(count)]:
42 resp.append( podcast_data(p.get_podcast()) )
44 return JsonResponse(resp)
47 def podcast_info(request):
48 url = sanitize_url(request.GET.get('url', ''))
49 podcast = get_object_or_404(Podcast, url=url)
50 resp = podcast_data(podcast)
52 return JsonResponse(resp)
55 def episode_info(request):
56 podcast_url = sanitize_url(request.GET.get('podcast', ''))
57 episode_url = sanitize_url(request.GET.get('url', ''), podcast=False, episode=True)
58 episode = get_object_or_404(Episode, url=episode_url, podcast__url=podcast_url)
60 resp = episode_data(episode)
61 return JsonResponse(resp)
64 def podcast_data(podcast):
65 site = Site.objects.get_current()
66 if podcast.group:
67 try:
68 e = ToplistEntry.objects.get(podcast_group=podcast.group)
70 # no toplist entry has been created for the group yet
71 except ToplistEntry.DoesNotExist:
72 e = ToplistEntry.objects.get(podcast=podcast)
73 else:
74 e = ToplistEntry.objects.get(podcast=podcast)
76 return {
77 "url": podcast.url,
78 "title": podcast.title,
79 "description": podcast.description,
80 "subscribers": e.subscriptions,
81 "logo_url": podcast.logo_url,
82 "website": podcast.link,
83 "mygpo_link": 'http://%s/podcast/%s' % (site.domain, podcast.id),
86 def episode_data(episode):
87 site = Site.objects.get_current()
89 return {
90 "title": episode.title,
91 "url": episode.url,
92 "podcast_title": episode.podcast.title,
93 "podcast_url": episode.podcast.url,
94 "description": episode.description,
95 "website": episode.link,
96 "mygpo_link": 'http://%s/episode/%s' % (site.domain, episode.id),