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