Correctly handle "torrent finished" events
[qBittorrent.git] / src / searchengine / nova3 / novaprinter.py
blobe5c0b0b7461a91c1243f7f4338aedb709cfd3b7e
1 #VERSION: 1.52
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are met:
6 # * Redistributions of source code must retain the above copyright notice,
7 # this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
11 # * Neither the name of the author nor the names of its contributors may be
12 # used to endorse or promote products derived from this software without
13 # specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
27 import re
28 from typing import TypedDict, Union
30 SearchResults = TypedDict('SearchResults', {
31 'link': str,
32 'name': str,
33 'size': Union[float, int, str], # TODO: use `float | int | str` when using Python >= 3.10
34 'seeds': int,
35 'leech': int,
36 'engine_url': str,
37 'desc_link': str, # Optional # TODO: use `NotRequired[str]` when using Python >= 3.11
38 'pub_date': int # Optional # TODO: use `NotRequired[int]` when using Python >= 3.11
42 def prettyPrinter(dictionary: SearchResults) -> None:
43 outtext = "|".join((
44 dictionary["link"],
45 dictionary["name"].replace("|", " "),
46 str(anySizeToBytes(dictionary['size'])),
47 str(dictionary["seeds"]),
48 str(dictionary["leech"]),
49 dictionary["engine_url"],
50 dictionary.get("desc_link", ""), # Optional
51 str(dictionary.get("pub_date", -1)) # Optional
54 # fd 1 is stdout
55 with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout:
56 print(outtext, file=utf8stdout)
59 sizeUnitRegex: re.Pattern[str] = re.compile(r"^(?P<size>\d*\.?\d+) *(?P<unit>[a-z]+)?", re.IGNORECASE)
62 # TODO: use `float | int | str` when using Python >= 3.10
63 def anySizeToBytes(size_string: Union[float, int, str]) -> int:
64 """
65 Convert a string like '1 KB' to '1024' (bytes)
67 The canonical type for `size_string` is `str`. However numeric types are also accepted in order to
68 accommodate poorly written plugins.
69 """
71 if isinstance(size_string, int):
72 return size_string
73 if isinstance(size_string, float):
74 return round(size_string)
76 match = sizeUnitRegex.match(size_string.strip())
77 if match is None:
78 return -1
80 size = float(match.group('size')) # need to match decimals
81 unit = match.group('unit')
83 if unit is not None:
84 units_exponents = {'T': 40, 'G': 30, 'M': 20, 'K': 10}
85 exponent = units_exponents.get(unit[0].upper(), 0)
86 size *= 2**exponent
88 return round(size)