1+
2+ """
3+ Module: fetch_pepy_downloads.py
4+
5+ This module fetches download statistics for a specific Python package
6+ hosted on pepy.tech using the Pro API (which requires authentication),
7+ and generates a badge (as an SVG file) representing the total number of downloads.
8+
9+ Features:
10+ - Secure API access using a GitHub Actions secret.
11+ - Aggregates download counts from the past year (including CI downloads).
12+ - Outputs a static SVG badge suitable for embedding in README files.
13+ """
14+
15+ # Import the os module to access environment variables
116import os
17+
18+ # Import the requests library to perform HTTP operations
219import requests
320
4- PEPY_API_URL = "https://api.pepy.tech/service-api/v1/pro/projects/{package}/downloads"
21+ # Define the base URL for the pepy.tech Pro API endpoint for project download stats
22+ BASE_URL = "https://api.pepy.tech/service-api/v1/pro/projects/{package}/downloads"
23+
24+ # Construct the full URL for fetching download stats for the given package,
25+ # including query parameters to include CI downloads and cover the last year
26+ PEPY_API_URL = BASE_URL .format (package = "robotframework-xmlvalidator" ) + "?includeCIDownloads=true&timeRange=ONE_YEAR"
27+
28+ # Define the name of the package to query on pepy.tech
529PACKAGE_NAME = "robotframework-xmlvalidator"
30+
31+ # Fetch the API key from the environment variable; this must be configured in GitHub Secrets
632API_KEY = os .getenv ("PEPY_API_KEY" )
733
8- SVG_BADGE_TEMPLATE = '''<svg xmlns="http://www.w3.org/2000/svg" width="200" height="20">
34+ # Define an SVG badge template with placeholders for the download count
35+ SVG_BADGE_TEMPLATE = """<svg xmlns="http://www.w3.org/2000/svg" width="200" height="20">
936 <linearGradient id="a" x2="0" y2="100%%">
1037 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
1138 <stop offset="1" stop-opacity=".1"/>
2249 <text x="135" y="14">{count}</text>
2350 </g>
2451</svg>
25- '''
52+ """
2653
2754def fetch_download_count ():
55+ """
56+ Fetch the total number of downloads for the specified package
57+ from the pepy.tech Pro API, aggregated over the past year
58+ and including CI downloads.
59+
60+ Returns:
61+ str: A formatted string representing the total number of downloads
62+ (e.g., '12,345') for inclusion in an SVG badge.
63+
64+ Raises:
65+ EnvironmentError: If the API key is not found in environment variables.
66+ Exception: If the API response status is 401, 403, or 404.
67+ """
2868 if not API_KEY :
2969 raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
3070
3171 headers = {
3272 "X-API-Key" : API_KEY
3373 }
3474
35- url = PEPY_API_URL . format ( package = PACKAGE_NAME )
75+ url = PEPY_API_URL
3676 response = requests .get (url , headers = headers )
77+
3778 if response .status_code == 404 :
3879 raise Exception (f"Package '{ PACKAGE_NAME } ' not found on pepy.tech Pro API." )
3980 elif response .status_code == 403 :
@@ -47,9 +88,17 @@ def fetch_download_count():
4788 for day , versions in data .get ("downloads" , {}).items ():
4889 total += sum (versions .values ())
4990
50- return f"{ total :,} " # e.g., 12,345
91+ return f"{ total :,} " # e.g., ' 12,345'
5192
5293def create_badge_svg (count , output_path = "badge_pepy_downloads.svg" ):
94+ """
95+ Generate an SVG badge using the total download count
96+ and save it to the specified output path.
97+
98+ Args:
99+ count (str): The formatted string of total downloads.
100+ output_path (str): Path to the output SVG file (default: 'badge_pepy_downloads.svg').
101+ """
53102 svg_content = SVG_BADGE_TEMPLATE .format (count = count )
54103 with open (output_path , "w" ) as f :
55104 f .write (svg_content )
0 commit comments