diff --git a/ncoreparser/client.py b/ncoreparser/client.py index 1dfd1ed..8792249 100644 --- a/ncoreparser/client.py +++ b/ncoreparser/client.py @@ -67,7 +67,7 @@ def search(self, pattern, type=SearchParamType.ALL_OWN, where=SearchParamWhere.N sort_by=ParamSort.UPLOAD, sort_order=ParamSeq.DECREASING, number=None): page_count = 1 torrents = [] - while number is None or len(torrents) < number: + while number is in (None, -1) or len(torrents) < number: url = URLs.DOWNLOAD_PATTERN.value.format(page=page_count, t_type=type.value, sort=sort_by.value, @@ -79,9 +79,9 @@ def search(self, pattern, type=SearchParamType.ALL_OWN, where=SearchParamWhere.N except Exception as e: raise NcoreConnectionError(f"Error while searhing torrents. {e}") from e new_torrents = [Torrent(**params) for params in self._page_parser.get_items(request.text)] + torrents.extend(new_torrents) if number is None or len(new_torrents) == 0: return torrents - torrents.extend(new_torrents) page_count += 1 return torrents[:number] diff --git a/ncoreparser/client_async.py b/ncoreparser/client_async.py index 3f818fc..6a2a9fa 100644 --- a/ncoreparser/client_async.py +++ b/ncoreparser/client_async.py @@ -69,7 +69,7 @@ async def search(self, pattern, type=SearchParamType.ALL_OWN, where=SearchParamW sort_by=ParamSort.UPLOAD, sort_order=ParamSeq.DECREASING, number=None): page_count = 1 torrents = [] - while number is None or len(torrents) < number: + while number is None or number == -1 or len(torrents) < number: url = URLs.DOWNLOAD_PATTERN.value.format(page=page_count, t_type=type.value, sort=sort_by.value, @@ -81,9 +81,9 @@ async def search(self, pattern, type=SearchParamType.ALL_OWN, where=SearchParamW except Exception as e: raise NcoreConnectionError(f"Error while searhing torrents. {e}") from e new_torrents = [Torrent(**params) for params in self._page_parser.get_items(request.text)] + torrents.extend(new_torrents) if number is None or len(new_torrents) == 0: return torrents - torrents.extend(new_torrents) page_count += 1 return torrents[:number] diff --git a/ncoreparser/parser.py b/ncoreparser/parser.py index 01547ed..baa317d 100644 --- a/ncoreparser/parser.py +++ b/ncoreparser/parser.py @@ -9,13 +9,18 @@ class TorrentsPageParser: def __init__(self): self.type_pattern = re.compile(r'' r'.*') - self.id_name_pattern = re.compile(r'') + self.id_name_poster_pattern = re.compile( + r'.*?(?:onmouseover="mutat\(\'(https:\/\/.*?)\',.*?)', + re.DOTALL + ) + self.id_name_patter = re.compile(r'') self.date_pattern = re.compile(r'
(.*?)
(.*?)
') self.size_pattern = re.compile(r'
(.*?)
') self.not_found_pattern = re.compile(r'
Nincs találat!
') self.seeders_pattern = re.compile(r'
([0-9]+)
') self.leechers_pattern = re.compile(r'
([0-9]+)
') + @staticmethod def get_key(data): key_pattern = r'Seederek:.*?
' r'(?P[0-9]+)
.*?
Leecherek:
.*?
(?P[0-9]+)
', re.DOTALL) + self.poster_image_pattern = re.compile(r']*src="(https://[^\"]+)', re.DOTALL) def get_item(self, data): try: @@ -69,9 +94,12 @@ def get_item(self, data): peers = self.peers_pattern.search(data) seed = peers.group('seed') leech = peers.group('leech') + poster_image_match = self.poster_image_pattern.search(data) + poster_image = poster_image_match.group(1) if poster_image_match else "No cover" + except AttributeError as e: raise NcoreParserError(f"Error while parsing by detailed page. {e}") from e - return {"title": title, "key": key, "date": date, "size": size, "type": t_type, 'seed': seed, 'leech': leech} + return {"title": title, "key": key, "date": date, "size": size, "type": t_type, 'seed': seed, 'leech': leech, 'poster_image': poster_image} class RssParser: diff --git a/ncoreparser/torrent.py b/ncoreparser/torrent.py index 392250c..9da61d8 100644 --- a/ncoreparser/torrent.py +++ b/ncoreparser/torrent.py @@ -4,7 +4,7 @@ class Torrent: def __init__(self, id, title, key, size, #pylint: disable=too-many-arguments - type, date, seed, leech, **params): #pylint: disable=too-many-arguments + type, date, seed, leech, poster_image, **params): #pylint: disable=too-many-arguments self._details = {} self._details["id"] = int(id) self._details["title"] = title @@ -14,6 +14,7 @@ def __init__(self, id, title, key, size, #pylint: disable=too-many-arguments self._details["date"] = date self._details["seed"] = seed self._details["leech"] = leech + self._details["poster_image"] = poster_image self._details["download"] = URLs.DOWNLOAD_LINK.value.format(id=id, key=key) self._details.update(params) diff --git a/pyproject.toml b/pyproject.toml index f92d4ae..808f4d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "hatchling.build" [project] name = "ncoreparser" -version = "2.1.2" +version = "2.2.0" description = "Package to download from ncore.pro" authors = [ { name="Aron Radics", email="aron.radics.jozsef@gmail.com" } diff --git a/tests/manual.py b/tests/manual.py index 0596a85..e813a53 100644 --- a/tests/manual.py +++ b/tests/manual.py @@ -5,21 +5,22 @@ def print_category(msg): print("") - print("*{:175}*".format("-" * 175)) - print(f"|{msg:^175}|") - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) - print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|".format("Title", "Type", "Size", "ID", "Seed", "Leech")) - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) + print("*{:276}*".format("-" * 276)) + print(f"|{msg:^276}|") + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) + print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|{:^100}|".format("Title", "Type", "Size", "ID", "Seed", "Leech", "Poster")) + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) def pretty_print(torrent): - print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|".format(torrent['title'], + print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|{:^100}|".format(torrent['title'], torrent['type'], str(torrent['size']), str(torrent['id']), torrent['seed'], - torrent['leech'])) - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) + torrent['leech'], + torrent['poster_image'])) + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) parser = argparse.ArgumentParser() @@ -65,6 +66,16 @@ def pretty_print(torrent): for torrent in torrents: pretty_print(torrent) + print_category("Test number=None") + torrents = client.search(pattern="") + for torrent in torrents: + pretty_print(torrent) + + print_category("Test number=-1") + torrents = client.search(pattern="creed", number=-1) + for torrent in torrents: + pretty_print(torrent) + client.logout() end = time.time() diff --git a/tests/manual_async.py b/tests/manual_async.py index 07a596b..65a998c 100644 --- a/tests/manual_async.py +++ b/tests/manual_async.py @@ -6,21 +6,22 @@ def print_category(msg): print("") - print("*{:175}*".format("-" * 175)) - print(f"|{msg:^175}|") - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) - print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|".format("Title", "Type", "Size", "ID", "Seed", "Leech")) - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) + print("*{:276}*".format("-" * 276)) + print(f"|{msg:^276}|") + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) + print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|{:^100}|".format("Title", "Type", "Size", "ID", "Seed", "Leech", "Poster")) + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) def pretty_print(torrent): - print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|".format(torrent['title'], + print("|{:^100}|{:^30}|{:^10}|{:^10}|{:^10}|{:^10}|{:^100}|".format(torrent['title'], torrent['type'], str(torrent['size']), str(torrent['id']), torrent['seed'], - torrent['leech'])) - print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10)) + torrent['leech'], + torrent['poster_image'])) + print("*{:^100}*{:^30}*{:^10}*{:^10}*{:^10}*{:^10}*{:^100}*".format("-" * 100, "-" * 30, "-" * 10, "-" * 10, "-" * 10, "-" * 10, "-" * 100)) parser = argparse.ArgumentParser() @@ -65,6 +66,16 @@ async def main(): for torrent in torrents: pretty_print(torrent) + print_category("Test number=None") + torrents = await client.search(pattern="") + for torrent in torrents: + pretty_print(torrent) + + print_category("Test number=-1") + torrents = await client.search(pattern="creed", number=-1) + for torrent in torrents: + pretty_print(torrent) + await client.logout() end = time.time()