35032ec02f0f177a0cb645f2638709af8be05405
[mygpo.git] / mygpo / api / advanced / directory.py
blob35032ec02f0f177a0cb645f2638709af8be05405
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 django.http import Http404
19 from django.core.urlresolvers import reverse
20 from django.contrib.sites.models import RequestSite
21 from django.views.decorators.csrf import csrf_exempt
22 from django.views.decorators.cache import cache_page
24 from mygpo.core import models
25 from mygpo.core.models import Podcast, PodcastGroup
26 from mygpo.utils import parse_range
27 from mygpo.directory.tags import Topics
28 from mygpo.web.utils import get_episode_link_target, get_podcast_link_target
29 from mygpo.api.httpresponse import JsonResponse
30 from mygpo.api.sanitizing import sanitize_url
31 from mygpo.db.couchdb.episode import episode_for_podcast_url
32 from mygpo.db.couchdb.podcast import podcast_by_id, podcast_for_url
33 from mygpo.db.couchdb.directory import category_for_tag
36 @csrf_exempt
37 @cache_page(60 * 60 * 24)
38 def top_tags(request, count):
39 count = parse_range(count, 1, 100, 100)
40 tag_cloud = Topics(count, num_cat=0)
41 resp = map(category_data, tag_cloud.tagcloud)
42 return JsonResponse(resp)
45 @csrf_exempt
46 @cache_page(60 * 60 * 24)
47 def tag_podcasts(request, tag, count):
48 count = parse_range(count, 1, 100, 100)
49 category = category_for_tag(tag)
50 if not category:
51 return JsonResponse([])
53 domain = RequestSite(request).domain
54 query = category.get_podcasts(0, count)
55 resp = map(lambda p: podcast_data(p, domain), query)
56 return JsonResponse(resp)
59 @cache_page(60 * 60)
60 def podcast_info(request):
61 url = sanitize_url(request.GET.get('url', ''))
63 # 404 before we query for url, because query would complain
64 # about missing param
65 if not url:
66 raise Http404
68 podcast = podcast_for_url(url)
69 if not podcast:
70 raise Http404
71 domain = RequestSite(request).domain
72 resp = podcast_data(podcast, domain)
74 return JsonResponse(resp)
77 @cache_page(60 * 60)
78 def episode_info(request):
79 podcast_url = sanitize_url(request.GET.get('podcast', ''))
80 episode_url = sanitize_url(request.GET.get('url', ''), 'episode')
82 # 404 before we query for url, because query would complain
83 # about missing parameters
84 if not podcast_url or not episode_url:
85 raise Http404
87 episode = episode_for_podcast_url(podcast_url, episode_url)
89 if episode is None:
90 raise Http404
92 domain = RequestSite(request).domain
94 resp = episode_data(episode, domain)
95 return JsonResponse(resp)
98 def podcast_data(obj, domain, scaled_logo_size=64):
99 if obj is None:
100 raise ValueError('podcast should not be None')
102 if isinstance(obj, Podcast):
103 podcast = obj
104 elif isinstance(obj, PodcastGroup):
105 podcast = obj.get_podcast()
107 subscribers = obj.subscriber_count()
108 last_subscribers = obj.prev_subscriber_count()
110 scaled_logo_url = obj.get_logo_url(scaled_logo_size)
112 return {
113 "url": podcast.url,
114 "title": podcast.title,
115 "description": podcast.description,
116 "subscribers": subscribers,
117 "subscribers_last_week": last_subscribers,
118 "logo_url": podcast.logo_url,
119 "scaled_logo_url": 'http://%s%s' % (domain, scaled_logo_url),
120 "website": podcast.link,
121 "mygpo_link": 'http://%s%s' % (domain, get_podcast_link_target(obj)),
124 def episode_data(episode, domain, podcast=None):
126 podcast = podcast or podcast_by_id(episode.podcast)
128 data = {
129 "title": episode.title,
130 "url": episode.url,
131 "podcast_title": podcast.title if podcast else '',
132 "podcast_url": podcast.url if podcast else '',
133 "description": episode.description,
134 "website": episode.link,
135 "mygpo_link": 'http://%(domain)s%(res)s' % dict(domain=domain,
136 res=get_episode_link_target(episode, podcast)) if podcast else ''
139 if episode.released:
140 data['released'] = episode.released.strftime('%Y-%m-%dT%H:%M:%S')
142 return data
145 def category_data(category):
146 return dict(
147 tag = category.label,
148 usage = category.get_weight()