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