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
.api
.httpresponse
import JsonResponse
30 from mygpo
.db
.couchdb
.episode
import episode_for_podcast_url
31 from mygpo
.db
.couchdb
.podcast
import podcast_by_id
, podcast_for_url
32 from mygpo
.db
.couchdb
.directory
import category_for_tag
36 @cache_page(60 * 60 * 24)
37 def top_tags(request
, count
):
38 count
= parse_range(count
, 1, 100, 100)
39 tag_cloud
= Topics(count
, num_cat
=0)
40 resp
= map(category_data
, tag_cloud
.tagcloud
)
41 return JsonResponse(resp
)
45 @cache_page(60 * 60 * 24)
46 def tag_podcasts(request
, tag
, count
):
47 count
= parse_range(count
, 1, 100, 100)
48 category
= category_for_tag(tag
)
50 return JsonResponse([])
52 domain
= RequestSite(request
).domain
53 query
= category
.get_podcasts(0, count
)
54 resp
= map(lambda p
: podcast_data(p
, domain
), query
)
55 return JsonResponse(resp
)
59 def podcast_info(request
):
60 url
= normalize_feed_url(request
.GET
.get('url', ''))
62 # 404 before we query for url, because query would complain
67 podcast
= podcast_for_url(url
)
70 domain
= RequestSite(request
).domain
71 resp
= podcast_data(podcast
, domain
)
73 return JsonResponse(resp
)
77 def episode_info(request
):
78 podcast_url
= normalize_feed_url(request
.GET
.get('podcast', ''))
79 episode_url
= normalize_feed_url(request
.GET
.get('url', ''))
81 # 404 before we query for url, because query would complain
82 # about missing parameters
83 if not podcast_url
or not episode_url
:
86 episode
= episode_for_podcast_url(podcast_url
, episode_url
)
91 domain
= RequestSite(request
).domain
93 resp
= episode_data(episode
, domain
)
94 return JsonResponse(resp
)
97 def podcast_data(obj
, domain
, scaled_logo_size
=64):
99 raise ValueError('podcast should not be None')
101 if isinstance(obj
, Podcast
):
103 elif isinstance(obj
, PodcastGroup
):
104 podcast
= obj
.get_podcast()
106 subscribers
= obj
.subscriber_count()
107 last_subscribers
= obj
.prev_subscriber_count()
109 scaled_logo_url
= obj
.get_logo_url(scaled_logo_size
)
113 "title": podcast
.title
,
114 "description": podcast
.description
,
115 "subscribers": subscribers
,
116 "subscribers_last_week": last_subscribers
,
117 "logo_url": podcast
.logo_url
,
118 "scaled_logo_url": 'http://%s%s' % (domain
, scaled_logo_url
),
119 "website": podcast
.link
,
120 "mygpo_link": 'http://%s%s' % (domain
, get_podcast_link_target(obj
)),
123 def episode_data(episode
, domain
, podcast
=None):
125 podcast
= podcast
or podcast_by_id(episode
.podcast
)
128 "title": episode
.title
,
130 "podcast_title": podcast
.title
if podcast
else '',
131 "podcast_url": podcast
.url
if podcast
else '',
132 "description": episode
.description
,
133 "website": episode
.link
,
134 "mygpo_link": 'http://%(domain)s%(res)s' % dict(domain
=domain
,
135 res
=get_episode_link_target(episode
, podcast
)) if podcast
else ''
139 data
['released'] = episode
.released
.strftime('%Y-%m-%dT%H:%M:%S')
144 def category_data(category
):
146 tag
= category
.label
,
147 usage
= category
.get_weight()