IGN:Cleanup stanza server code

This commit is contained in:
Kovid Goyal 2009-09-10 22:28:00 -06:00
parent 6fea6a8fa7
commit c8d2b4468f

View File

@ -105,7 +105,7 @@ class LibraryServer(object):
<entry xmlns:py="http://genshi.edgewall.org/"> <entry xmlns:py="http://genshi.edgewall.org/">
<title>${authors}</title> <title>${authors}</title>
<id>urn:calibre:${record[FM['id']]}</id> <id>urn:calibre:${record[FM['id']]}</id>
<updated>${timestamp}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?authorid=${record[FM['id']]}" /> <link type="application/atom+xml" href="/stanza/?authorid=${record[FM['id']]}" />
</entry> </entry>
''')) '''))
@ -114,7 +114,7 @@ class LibraryServer(object):
<entry xmlns:py="http://genshi.edgewall.org/"> <entry xmlns:py="http://genshi.edgewall.org/">
<title>${tags}</title> <title>${tags}</title>
<id>urn:calibre:${record[FM['id']]}</id> <id>urn:calibre:${record[FM['id']]}</id>
<updated>${timestamp}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?tagid=${record[FM['id']]}" /> <link type="application/atom+xml" href="/stanza/?tagid=${record[FM['id']]}" />
</entry> </entry>
''')) '''))
@ -123,7 +123,7 @@ class LibraryServer(object):
<entry xmlns:py="http://genshi.edgewall.org/"> <entry xmlns:py="http://genshi.edgewall.org/">
<title>${series}</title> <title>${series}</title>
<id>urn:calibre:${record[FM['id']]}</id> <id>urn:calibre:${record[FM['id']]}</id>
<updated>${timestamp}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?seriesid=${record[FM['id']]}" /> <link type="application/atom+xml" href="/stanza/?seriesid=${record[FM['id']]}" />
</entry> </entry>
''')) '''))
@ -167,30 +167,35 @@ class LibraryServer(object):
<id>urn:uuid:fc000fa0-8c23-11de-a31d-0002a5d5c51b</id> <id>urn:uuid:fc000fa0-8c23-11de-a31d-0002a5d5c51b</id>
<updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?sortby=byauthor" /> <link type="application/atom+xml" href="/stanza/?sortby=byauthor" />
<content type="text">Books sorted by Author</content>
</entry> </entry>
<entry> <entry>
<title>By Title</title> <title>By Title</title>
<id>urn:uuid:1df4fe40-8c24-11de-b4c6-0002a5d5c51b</id> <id>urn:uuid:1df4fe40-8c24-11de-b4c6-0002a5d5c51b</id>
<updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?sortby=bytitle" /> <link type="application/atom+xml" href="/stanza/?sortby=bytitle" />
<content type="text">Books sorted by Title</content>
</entry> </entry>
<entry> <entry>
<title>By Newest</title> <title>By Newest</title>
<id>urn:uuid:3c6d4940-8c24-11de-a4d7-0002a5d5c51b</id> <id>urn:uuid:3c6d4940-8c24-11de-a4d7-0002a5d5c51b</id>
<updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?sortby=bynewest" /> <link type="application/atom+xml" href="/stanza/?sortby=bynewest" />
<content type="text">Books sorted by Date</content>
</entry> </entry>
<entry> <entry>
<title>By Tag</title> <title>By Tag</title>
<id>urn:uuid:824921e8-db8a-4e61-7d38-f1ce41502853</id> <id>urn:uuid:824921e8-db8a-4e61-7d38-f1ce41502853</id>
<updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?sortby=bytag" /> <link type="application/atom+xml" href="/stanza/?sortby=bytag" />
<content type="text">Books sorted by Tags</content>
</entry> </entry>
<entry> <entry>
<title>By Series</title> <title>By Series</title>
<id>urn:uuid:512a5e50-a88f-f6b8-82aa-8f129c719f61</id> <id>urn:uuid:512a5e50-a88f-f6b8-82aa-8f129c719f61</id>
<updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated> <updated>${updated.strftime('%Y-%m-%dT%H:%M:%S+00:00')}</updated>
<link type="application/atom+xml" href="/stanza/?sortby=byseries" /> <link type="application/atom+xml" href="/stanza/?sortby=byseries" />
<content type="text">Books sorted by Series</content>
</entry> </entry>
</feed> </feed>
''')) '''))
@ -375,9 +380,11 @@ class LibraryServer(object):
updated = self.db.last_modified() updated = self.db.last_modified()
cherrypy.response.headers['Last-Modified'] = self.last_modified(updated) cherrypy.response.headers['Last-Modified'] = self.last_modified(updated)
cherrypy.response.headers['Content-Type'] = 'text/xml' cherrypy.response.headers['Content-Type'] = 'text/xml'
# Main feed
if not sortby and not search and not authorid and not tagid and not seriesid: if not sortby and not search and not authorid and not tagid and not seriesid:
return self.STANZA_MAIN.generate(subtitle='', data=books, FM=FIELD_MAP, return self.STANZA_MAIN.generate(subtitle='', data=books, FM=FIELD_MAP,
updated=updated, id='urn:calibre:main').render('xml') updated=updated, id='urn:calibre:main').render('xml')
# Sub-feed
if authorid: if authorid:
authorid=int(authorid) authorid=int(authorid)
au = self.db.authors(authorid, index_is_id=True) au = self.db.authors(authorid, index_is_id=True)
@ -393,6 +400,8 @@ class LibraryServer(object):
else: else:
ids = self.db.data.parse(search) if search and search.strip() else self.db.data.universal_set() ids = self.db.data.parse(search) if search and search.strip() else self.db.data.universal_set()
record_list = list(iter(self.db)) record_list = list(iter(self.db))
# Sort the record list
if sortby == "byauthor": if sortby == "byauthor":
record_list.sort(lambda x, y: cmp(x[FIELD_MAP['author_sort']], y[FIELD_MAP['author_sort']])) record_list.sort(lambda x, y: cmp(x[FIELD_MAP['author_sort']], y[FIELD_MAP['author_sort']]))
elif sortby == "bytag": elif sortby == "bytag":
@ -406,6 +415,8 @@ class LibraryServer(object):
record_list.sort(lambda x, y: cmp(x[FIELD_MAP['series_index']], y[FIELD_MAP['series_index']])) record_list.sort(lambda x, y: cmp(x[FIELD_MAP['series_index']], y[FIELD_MAP['series_index']]))
else: else:
record_list = reversed(record_list) record_list = reversed(record_list)
author_list=[] author_list=[]
tag_list=[] tag_list=[]
series_list=[] series_list=[]
@ -413,74 +424,69 @@ class LibraryServer(object):
if record[0] not in ids: continue if record[0] not in ids: continue
r = record[FIELD_MAP['formats']] r = record[FIELD_MAP['formats']]
r = r.upper() if r else '' r = r.upper() if r else ''
if 'EPUB' in r or 'PDB' in r: if not ('EPUB' in r or 'PDB' in r):
z = record[FIELD_MAP['authors']] continue
if not z:
z = _('Unknown') z = record[FIELD_MAP['authors']]
authors = ' & '.join([i.replace('|', ',') for i in if not z:
z.split(',')]) z = _('Unknown')
extra = [] authors = ' & '.join([i.replace('|', ',') for i in
rating = record[FIELD_MAP['rating']] z.split(',')])
if rating > 0:
rating = ''.join(repeat('&#9733;', rating)) # Setup extra description
extra.append('RATING: %s<br />'%rating) extra = []
tags = record[FIELD_MAP['tags']] rating = record[FIELD_MAP['rating']]
if tags: if rating > 0:
extra.append('TAGS: %s<br />'%\ rating = ''.join(repeat('&#9733;', rating))
prepare_string_for_xml(', '.join(tags.split(',')))) extra.append('RATING: %s<br />'%rating)
series = record[FIELD_MAP['series']] tags = record[FIELD_MAP['tags']]
if series: if tags:
extra.append('SERIES: %s [%s]<br />'%\ extra.append('TAGS: %s<br />'%\
(prepare_string_for_xml(series), prepare_string_for_xml(', '.join(tags.split(','))))
fmt_sidx(float(record[FIELD_MAP['series_index']])))) series = record[FIELD_MAP['series']]
fmt = 'epub' if 'EPUB' in r else 'pdb' if series:
mimetype = guess_type('dummy.'+fmt)[0] extra.append('SERIES: %s [%s]<br />'%\
if sortby == "byauthor": (prepare_string_for_xml(series),
if authors and authors not in author_list: fmt_sidx(float(record[FIELD_MAP['series_index']]))))
author_list.append(authors)
books.append(self.STANZA_AUTHOR_ENTRY.generate( fmt = 'epub' if 'EPUB' in r else 'pdb'
authors=authors, mimetype = guess_type('dummy.'+fmt)[0]
record=record, FM=FIELD_MAP,
port=self.opts.port, # Create the sub-catalog, which is either a list of
extra=''.join(extra), # authors/tags/series or a list of books
mimetype=mimetype, data = dict(
fmt=fmt, record=record,
timestamp=strftime('%Y-%m-%dT%H:%M:%S+00:00', record[5]), updated=updated,
).render('xml').decode('utf8')) authors=authors,
elif sortby == "bytag": tags=tags,
if tags and tags not in tag_list: series=series,
tag_list.append(tags) FM=FIELD_MAP
books.append(self.STANZA_TAG_ENTRY.generate( )
tags=tags, if sortby == "byauthor":
record=record, FM=FIELD_MAP, if authors and authors not in author_list:
port=self.opts.port, author_list.append(authors)
extra=''.join(extra), books.append(self.STANZA_AUTHOR_ENTRY.generate(**data)\
mimetype=mimetype, .render('xml').decode('utf8'))
fmt=fmt,
timestamp=strftime('%Y-%m-%dT%H:%M:%S+00:00', record[5]), elif sortby == "bytag":
).render('xml').decode('utf8')) if tags and tags not in tag_list:
elif sortby == "byseries": tag_list.append(tags)
if series and series not in series_list: books.append(self.STANZA_TAG_ENTRY.generate(**data)\
series_list.append(series) .render('xml').decode('utf8'))
books.append(self.STANZA_SERIES_ENTRY.generate(
series=series, elif sortby == "byseries":
record=record, FM=FIELD_MAP, if series and series not in series_list:
port=self.opts.port, series_list.append(series)
extra=''.join(extra), books.append(self.STANZA_SERIES_ENTRY.generate(**data)\
mimetype=mimetype, .render('xml').decode('utf8'))
fmt=fmt,
timestamp=strftime('%Y-%m-%dT%H:%M:%S+00:00', record[5]), else: # An actual book list
).render('xml').decode('utf8')) data['extra'] = ''.join(extra)
else: data['mimetype'] = mimetype
books.append(self.STANZA_ENTRY.generate( data['fmt'] = fmt
authors=authors, data['timestamp'] = strftime('%Y-%m-%dT%H:%M:%S+00:00', record[5])
record=record, FM=FIELD_MAP, books.append(self.STANZA_ENTRY.generate(**data)\
port=self.opts.port, .render('xml').decode('utf8'))
extra=''.join(extra),
mimetype=mimetype,
fmt=fmt,
timestamp=strftime('%Y-%m-%dT%H:%M:%S+00:00', record[5]),
).render('xml').decode('utf8'))
return self.STANZA.generate(subtitle='', data=books, FM=FIELD_MAP, return self.STANZA.generate(subtitle='', data=books, FM=FIELD_MAP,
updated=updated, id='urn:calibre:main').render('xml') updated=updated, id='urn:calibre:main').render('xml')