[Mobi] Fix for inconsistent styles between on-line reader and Mobi.
[librarian.git] / librarian / packagers.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import os
7 from copy import deepcopy
8 from lxml import etree
9 from librarian import pdf, epub, DirDocProvider, ParseError, cover
10 from librarian.parser import WLDocument
11
12
13 class Packager(object):
14     cover = None
15     flags = None
16
17     @classmethod
18     def prepare_file(cls, main_input, output_dir, verbose=False):
19         path, fname = os.path.realpath(main_input).rsplit('/', 1)
20         provider = DirDocProvider(path)
21         slug, ext = os.path.splitext(fname)
22
23         if output_dir != '':
24             try:
25                 os.makedirs(output_dir)
26             except:
27                 pass
28         outfile = os.path.join(output_dir, slug + '.' + cls.ext)
29
30         doc = WLDocument.from_file(main_input, provider=provider)
31         output_file = cls.converter.transform(doc,
32                 cover=cls.cover, flags=cls.flags)
33         doc.save_output_file(output_file, output_path=outfile)
34
35
36     @classmethod
37     def prepare(cls, input_filenames, output_dir='', verbose=False):
38         try:
39             for main_input in input_filenames:
40                 if verbose:
41                     print main_input
42                 cls.prepare_file(main_input, output_dir, verbose)
43         except ParseError, e:
44             print '%(file)s:%(name)s:%(message)s' % {
45                 'file': main_input,
46                 'name': e.__class__.__name__,
47                 'message': e.message
48             }
49
50
51 class EpubPackager(Packager):
52     converter = epub
53     ext = 'epub'
54
55 class PdfPackager(Packager):
56     converter = pdf
57     ext = 'pdf'
58
59
60 class GandalfEpubPackager(EpubPackager):
61     cover = cover.GandalfCover
62
63 class GandalfPdfPackager(PdfPackager):
64     cover = cover.GandalfCover
65
66 class BookotekaEpubPackager(EpubPackager):
67     cover = cover.BookotekaCover
68
69 class PrestigioEpubPackager(EpubPackager):
70     cover = cover.PrestigioCover
71     flags = ('less-advertising',)
72
73 class PrestigioPdfPackager(PdfPackager):
74     cover = cover.PrestigioCover
75     flags = ('less-advertising',)
76
77
78 class VirtualoPackager(Packager):
79     @staticmethod
80     def utf_trunc(text, limit):
81         """ truncates text to at most `limit' bytes in utf-8 """
82         if text is None:
83             return text
84         if len(text.encode('utf-8')) > limit:
85             newlimit = limit - 3
86             while len(text.encode('utf-8')) > newlimit:
87                 text = text[:(newlimit - len(text.encode('utf-8'))) / 4]
88             text += '...'
89         return text
90
91     @classmethod
92     def prepare(cls, input_filenames, output_dir='', verbose=False):
93         xml = etree.fromstring("""<?xml version="1.0" encoding="utf-8"?>
94             <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></products>""")
95         product = etree.fromstring("""<product>
96                 <publisherProductId></publisherProductId>
97                 <title></title>
98                 <info></info>
99                 <description></description>
100                 <authors>
101                     <author>
102                         <names>Jan</names>
103                         <lastName>Kowalski</lastName>
104                     </author>
105                 </authors>
106                 <price>0.0</price>
107                 <language>PL</language>
108             </product>""")
109
110         try:
111             for main_input in input_filenames:
112                 if verbose:
113                     print main_input
114                 path, fname = os.path.realpath(main_input).rsplit('/', 1)
115                 provider = DirDocProvider(path)
116                 slug, ext = os.path.splitext(fname)
117
118                 outfile_dir = os.path.join(output_dir, slug)
119                 os.makedirs(os.path.join(output_dir, slug))
120
121                 doc = WLDocument.from_file(main_input, provider=provider)
122                 info = doc.book_info
123
124                 product_elem = deepcopy(product)
125                 product_elem[0].text = cls.utf_trunc(slug, 100)
126                 product_elem[1].text = cls.utf_trunc(info.title, 255)
127                 product_elem[2].text = cls.utf_trunc(info.description, 255)
128                 product_elem[3].text = cls.utf_trunc(info.source_name, 3000)
129                 product_elem[4][0][0].text = cls.utf_trunc(u' '.join(info.author.first_names), 100)
130                 product_elem[4][0][1].text = cls.utf_trunc(info.author.last_name, 100)
131                 xml.append(product_elem)
132
133                 cover.VirtualoCover(info).save(os.path.join(outfile_dir, slug+'.jpg'))
134                 outfile = os.path.join(outfile_dir, '1.epub')
135                 outfile_sample = os.path.join(outfile_dir, '1.sample.epub')
136                 doc.save_output_file(doc.as_epub(),
137                         output_path=outfile)
138                 doc.save_output_file(doc.as_epub(doc, sample=25), 
139                         output_path=outfile_sample)
140                 outfile = os.path.join(outfile_dir, '1.mobi')
141                 outfile_sample = os.path.join(outfile_dir, '1.sample.mobi')
142                 doc.save_output_file(doc.as_mobi(cover=cover.VirtualoCover),
143                         output_path=outfile)
144                 doc.save_output_file(
145                         doc.as_mobi(doc, cover=cover.VirtualoCover, sample=25), 
146                         output_path=outfile_sample)
147         except ParseError, e:
148             print '%(file)s:%(name)s:%(message)s' % {
149                 'file': main_input,
150                 'name': e.__class__.__name__,
151                 'message': e.message
152             }
153
154         xml_file = open(os.path.join(output_dir, 'import_products.xml'), 'w')
155         xml_file.write(etree.tostring(xml, pretty_print=True, encoding=unicode).encode('utf-8'))
156         xml_file.close()