autocat3/CoverPages.py

97 lines
3.3 KiB
Python
Raw Normal View History

2019-03-28 13:45:03 +00:00
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: utf-8 -*-
"""
CoverPages.py
Copyright 2009-2010 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
Serve cover images of most popular and latest ebooks.
"""
from __future__ import unicode_literals
import cherrypy
import six
2020-03-11 19:52:26 +00:00
import textwrap
2019-03-28 13:45:03 +00:00
from libgutenberg import GutenbergGlobals as gg
2020-04-15 17:43:27 +00:00
import re
2019-03-28 13:45:03 +00:00
import BaseSearcher
class CoverPages (object):
""" Output a gallery of cover pages. """
orders = { 'latest': 'release_date',
'popular': 'downloads' }
@staticmethod
def serve (rows, size):
""" Output a gallery of coverpages. """
cherrypy.response.headers['Content-Type'] = 'text/html; charset=utf-8'
cherrypy.response.headers['Content-Language'] = 'en'
2020-04-15 17:43:27 +00:00
s = """<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" xml:base="http://www.gutenberg.org">-->
<!--<head>
2019-03-28 13:45:03 +00:00
<title>Cover Flow</title>
2020-04-15 17:43:27 +00:00
</head>-->
<!--<body><div>-->"""
2019-03-28 13:45:03 +00:00
for row in rows:
url = '/' + row.filename
href = '/ebooks/%d' % row.pk
title = gg.xmlspecialchars (row.title)
2020-04-29 17:01:39 +00:00
#Shortening long titles for latest covers
short_title = title
short_title = short_title.replace ('"', '&quot;')
2020-04-20 15:49:39 +00:00
title_len = len(title)
2020-04-29 17:01:39 +00:00
short_title = re.sub(r"\-+"," ",short_title)
2020-04-20 15:49:39 +00:00
#title = re.sub (r"\-+"," ",title)
2020-04-15 17:43:27 +00:00
#new_title= re.sub(r'\-+',' ',title)
2020-04-29 17:01:39 +00:00
short_title = short_title.splitlines()[0]
2020-03-11 19:52:26 +00:00
if(title_len>80):
2020-04-29 17:01:39 +00:00
short_title = textwrap.wrap(short_title,80)[0]
2019-03-28 13:45:03 +00:00
2020-04-20 15:49:39 +00:00
s += """<a href="{href}" title="{title}" target="_top"><div class="cover_image">
<div class="cover_img"><img src="{url}" alt="{title}" title="{title}" draggable="false">
2020-04-29 17:01:39 +00:00
</div><div class="cover_title"><h5>{short_title}</h5></div></div></a>\n""".format (
url = url, href = href, title = title, short_title = short_title, size = size)
2020-03-11 19:52:26 +00:00
return (s + '<!--</div></body></html>-->\n').encode ('utf-8')
2019-03-28 13:45:03 +00:00
2019-03-28 13:45:03 +00:00
def index (self, count, size, order, **kwargs):
""" Internal help function. """
try:
count = int (count)
if count < 1:
raise ValueError ('count < 0')
if size not in ('small', 'medium'):
raise ValueError ('bogus size')
order = 'books.%s' % self.orders[order]
rows = BaseSearcher.SQLSearcher.execute (
"""SELECT files.filename, books.pk, books.title FROM files, books
WHERE files.fk_books = books.pk
AND files.diskstatus = 0
AND files.fk_filetypes = %%(size)s
ORDER BY %s DESC
OFFSET 1 LIMIT %%(count)s -- %s""" % (order, cherrypy.request.remote.ip),
{ 'count': count,
'size': 'cover.%s' % size,
})
if rows:
return self.serve (rows, size)
except (ValueError, KeyError) as what:
raise cherrypy.HTTPError (400, 'Bad Request. %s' % six.text_type (what))
except IOError:
pass
raise cherrypy.HTTPError (500, 'Internal Server Error.')