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.
28 from typing
import TypedDict
, Union
30 SearchResults
= TypedDict('SearchResults', {
33 'size': Union
[float, int, str], # TODO: use `float | int | str` when using Python >= 3.10
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:
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
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:
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.
71 if isinstance(size_string
, int):
73 if isinstance(size_string
, float):
74 return round(size_string
)
76 match
= sizeUnitRegex
.match(size_string
.strip())
80 size
= float(match
.group('size')) # need to match decimals
81 unit
= match
.group('unit')
84 units_exponents
= {'T': 40, 'G': 30, 'M': 20, 'K': 10}
85 exponent
= units_exponents
.get(unit
[0].upper(), 0)