Skip to content

Commit

Permalink
Show packages without description
Browse files Browse the repository at this point in the history
  • Loading branch information
shidenko97 committed Oct 16, 2022
1 parent a20734a commit 4bd6e4d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <query>`
Expand All @@ -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 <query>`.

2 changes: 1 addition & 1 deletion pypisearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.3"
__version__ = "1.3.4"
20 changes: 15 additions & 5 deletions pypisearch/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
44 changes: 26 additions & 18 deletions pypisearch/search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from typing import List, Optional

import tabulate

Expand All @@ -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."""
Expand All @@ -53,5 +62,4 @@ def tabulated_result(self) -> str:
],
tablefmt="plain",
)
or "Sorry, we haven't results by your query"
)

0 comments on commit 4bd6e4d

Please sign in to comment.