Fundraising in PDF.
[wolnelektury.git] / src / isbn / utils.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from librarian import RDFNS, DCNS
5
6
7 FORMATS = ('PDF', 'HTML', 'TXT', 'EPUB', 'MOBI')
8
9 FORMATS_WITH_CHILDREN = ('PDF', 'EPUB', 'MOBI')
10
11 PRODUCT_FORMS = {
12     'HTML': 'EC',
13     'PDF': 'EB',
14     'TXT': 'EB',
15     'EPUB': 'ED',
16     'MOBI': 'ED',
17     'SOFT': 'BC',
18 }
19
20 PRODUCT_FORM_DETAILS = {
21     'HTML': 'E105',
22     'PDF': 'E107',
23     'TXT': 'E112',
24     'EPUB': 'E101',
25     'MOBI': 'E127',
26 }
27
28 PRODUCT_FORMATS = {
29     'E105': ('html', 'text/html'),
30     'E107': ('pdf', 'application/pdf'),
31     'E112': ('txt', 'text/plain'),
32     'E101': ('epub', 'application/epub+zip'),
33     'E127': ('mobi', 'application/x-mobipocket-ebook'),
34 }
35
36 VOLUME_SEPARATORS = ('. część ', ', część ', ', tom ', '. der tragödie ')
37
38
39 def is_institution(name):
40     return name.startswith('Zgromadzenie Ogólne')
41
42
43 def get_volume(title):
44     for volume_separator in VOLUME_SEPARATORS:
45         if volume_separator in title.lower():
46             vol_idx = title.lower().index(volume_separator)
47             stripped = title[:vol_idx]
48             vol_name = title[vol_idx + 2:]
49             return stripped, vol_name
50     return title, ''
51
52
53 def dc_values(desc, tag):
54     return [e.text.strip() for e in desc.findall('.//' + DCNS(tag))]
55
56
57 def isbn_data(wldoc, file_format=None):
58     desc = wldoc.edoc.find('.//' + RDFNS('Description'))
59     title, volume = get_volume(dc_values(desc, 'title')[0])
60     author = '; '.join(author.strip() for author in dc_values(desc, 'creator'))
61     data = {
62         'imprint': '; '.join(dc_values(desc, 'publisher')),
63         'title': title,
64         'subtitle': '',
65         'year': '',
66         'part_number': volume,
67         'name': author if not is_institution(author) else '',
68         'corporate_name': author if is_institution(author) else '',
69         'edition_type': 'DGO',
70         'edition_number': '1',
71         'language': dc_values(desc, 'language')[0],
72         'dc_slug': wldoc.book_info.url.slug,
73     }
74     if file_format:
75         data['product_form'] = PRODUCT_FORMS[file_format]
76         data['product_form_detail'] = PRODUCT_FORM_DETAILS[file_format]
77         return data
78     else:
79         has_children = len(dc_values(desc, 'relation.hasPart')) > 0
80         data['formats'] = FORMATS_WITH_CHILDREN if has_children else FORMATS
81         return data