Experimetal Woblink.
[redakcja.git] / src / depot / publishers / base.py
1 import requests
2 from librarian.html import transform_abstrakt
3 from slugify import slugify
4
5
6 class BasePublisher:
7     def __init__(self, username, password, publisher_handle):
8         self.username = username
9         self.password = password
10         self.publisher_handle = publisher_handle
11         self._session = None
12
13     @property
14     def session(self):
15         if self._session is None:
16             self._session = requests.Session()
17             self.login()
18         return self._session
19
20     def send_book(self, shop, book, changes=None):
21         raise NotImplementedError()
22
23     def get_description(self, wlbook, description_add=''):
24         description = ''
25         abstract = wlbook.tree.find('.//abstrakt')
26         if abstract is not None:
27             description = transform_abstrakt(abstract)
28         description += description_add
29         description += '<p>'
30         description += ', '.join(
31             '<a href="https://wolnelektury.pl/katalog/autor/{}/">{}</a>'.format(
32                 slugify(p.readable()),
33                 p.readable(),
34             )
35             for p in wlbook.meta.authors
36         ) + '<br>'
37         description += '<a href="https://wolnelektury.pl/katalog/lektura/{}/">{}</a><br>'.format(
38             wlbook.meta.url.slug,
39             wlbook.meta.title
40         )
41         if wlbook.meta.translators:
42             description += 'tłum. ' + ', '.join(p.readable() for p in wlbook.meta.translators) + '<br>'
43         description += 'Epoka: ' + ', '.join(
44             '<a href="https://wolnelektury.pl/katalog/epoka/{}/">{}</a>'.format(
45                 slugify(p),
46                 p,
47             )
48             for p in wlbook.meta.epochs
49         ) + ' '
50         description += 'Rodzaj: ' + ', '.join(
51             '<a href="https://wolnelektury.pl/katalog/rodzaj/{}/">{}</a>'.format(
52                 slugify(p),
53                 p,
54             )
55             for p in wlbook.meta.kinds
56         ) + ' '
57         description += 'Gatunek: ' + ', '.join(
58             '<a href="https://wolnelektury.pl/katalog/gatunek/{}/">{}</a>'.format(
59                 slugify(p),
60                 p,
61             )
62             for p in wlbook.meta.genres
63         ) + '</p>'
64
65         # TODO: Move away from using audiences for this.
66         if wlbook.meta.audience in ('L', 'SP1', 'SP2', 'SP3', 'SP4'):
67             description += '<p><em>{}</em> to lektura szkolna.'.format(wlbook.meta.title)
68             if wlbook.tree.find('//pe') is not None:
69                 description += '<br>Ebook <em>{title}</em> zawiera przypisy opracowane specjalnie dla uczennic i uczniów {school}.'.format(
70                     title=wlbook.meta.title,
71                     school='szkoły podstawowej' if wlbook.meta.audience.startswith('SP') else 'liceum i technikum'
72                 )
73             description += '</p>'
74         return description
75