paginate feeds

pull/1/head
eric 2014-11-03 19:36:26 -05:00
parent db748ef6f8
commit 5b2147958b
2 changed files with 39 additions and 13 deletions

View File

@ -19,6 +19,7 @@ FORMAT_TO_MIMETYPE = {'pdf':"application/pdf",
facets = ["creative_commons","active_campaigns"] facets = ["creative_commons","active_campaigns"]
UNGLUEIT_URL= 'https://unglue.it' UNGLUEIT_URL= 'https://unglue.it'
NAVIGATION = "application/atom+xml;profile=opds-catalog;kind=navigation"
def feeds(): def feeds():
for facet in facets: for facet in facets:
yield globals()[facet]() yield globals()[facet]()
@ -117,8 +118,8 @@ class Facet:
feed_path = '' feed_path = ''
description = '' description = ''
def feed(self): def feed(self, page=None):
return opds_feed_for_works(self.works, self.feed_path, title=self.title) return opds_feed_for_works(self.works, self.feed_path, title=self.title, page=page)
def updated(self): def updated(self):
# return the creation date for most recently added item # return the creation date for most recently added item
@ -145,7 +146,7 @@ class active_campaigns(Facet):
editions__ebooks__isnull=False).distinct().order_by('-created') editions__ebooks__isnull=False).distinct().order_by('-created')
description= "With your help we're raising money to make these books free to the world." description= "With your help we're raising money to make these books free to the world."
def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog"): def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog", page=None):
feed_xml = """<feed xmlns:dcterms="http://purl.org/dc/terms/" feed_xml = """<feed xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:opds="http://opds-spec.org/" xmlns:opds="http://opds-spec.org/"
@ -183,17 +184,37 @@ def opds_feed_for_works(works, feed_path, title="Unglue.it Catalog"):
# links: start, self, next/prev (depending what's necessary -- to start with put all CC books) # links: start, self, next/prev (depending what's necessary -- to start with put all CC books)
# start link # start link
append_navlink(feed, 'start', feed_path, None )
start_link = etree.Element("link") # next link
start_link.attrib.update({"rel":"start",
"href":"https://unglue.it/api/opds/",
"type":"application/atom+xml;profile=opds-catalog;kind=navigation",
})
feed.append(start_link)
if not page:
page =0
else:
try:
page=int(page)
except TypeError:
page=0
for work in islice(works,None): try:
works[10 * page + 10]
append_navlink(feed, 'next', feed_path, page+1 )
except IndexError:
pass
works = islice(works, 10 * page, 10 * page + 10)
if page > 0:
append_navlink(feed, 'previous', feed_path, page-1)
for work in works:
node = work_node(work) node = work_node(work)
feed.append(node) feed.append(node)
return etree.tostring(feed, pretty_print=True) return etree.tostring(feed, pretty_print=True)
def append_navlink(feed, rel, path, page):
link = etree.Element("link")
link.attrib.update({"rel":rel,
"href": UNGLUEIT_URL + "/api/opds/" + path + ('/?page=' + unicode(page) if page!=None else '/'),
"type": NAVIGATION,
})
feed.append(link)

View File

@ -101,9 +101,14 @@ class OPDSAcquisitionView(View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
facet = kwargs.get('facet') facet = kwargs.get('facet')
page = request.GET.get('page', None)
try:
page = int(page)
except:
page = None
if facet in opds.facets: if facet in opds.facets:
facet_class = getattr(opds, facet)() facet_class = getattr(opds, facet)()
return HttpResponse(facet_class.feed(), return HttpResponse(facet_class.feed(page),
content_type="application/atom+xml;profile=opds-catalog;kind=acquisition") content_type="application/atom+xml;profile=opds-catalog;kind=acquisition")
else: else:
return HttpResponseNotFound("invalid facet") return HttpResponseNotFound("invalid facet")