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
23 from django
.shortcuts
import get_object_or_404
25 from mygpo
.podcasts
.models
import Podcast
, Episode
26 from mygpo
.utils
import parse_range
, normalize_feed_url
27 from mygpo
.directory
.tags
import Topics
28 from mygpo
.web
.utils
import get_episode_link_target
, get_podcast_link_target
29 from mygpo
.web
.logo
import get_logo_url
30 from mygpo
.subscriptions
.models
import SubscribedPodcast
31 from mygpo
.decorators
import cors_origin
32 from mygpo
.api
.httpresponse
import JsonResponse
33 from mygpo
.db
.couchdb
.directory
import category_for_tag
37 @cache_page(60 * 60 * 24)
39 def top_tags(request
, count
):
40 count
= parse_range(count
, 1, 100, 100)
41 tag_cloud
= Topics(count
, num_cat
=0)
42 resp
= map(category_data
, tag_cloud
.tagcloud
)
43 return JsonResponse(resp
)
47 @cache_page(60 * 60 * 24)
49 def tag_podcasts(request
, tag
, count
):
50 count
= parse_range(count
, 1, 100, 100)
51 category
= category_for_tag(tag
)
53 return JsonResponse([])
55 domain
= RequestSite(request
).domain
56 query
= category
.get_podcasts(0, count
)
57 resp
= map(lambda p
: podcast_data(p
, domain
), query
)
58 return JsonResponse(resp
)
63 def podcast_info(request
):
64 url
= normalize_feed_url(request
.GET
.get('url', ''))
66 # 404 before we query for url, because query would complain
71 podcast
= get_object_or_404(Podcast
, urls__url
=url
)
72 domain
= RequestSite(request
).domain
73 resp
= podcast_data(podcast
, domain
)
75 return JsonResponse(resp
)
80 def episode_info(request
):
81 podcast_url
= normalize_feed_url(request
.GET
.get('podcast', ''))
82 episode_url
= normalize_feed_url(request
.GET
.get('url', ''))
84 # 404 before we query for url, because query would complain
85 # about missing parameters
86 if not podcast_url
or not episode_url
:
90 query
= Episode
.objects
.filter(podcast__urls__url
=podcast_url
,
91 urls__url
=episode_url
)
92 episode
= query
.select_related('podcast').get()
93 except Episode
.DoesNotExist
:
96 domain
= RequestSite(request
).domain
98 resp
= episode_data(episode
, domain
)
99 return JsonResponse(resp
)
102 def podcast_data(obj
, domain
, scaled_logo_size
=64):
104 raise ValueError('podcast should not be None')
106 if isinstance(obj
, SubscribedPodcast
):
108 podcast
= obj
.podcast
113 subscribers
= podcast
.subscribers
114 last_subscribers
= podcast
.subscribers
116 scaled_logo_url
= get_logo_url(podcast
, scaled_logo_size
)
120 "title": podcast
.title
,
121 "description": podcast
.description
,
122 "subscribers": subscribers
,
123 "subscribers_last_week": last_subscribers
,
124 "logo_url": podcast
.logo_url
,
125 "scaled_logo_url": 'http://%s%s' % (domain
, scaled_logo_url
),
126 "website": podcast
.link
,
127 "mygpo_link": 'http://%s%s' % (domain
, get_podcast_link_target(podcast
)),
130 def episode_data(episode
, domain
, podcast
=None):
132 podcast
= podcast
or episode
.podcast
135 "title": episode
.title
,
137 "podcast_title": podcast
.title
if podcast
else '',
138 "podcast_url": podcast
.url
if podcast
else '',
139 "description": episode
.description
,
140 "website": episode
.link
,
141 "mygpo_link": 'http://%(domain)s%(res)s' % dict(domain
=domain
,
142 res
=get_episode_link_target(episode
, podcast
)) if podcast
else ''
146 data
['released'] = episode
.released
.strftime('%Y-%m-%dT%H:%M:%S')
151 def category_data(category
):
153 tag
= category
.label
,
154 usage
= category
.get_weight()