publisher info in generated files (except fb2)
[librarian.git] / librarian / partners.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
7 """
8 Classes for packaging ebooks for some old partners.
9 These should be removed from librarian to separate package,
10 along with custom cover images etc.
11
12 New partners shouldn't be added here, but in the partners repository.
13 """
14
15 from librarian import packagers, cover
16 from util import makedirs
17
18
19 class GandalfEpub(packagers.EpubPackager):
20     cover = cover.GandalfCover
21
22
23 class GandalfPdf(packagers.PdfPackager):
24     cover = cover.GandalfCover
25
26
27 class BookotekaEpub(packagers.EpubPackager):
28     cover = cover.BookotekaCover
29
30
31 class PrestigioEpub(packagers.EpubPackager):
32     cover = cover.PrestigioCover
33     flags = ('less-advertising',)
34
35
36 class PrestigioPdf(packagers.PdfPackager):
37     cover = cover.PrestigioCover
38     flags = ('less-advertising',)
39
40
41 class Virtualo(packagers.Packager):
42     @staticmethod
43     def utf_trunc(text, limit):
44         """ truncates text to at most `limit' bytes in utf-8 """
45         if text is None:
46             return text
47         if len(text.encode('utf-8')) > limit:
48             newlimit = limit - 3
49             while len(text.encode('utf-8')) > newlimit:
50                 text = text[:(newlimit - len(text.encode('utf-8'))) / 4]
51             text += '...'
52         return text
53
54     @classmethod
55     def prepare(cls, input_filenames, output_dir='', verbose=False):
56         from lxml import etree
57         from librarian import DirDocProvider, ParseError
58         from librarian.parser import WLDocument
59         from copy import deepcopy
60         import os.path
61
62         xml = etree.fromstring("""<?xml version="1.0" encoding="utf-8"?>
63             <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></products>""")
64         product = etree.fromstring("""<product>
65                 <publisherProductId></publisherProductId>
66                 <title></title>
67                 <info></info>
68                 <description></description>
69                 <authors>
70                     <author>
71                         <names>Jan</names>
72                         <lastName>Kowalski</lastName>
73                     </author>
74                 </authors>
75                 <price>0.0</price>
76                 <language>PL</language>
77             </product>""")
78
79         try:
80             for main_input in input_filenames:
81                 if verbose:
82                     print main_input
83                 path, fname = os.path.realpath(main_input).rsplit('/', 1)
84                 provider = DirDocProvider(path)
85                 slug, ext = os.path.splitext(fname)
86
87                 outfile_dir = os.path.join(output_dir, slug)
88                 makedirs(os.path.join(output_dir, slug))
89
90                 doc = WLDocument.from_file(main_input, provider=provider)
91                 info = doc.book_info
92
93                 product_elem = deepcopy(product)
94                 product_elem[0].text = cls.utf_trunc(slug, 100)
95                 product_elem[1].text = cls.utf_trunc(info.title, 255)
96                 product_elem[2].text = cls.utf_trunc(info.description, 255)
97                 product_elem[3].text = cls.utf_trunc(info.source_name, 3000)
98                 product_elem[4][0][0].text = cls.utf_trunc(u' '.join(info.author.first_names), 100)
99                 product_elem[4][0][1].text = cls.utf_trunc(info.author.last_name, 100)
100                 xml.append(product_elem)
101
102                 cover.VirtualoCover(info).save(os.path.join(outfile_dir, slug+'.jpg'))
103                 outfile = os.path.join(outfile_dir, '1.epub')
104                 outfile_sample = os.path.join(outfile_dir, '1.sample.epub')
105                 doc.save_output_file(doc.as_epub(), output_path=outfile)
106                 doc.save_output_file(doc.as_epub(doc, sample=25), output_path=outfile_sample)
107                 outfile = os.path.join(outfile_dir, '1.mobi')
108                 outfile_sample = os.path.join(outfile_dir, '1.sample.mobi')
109                 doc.save_output_file(doc.as_mobi(cover=cover.VirtualoCover), output_path=outfile)
110                 doc.save_output_file(
111                     doc.as_mobi(doc, cover=cover.VirtualoCover, sample=25),
112                     output_path=outfile_sample)
113         except ParseError, e:
114             print '%(file)s:%(name)s:%(message)s' % {
115                 'file': main_input,
116                 'name': e.__class__.__name__,
117                 'message': e.message
118             }
119
120         xml_file = open(os.path.join(output_dir, 'import_products.xml'), 'w')
121         xml_file.write(etree.tostring(xml, pretty_print=True, encoding=unicode).encode('utf-8'))
122         xml_file.close()