Str/bytes fix for publishing audiobooks.
[wolnelektury.git] / src / opds / views.py
index 0c31cd2..f14696b 100644 (file)
@@ -1,9 +1,9 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+from functools import reduce
 import os.path
-from urlparse import urljoin
+from urllib.parse import urljoin
 
 from django.contrib.syndication.views import Feed
 from django.core.urlresolvers import reverse
@@ -78,14 +78,14 @@ class OPDSFeed(Atom1Feed):
 
     _book_parent_img = lazy(lambda: full_url(os.path.join(settings.STATIC_URL, "img/book-parent.png")), str)()
     try:
-        _book_parent_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png")))
-    except IOError:
+        _book_parent_img_size = str(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book-parent.png")))
+    except OSError:
         _book_parent_img_size = ''
 
     _book_img = lazy(lambda: full_url(os.path.join(settings.STATIC_URL, "img/book.png")), str)()
     try:
-        _book_img_size = unicode(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png")))
-    except IOError:
+        _book_img_size = str(os.path.getsize(os.path.join(settings.STATIC_ROOT, "img/book.png")))
+    except OSError:
         _book_img_size = ''
 
     def add_root_elements(self, handler):
@@ -104,7 +104,7 @@ class OPDSFeed(Atom1Feed):
         handler.addQuickElement(u"title", item['title'])
 
         # add a OPDS Navigation link if there's no enclosure
-        if item['enclosure'] is None:
+        if not item.get('enclosures') is None:
             handler.addQuickElement(
                 u"link", u"", {u"href": item['link'], u"rel": u"subsection", u"type": u"application/atom+xml"})
             # add a "green book" icon
@@ -144,14 +144,14 @@ class OPDSFeed(Atom1Feed):
             handler.addQuickElement(u"summary", item['description'], {u"type": u"text"})
 
         # Enclosure as OPDS Acquisition Link
-        if item['enclosure'] is not None:
+        for enc in item.get('enclosures', []):
             handler.addQuickElement(
                 u"link", '',
                 {
                     u"rel": u"http://opds-spec.org/acquisition",
-                    u"href": item['enclosure'].url,
-                    u"length": item['enclosure'].length,
-                    u"type": item['enclosure'].mime_type,
+                    u"href": enc.url,
+                    u"length": enc.length,
+                    u"type": enc.mime_type,
                 })
             # add a "red book" icon
             handler.addQuickElement(