Created
January 13, 2025 21:15
-
-
Save WilliamBergamin/15a0605627731e9b7960595c5b266f59 to your computer and use it in GitHub Desktop.
Python package download statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
from typing import Dict, TypedDict | |
relevant_python_versions = ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] | |
allowed_pkg_names = ["slack_sdk", "slack_bolt", "slack_cli_hooks"] | |
pkg_name = allowed_pkg_names[0] | |
if len(sys.argv) >= 2: | |
if sys.argv[1] not in allowed_pkg_names: | |
sys.stderr.write(f"Allowed packaged: {allowed_pkg_names}") | |
sys.exit() | |
pkg_name = sys.argv[1] | |
print( | |
"Display the last 180 days package download statistics\n" | |
"Source: https://pypistats.org/\n" | |
) | |
class PypiStatsItem(TypedDict): | |
category: str | |
date: str | |
downloads: int | |
class PypiStatsObject(TypedDict): | |
data: list[PypiStatsItem] | |
package: str | |
type: str | |
def get_downloads(pkg_name: str, version: str) -> int: | |
response = requests.get( | |
f"https://pypistats.org/api/packages/{pkg_name}/python_minor?version={version}" | |
) | |
json: PypiStatsObject = response.json() | |
return sum([item["downloads"] for item in json["data"]]) | |
version_downloads: Dict[str, int] = {} | |
total_downloads = 0 | |
for version in relevant_python_versions: | |
downloads = get_downloads(pkg_name=pkg_name, version=version) | |
total_downloads += downloads | |
version_downloads[version] = downloads | |
columns = ["Version", "Downloads", "Percentage"] | |
row_formatter = "{:<13}" * (len(columns)) | |
print(row_formatter.format(*columns)) | |
for version, downloads in version_downloads.items(): | |
percentage = round(downloads / total_downloads * 100, 2) | |
print(row_formatter.format(version, downloads, f"{percentage}%")) | |
print(row_formatter.format("Total", total_downloads, "n/a"), end="\n\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests==2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update