autocat3/CoverPages.py

100 lines
3.2 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
2020-08-27 17:39:21 +00:00
import re
2019-03-28 13:45:03 +00:00
import cherrypy
import six
2020-03-11 19:52:26 +00:00
import textwrap
2020-08-27 17:39:21 +00:00
2019-03-28 13:45:03 +00:00
from libgutenberg import GutenbergGlobals as gg
2020-08-27 17:39:21 +00:00
2019-03-28 13:45:03 +00:00
import BaseSearcher
2020-08-27 17:39:21 +00:00
class CoverPages(object):
2019-03-28 13:45:03 +00:00
""" Output a gallery of cover pages. """
2020-08-27 17:39:21 +00:00
orders = {'latest': 'release_date',
'popular': 'downloads'}
2019-03-28 13:45:03 +00:00
@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-08-27 19:37:12 +00:00
s = ''
2019-03-28 13:45:03 +00:00
for row in rows:
url = '/' + row.filename
href = '/ebooks/%d' % row.pk
2020-08-27 21:59:16 +00:00
if row.title:
title = gg.xmlspecialchars (row.title) # handles <,>,&
#Shortening long titles for latest covers
title = title.replace('"', '&quot;')
title = title.replace("'", '&apos;')
else:
title = '!! missing title !!'
2020-04-29 17:01:39 +00:00
short_title = title
2020-04-20 15:49:39 +00:00
title_len = len(title)
2020-08-27 17:39:21 +00:00
short_title = re.sub(r"\-+", " ", short_title)
2020-04-29 17:01:39 +00:00
short_title = short_title.splitlines()[0]
2020-08-27 17:39:21 +00:00
if(title_len > 80):
short_title = textwrap.wrap(short_title, 80)[0]
2020-08-27 19:37:12 +00:00
s += """
2020-08-27 17:39:21 +00:00
<a href="{href}" title="{title}" target="_top">
<div class="cover_image">
<div class="cover_img">
<img src="{url}" alt="{title}" title="{title}" draggable="false">
</div>
<div class="cover_title">
<h5>{short_title}</h5>
</div>
</div>
</a>
""".format(url=url, href=href, title=title, short_title=short_title, size=size)
return s.encode('utf-8')
2019-03-28 13:45:03 +00:00
2020-08-27 17:39:21 +00:00
def index(self, count, size, order, **kwargs):
2019-03-28 13:45:03 +00:00
""" Internal help function. """
try:
2020-08-27 17:39:21 +00:00
count = int(count)
2019-03-28 13:45:03 +00:00
if count < 1:
2020-08-27 17:39:21 +00:00
raise ValueError('count < 0')
2019-03-28 13:45:03 +00:00
if size not in ('small', 'medium'):
2020-08-27 17:39:21 +00:00
raise ValueError('bogus size')
2019-03-28 13:45:03 +00:00
order = 'books.%s' % self.orders[order]
2020-08-27 17:39:21 +00:00
rows = BaseSearcher.SQLSearcher.execute(
2019-03-28 13:45:03 +00:00
"""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),
2020-08-27 17:39:21 +00:00
{'count': count, 'size': 'cover.%s' % size,}
)
2019-03-28 13:45:03 +00:00
if rows:
2020-08-27 17:39:21 +00:00
return self.serve(rows, size)
2019-03-28 13:45:03 +00:00
except (ValueError, KeyError) as what:
2020-08-27 17:39:21 +00:00
raise cherrypy.HTTPError (400, 'Bad Request. %s' % six.text_type(what))
2019-03-28 13:45:03 +00:00
except IOError:
pass
raise cherrypy.HTTPError (500, 'Internal Server Error.')