From 4bd6e4d4862392d53a388163af35b2058e532bb9 Mon Sep 17 00:00:00 2001 From: shidenko97 Date: Sun, 16 Oct 2022 14:50:16 +0300 Subject: [PATCH] Show packages without description --- README.md | 2 +- pypisearch/__init__.py | 2 +- pypisearch/main.py | 20 ++++++++++++++----- pypisearch/search.py | 44 +++++++++++++++++++++++++----------------- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ef991c2..cd0d45d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![License](https://img.shields.io/github/license/shidenko97/pypisearch)](https://github.com/shidenko97/pypisearch/blob/master/LICENSE) One more replacement of temporarily deprecated pip search command implemented on regular expressions and uses [pypi site](https://pypi.org/) search line. +You are able to query multiple pages providing range to page argument (e.g. 1-5). ## Install and run - Clone this repo and run `python pypisearch ` @@ -14,4 +15,3 @@ One more replacement of temporarily deprecated pip search command implemented on - Clone this repo, cd inside and run `pip install .` - Run from anywhere `pypisearch `. - diff --git a/pypisearch/__init__.py b/pypisearch/__init__.py index 7b1e312..f9e47b6 100644 --- a/pypisearch/__init__.py +++ b/pypisearch/__init__.py @@ -1 +1 @@ -__version__ = "1.3.3" +__version__ = "1.3.4" diff --git a/pypisearch/main.py b/pypisearch/main.py index b657a3e..b86277c 100644 --- a/pypisearch/main.py +++ b/pypisearch/main.py @@ -16,16 +16,26 @@ def main() -> None: arg_parser.add_argument( "-p", "--page", - default=1, + default="1", metavar="page", - type=int, - help="search page (default 1)", + type=str, + help="search page (default 1, could be range of pages like 1-5)", ) args = arg_parser.parse_args() + page = args.page + + if "-" in page: + page_from, page_to = args.page.split("-") + if page_from > page_to: + raise ValueError("Page from shouldn't be greater then page to") + else: + page_from = page + page_to = None + # Parse search url and print result table - search = Search(query=args.q, page=args.page) - print(search.tabulated_result) + search = Search(query=args.q, page_from=page_from, page_to=page_to) + print(search.tabulated_result or "No results") if __name__ == "__main__": diff --git a/pypisearch/search.py b/pypisearch/search.py index 6d36016..31866f1 100644 --- a/pypisearch/search.py +++ b/pypisearch/search.py @@ -1,4 +1,5 @@ import requests +from typing import List, Optional import tabulate @@ -8,36 +9,44 @@ class Search: """Main search process instance.""" + pypi_search_url = "https://pypi.org/search/?q={query}&page={page}" - def __init__(self, query: str, page: int = 1) -> None: - self.search_url = f"https://pypi.org/search/?q={query}&page={page}" + def __init__( + self, + query: str, + page_from: str = "", + page_to: Optional[str] = None, + ) -> None: + page_from, page_to = int(page_from), int(page_to) if page_to else None + self.result = [] - @property - def get_page_data(self) -> str: - """Returns page's HTML code.""" - - return requests.get(url=self.search_url).text - - @property - def plain_items(self) -> list: - """Returns plain result items.""" + if page_to is None: + self.result = self.download_data(query=query, page=page_from) + else: + for page in range(page_from, page_to + 1): + result = self.download_data(query=query, page=page) - return const.ITEM_RE.split(self.get_page_data) + if not result: + break - @property - def result(self) -> list: - """Returns list of result instances.""" + self.result.extend(result) - return list( + def download_data(self, *, query: str, page: int) -> List[ResultItem]: + url = self.pypi_search_url.format(query=query, page=page) + page_data = requests.get(url=url).text + items = const.ITEM_RE.split(page_data) + result = list( filter( lambda result_item: not result_item.is_empty, map( lambda plain_item: ResultItem(plain_text=plain_item), - self.plain_items, + items, ), ) ) + return result + @property def tabulated_result(self) -> str: """Returns tabulated list of results.""" @@ -53,5 +62,4 @@ def tabulated_result(self) -> str: ], tablefmt="plain", ) - or "Sorry, we haven't results by your query" )