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
, 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
.decorators
import cors_origin
30 from mygpo
.api
.httpresponse
import JsonResponse
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
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
= podcast_for_url(url
)
74 domain
= RequestSite(request
).domain
75 resp
= podcast_data(podcast
, domain
)
77 return JsonResponse(resp
)
82 def episode_info(request
):
83 podcast_url
= normalize_feed_url(request
.GET
.get('podcast', ''))
84 episode_url
= normalize_feed_url(request
.GET
.get('url', ''))
86 # 404 before we query for url, because query would complain
87 # about missing parameters
88 if not podcast_url
or not episode_url
:
91 episode
= episode_for_podcast_url(podcast_url
, episode_url
)
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 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
)
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
)
129 "title": episode
.title
,
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 ''
140 data
['released'] = episode
.released
.strftime('%Y-%m-%dT%H:%M:%S')
145 def category_data(category
):
147 tag
= category
.label
,
148 usage
= category
.get_weight()