Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / searchengine / nova3 / novaprinter.py
blob09250dc85ce4d04f8a4632ed75ecac2b7dd43d6d
1 #VERSION: 1.46
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 def prettyPrinter(dictionary):
29 dictionary['size'] = anySizeToBytes(dictionary['size'])
30 outtext = "|".join((dictionary["link"], dictionary["name"].replace("|", " "),
31 str(dictionary["size"]), str(dictionary["seeds"]),
32 str(dictionary["leech"]), dictionary["engine_url"]))
33 if 'desc_link' in dictionary:
34 outtext = "|".join((outtext, dictionary["desc_link"]))
36 # fd 1 is stdout
37 with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout:
38 print(outtext, file=utf8stdout)
41 def anySizeToBytes(size_string):
42 """
43 Convert a string like '1 KB' to '1024' (bytes)
44 """
45 # separate integer from unit
46 try:
47 size, unit = size_string.split()
48 except:
49 try:
50 size = size_string.strip()
51 unit = ''.join([c for c in size if c.isalpha()])
52 if len(unit) > 0:
53 size = size[:-len(unit)]
54 except:
55 return -1
56 if len(size) == 0:
57 return -1
58 size = float(size)
59 if len(unit) == 0:
60 return int(size)
61 short_unit = unit.upper()[0]
63 # convert
64 units_dict = {'T': 40, 'G': 30, 'M': 20, 'K': 10}
65 if short_unit in units_dict:
66 size = size * 2**units_dict[short_unit]
67 return int(size)