From e791103cbbecbec2a87e9d1ab643ccb4fa36e311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Tue, 21 Feb 2023 16:48:59 +0100 Subject: webui working --- travnik.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'travnik.py') diff --git a/travnik.py b/travnik.py index 20586f4..98ef449 100644 --- a/travnik.py +++ b/travnik.py @@ -75,24 +75,36 @@ class Torrent(): for z, v in paths_r(self.files): yield z, v def matches(self, r): - if search(r, self.dict.get(b'info').get(b'name'), IGNORECASE): + try: + decoded = self.dict.get(b'info').get(b'name').decode() + except UnicodeDecodeError: + decoded = self.dict.get(b'info').get(b'name').decode("iso-8859-2") + if search(r, decoded, IGNORECASE): return True - for path, size in paths(self): - if search(r, path, IGNORECASE): + for path, size in self.paths(): + try: + decd = b'/'.join(path).decode() + except UnicodeDecodeError: + decd = b'/'.join(path).decode("iso-8859-2") + if search(r, decd, IGNORECASE): return True return False - def matching_files(self, r): - def matching_files_r(dir, r): + def matching_files(self, r, decode=False): + def matching_files_r(dirc, r, decode): files = {} - for name, content in self.paths: - if search(r, name, IGNORECASE): - files[name] = content + for name, content in dirc.items(): + try: + decoded = name.decode() + except UnicodeDecodeError: + decoded = name.decode("iso-8859-2") # TODO we could try detecting the encoding + if search(r, decoded, IGNORECASE): + files[decoded if decode else name] = content if type(content) is dict: - inhalt = matching_files_r(content, r) + inhalt = matching_files_r(content, r, decode) if inhalt: - files[name] = inhalt + files[decoded if decode else name] = inhalt return files - return matching_files_r(self.paths, r) + return matching_files_r(self.files, r, decode) def __repr__(self): return str(self.__dict__) def __hash__(self): -- cgit v1.2.3