fix(docs): use pepy for integration package download badges (#32491)

pypi stats has been down for some time.
This commit is contained in:
ccurme
2025-08-10 19:41:36 -03:00
committed by GitHub
parent afcb097ef5
commit 9259eea846
3 changed files with 271 additions and 249 deletions

View File

@@ -1,5 +1,6 @@
from datetime import datetime, timedelta, timezone
from pathlib import Path
import re
import requests
from ruamel.yaml import YAML
@@ -11,10 +12,18 @@ PACKAGE_YML = Path(__file__).parents[2] / "libs" / "packages.yml"
def _get_downloads(p: dict) -> int:
url = f"https://pypistats.org/api/packages/{p['name']}/recent?period=month"
r = requests.get(url)
r.raise_for_status()
return r.json()["data"]["last_month"]
url = f"https://pepy.tech/badge/{p['name']}/month"
svg = requests.get(url, timeout=10).text
texts = re.findall(r"<text[^>]*>([^<]+)</text>", svg)
latest = texts[-1].strip() if texts else "0"
# parse "1.2k", "3.4M", "12,345" -> int
latest = latest.replace(",", "")
if latest.endswith(("k", "K")):
return int(float(latest[:-1]) * 1_000)
if latest.endswith(("m", "M")):
return int(float(latest[:-1]) * 1_000_000)
return int(float(latest) if "." in latest else int(latest))
current_datetime = datetime.now(timezone.utc)

View File

@@ -101,7 +101,12 @@ def package_row(p: dict) -> str:
link = p["provider_page"]
title = p["name_title"]
provider = f"[{title}]({link})" if link else title
return f"| {provider} | [{p['name']}]({p['package_url']}) | ![PyPI - Downloads](https://img.shields.io/pypi/dm/{p['name']}?style=flat-square&label=%20&color=blue) | ![PyPI - Version](https://img.shields.io/pypi/v/{p['name']}?style=flat-square&label=%20&color=orange) | {js} |"
return (
f"| {provider} | [{p['name']}]({p['package_url']}) | "
f"![Downloads](https://static.pepy.tech/badge/{p['name']}/month) | "
f"![PyPI - Version](https://img.shields.io/pypi/v/{p['name']}?style=flat-square&label=%20&color=orange) | "
f"{js} |"
)
def table() -> str: