Update workflows/publish_pypi.yml
[manga-dl.git] / manga_py / base_classes / static.py
blob14259782ba70d1ce3afd13a29364c002406a4bc7
1 from typing import Union, List
2 from lxml.html import document_fromstring, HtmlElement
3 # from purifier.purifier import HTMLPurifier
6 class Static:
8 # @staticmethod
9 # def _clear_html(body):
10 # purifier = HTMLPurifier({
11 # 'div': ['*'], 'span': ['*'],
12 # 'img': ['*'], 'a': ['*'],
13 # 'h1': ['*'], 'h2': ['*'],
14 # 'h3': ['*'], 'h4': ['*'],
15 # 'h5': ['*'], 'h6': ['*'],
16 # })
17 # return purifier.feed(body)
19 @staticmethod
20 def document_fromstring(body, selector: str = None, idx: int = None) -> Union[HtmlElement, List[HtmlElement]]: # pragma: no cover
21 if len(body) == 0:
22 raise RuntimeError('Content has empty!')
24 result = document_fromstring(body) # todo
25 if isinstance(selector, str):
26 result = result.cssselect(selector)
27 if isinstance(idx, int):
28 result = result[idx]
29 return result
31 @staticmethod
32 def _set_if_not_none(var, key, value): # pragma: no cover
33 if value is not None:
34 var[key] = value
36 @staticmethod
37 def __test_ascii(i):
38 o = ord(i)
39 _ = 39 < o < 127
40 _ = _ and o not in [42, 47, 92, 94]
41 return _ or o > 161
43 @staticmethod
44 def remove_not_ascii(value):
45 return "".join(i for i in value if i == '_' or Static.__test_ascii(i))