|
| 1 | +import logging |
| 2 | +from dataclasses import dataclass, asdict |
| 3 | +from itertools import count |
| 4 | +from typing import Iterator |
| 5 | + |
| 6 | +import requests |
| 7 | +from bs4 import BeautifulSoup |
| 8 | +from more_itertools import one |
| 9 | +from ratelimit import limits |
| 10 | +from ratelimit.exception import RateLimitException |
| 11 | +from tabulate import tabulate |
| 12 | +from tenacity import retry, retry_if_exception_type, wait_fixed |
| 13 | +from tqdm import tqdm |
| 14 | + |
| 15 | +session = requests.Session() |
| 16 | +log = logging.getLogger() |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class PypiProject: |
| 21 | + name: str |
| 22 | + rating: int |
| 23 | + is_ready: bool |
| 24 | + |
| 25 | + @property |
| 26 | + def url(self) -> str: |
| 27 | + return f'https://pypi.org/project/{self.name}' |
| 28 | + |
| 29 | + def __str__(self) -> str: |
| 30 | + return self.name |
| 31 | + |
| 32 | + def get_info(self) -> dict: |
| 33 | + response = session.get(f'https://pypi.org/pypi/{self.name}/json') |
| 34 | + response.raise_for_status() |
| 35 | + return response.json() |
| 36 | + |
| 37 | + @retry( |
| 38 | + wait=wait_fixed(1), |
| 39 | + retry=retry_if_exception_type(RateLimitException), |
| 40 | + ) |
| 41 | + @limits(calls=1, period=4) |
| 42 | + def get_language_ratio(self) -> dict[str, float]: |
| 43 | + |
| 44 | + links = self.get_info()['info']['project_urls'] |
| 45 | + try: |
| 46 | + github_url = one({url for url in links.values() if url.startswith('https://github.com') and url.count('/') == 4}) |
| 47 | + except ValueError: |
| 48 | + log.warning('Cannot detect github url for %s: %s', self.name, links) |
| 49 | + return {} |
| 50 | + |
| 51 | + owner, repo = github_url.removeprefix('https://github.com/').split('/') |
| 52 | + response = session.get(f'https://api.github.com/repos/{owner}/{repo}/languages') |
| 53 | + response.raise_for_status() |
| 54 | + languages = response.json() |
| 55 | + |
| 56 | + total = sum(languages.values()) |
| 57 | + return {language: value/total for language, value in languages.items()} |
| 58 | + |
| 59 | + |
| 60 | +def parse_pyreadiness(version: str = '3.11') -> Iterator[PypiProject]: |
| 61 | + response = session.get(f'https://pyreadiness.org/{version}') |
| 62 | + response.raise_for_status() |
| 63 | + |
| 64 | + counter = count() |
| 65 | + soup = BeautifulSoup(response.text, 'html.parser') |
| 66 | + lists = soup.find_all('div', class_='list') |
| 67 | + for list_ in lists: |
| 68 | + projects = list_.find_all('a') |
| 69 | + for project in projects: |
| 70 | + name, sign = project.text.strip().split(' ') |
| 71 | + yield PypiProject( |
| 72 | + name=name, |
| 73 | + rating=next(counter), |
| 74 | + is_ready=sign == '✓', |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + print(tabulate( |
| 80 | + [ |
| 81 | + ( |
| 82 | + project.rating, |
| 83 | + '+' if project.is_ready else '-', |
| 84 | + project.name, |
| 85 | + project.url, |
| 86 | + ' '.join(f'{language}:{value*100:.2f}%' for language, value in project.get_language_ratio().items()), |
| 87 | + ) for project in tqdm(parse_pyreadiness()) |
| 88 | + ], |
| 89 | + headers=('rating', 'ready', 'name', 'url', 'languages'), |
| 90 | + )) |
0 commit comments