optimize directory queries
[mygpo.git] / mygpo / api / advanced / directory.py
blob97238d9355f440bd0b173e324173ef025dfc1ede
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', ''))
62 podcast = podcast_for_url(url)
63 if not podcast:
64 raise Http404
65 domain = RequestSite(request).domain
66 resp = podcast_data(podcast, domain)
68 return JsonResponse(resp)
71 @cache_page(60 * 60)
72 def episode_info(request):
73 podcast_url = sanitize_url(request.GET.get('podcast', ''))
74 episode_url = sanitize_url(request.GET.get('url', ''), 'episode')
76 episode = episode_for_podcast_url(podcast_url, episode_url)
78 if episode is None:
79 raise Http404
81 domain = RequestSite(request).domain
83 resp = episode_data(episode, domain)
84 return JsonResponse(resp)
87 def podcast_data(obj, domain, scaled_logo_size=64):
88 if obj is None:
89 raise ValueError('podcast should not be None')
91 if isinstance(obj, Podcast):
92 podcast = obj
93 elif isinstance(obj, PodcastGroup):
94 podcast = obj.get_podcast()
96 subscribers = obj.subscriber_count()
97 last_subscribers = obj.prev_subscriber_count()
99 scaled_logo_url = obj.get_logo_url(scaled_logo_size)
101 return {
102 "url": podcast.url,
103 "title": podcast.title,
104 "description": podcast.description,
105 "subscribers": subscribers,
106 "subscribers_last_week": last_subscribers,
107 "logo_url": podcast.logo_url,
108 "scaled_logo_url": 'http://%s%s' % (domain, scaled_logo_url),
109 "website": podcast.link,
110 "mygpo_link": 'http://%s%s' % (domain, get_podcast_link_target(obj)),
113 def episode_data(episode, domain, podcast=None):
115 podcast = podcast or podcast_by_id(episode.podcast)
117 data = {
118 "title": episode.title,
119 "url": episode.url,
120 "podcast_title": podcast.title if podcast else '',
121 "podcast_url": podcast.url if podcast else '',
122 "description": episode.description,
123 "website": episode.link,
124 "mygpo_link": 'http://%(domain)s%(res)s' % dict(domain=domain,
125 res=get_episode_link_target(episode, podcast)) if podcast else ''
128 if episode.released:
129 data['released'] = episode.released.strftime('%Y-%m-%dT%H:%M:%S')
131 return data
134 def category_data(category):
135 return dict(
136 tag = category.label,
137 usage = category.get_weight()