1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from base64 import b64encode
8 from django.contrib.syndication.views import Feed
9 from django.core.urlresolvers import reverse
10 from django.shortcuts import get_object_or_404
11 from django.utils.feedgenerator import Atom1Feed
12 from django.conf import settings
13 from django.http import Http404
14 from django.contrib.sites.models import Site
16 from catalogue.models import Book, Tag
20 {u"category": u"author", u"title": u"Autorzy", u"description": u"Utwory wg autorów"},
21 {u"category": u"kind", u"title": u"Rodzaje", u"description": u"Utwory wg rodzajów"},
22 {u"category": u"genre", u"title": u"Gatunki", u"description": u"Utwory wg gatunków"},
23 {u"category": u"epoch", u"title": u"Epoki", u"description": u"Utwory wg epok"},
27 class OPDSFeed(Atom1Feed):
28 link_rel = u"subsection"
29 link_type = u"application/atom+xml"
32 with open(os.path.join(settings.STATIC_ROOT, "img/book-par ent.png")) as f:
34 _book_parent_img_size = len(t)
35 _book_parent_img = b64encode(t)
37 _book_parent_img = _book_parent_img_size = ''
40 with open(os.path.join(settings.STATIC_ROOT, "img/bo ok.png")) as f:
42 _book_img_size = len(t)
43 _book_img = b64encode(t)
45 _book_img = _book_img_size = ''
47 def add_root_elements(self, handler):
48 super(OPDSFeed, self).add_root_elements(handler)
49 handler.addQuickElement(u"link", u"", {u"href": reverse("opds_authors"), u"rel": u"start", u"type": u"application/atom+xml"})
52 def add_item_elements(self, handler, item):
53 """ modified from Atom1Feed.add_item_elements """
54 handler.addQuickElement(u"title", item['title'])
56 # add a OPDS Navigation link if there's no enclosure
57 if item['enclosure'] is None:
58 handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"})
59 # add a "green book" icon
60 handler.addQuickElement(u"link", '',
61 {u"rel": u"http://opds-spec.org/thumbnail",
62 u"href": u"data:image/png;base64,%s" % self._book_parent_img,
63 u"length": unicode(self._book_parent_img_size),
64 u"type": u"image/png"})
65 if item['pubdate'] is not None:
66 handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8'))
69 if item['author_name'] is not None:
70 handler.startElement(u"author", {})
71 handler.addQuickElement(u"name", item['author_name'])
72 if item['author_email'] is not None:
73 handler.addQuickElement(u"email", item['author_email'])
74 if item['author_link'] is not None:
75 handler.addQuickElement(u"uri", item['author_link'])
76 handler.endElement(u"author")
79 if item['unique_id'] is not None:
80 unique_id = item['unique_id']
82 unique_id = get_tag_uri(item['link'], item['pubdate'])
83 handler.addQuickElement(u"id", unique_id)
86 # OPDS needs type=text
87 if item['description'] is not None:
88 handler.addQuickElement(u"summary", item['description'], {u"type": u"text"})
90 # Enclosure as OPDS Acquisition Link
91 if item['enclosure'] is not None:
92 handler.addQuickElement(u"link", '',
93 {u"rel": u"http://opds-spec.org/acquisition",
94 u"href": item['enclosure'].url,
95 u"length": item['enclosure'].length,
96 u"type": item['enclosure'].mime_type})
97 # add a "red book" icon
98 handler.addQuickElement(u"link", '',
99 {u"rel": u"http://opds-spec.org/thumbnail",
100 u"href": u"data:image/png;base64,%s" % self._book_img,
101 u"length": unicode(self._book_img_size),
102 u"type": u"image/png"})
105 for cat in item['categories']:
106 handler.addQuickElement(u"category", u"", {u"term": cat})
109 if item['item_copyright'] is not None:
110 handler.addQuickElement(u"rights", item['item_copyright'])
113 class RootFeed(Feed):
115 title = u'Wolne Lektury'
116 link = u'http://www.wolnelektury.pl/'
117 description = u"Spis utworów na stronie http://WolneLektury.pl"
118 author_name = u"Wolne Lektury"
119 author_link = u"http://www.wolnelektury.pl/"
124 def item_title(self, item):
127 def item_link(self, item):
128 return reverse(u"opds_by_category", args=[item['category']])
130 def item_description(self, item):
131 return item['description']
134 class ByCategoryFeed(Feed):
136 link = u'http://www.wolnelektury.pl/'
137 description = u"Spis utworów na stronie http://WolneLektury.pl"
138 author_name = u"Wolne Lektury"
139 author_link = u"http://www.wolnelektury.pl/"
141 def get_object(self, request, category):
142 feed = [feed for feed in _root_feeds if feed['category']==category]
150 def title(self, feed):
153 def items(self, feed):
154 return (tag for tag in Tag.objects.filter(category=feed['category']) if tag.get_count() > 0)
156 def item_title(self, item):
159 def item_link(self, item):
160 return reverse("opds_by_tag", args=[item.category, item.slug])
162 def item_description(self):
166 class ByTagFeed(Feed):
168 link = u'http://www.wolnelektury.pl/'
169 item_enclosure_mime_type = "application/epub+zip"
170 author_name = u"Wolne Lektury"
171 author_link = u"http://www.wolnelektury.pl/"
174 return tag.get_absolute_url()
176 def title(self, tag):
179 def description(self, tag):
180 return u"Spis utworów na stronie http://WolneLektury.pl"
182 def get_object(self, request, category, slug):
183 return get_object_or_404(Tag, category=category, slug=slug)
185 def items(self, tag):
186 books = Book.tagged.with_any([tag])
187 l_tags = Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books])
188 descendants_keys = [book.pk for book in Book.tagged.with_any(l_tags)]
190 books = books.exclude(pk__in=descendants_keys)
194 def item_title(self, book):
197 def item_description(self):
200 def item_link(self, book):
201 return book.get_absolute_url()
203 def item_author_name(self, book):
205 return book.tags.filter(category='author')[0].name
209 def item_author_link(self, book):
211 return book.tags.filter(category='author')[0].get_absolute_url()
215 def item_enclosure_url(self, book):
216 return "http://%s%s" % (Site.objects.get_current().domain, book.epub_file.url)
218 def item_enclosure_length(self, book):
219 return book.epub_file.size