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
1
16
import os
17
+
18
+ # Import the requests library to perform HTTP operations
2
19
import requests
3
20
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
5
29
PACKAGE_NAME = "robotframework-xmlvalidator"
30
+
31
+ # Fetch the API key from the environment variable; this must be configured in GitHub Secrets
6
32
API_KEY = os .getenv ("PEPY_API_KEY" )
7
33
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">
9
36
<linearGradient id="a" x2="0" y2="100%%">
10
37
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
11
38
<stop offset="1" stop-opacity=".1"/>
22
49
<text x="135" y="14">{count}</text>
23
50
</g>
24
51
</svg>
25
- '''
52
+ """
26
53
27
54
def 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
+ """
28
68
if not API_KEY :
29
69
raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
30
70
31
71
headers = {
32
72
"X-API-Key" : API_KEY
33
73
}
34
74
35
- url = PEPY_API_URL . format ( package = PACKAGE_NAME )
75
+ url = PEPY_API_URL
36
76
response = requests .get (url , headers = headers )
77
+
37
78
if response .status_code == 404 :
38
79
raise Exception (f"Package '{ PACKAGE_NAME } ' not found on pepy.tech Pro API." )
39
80
elif response .status_code == 403 :
@@ -47,9 +88,17 @@ def fetch_download_count():
47
88
for day , versions in data .get ("downloads" , {}).items ():
48
89
total += sum (versions .values ())
49
90
50
- return f"{ total :,} " # e.g., 12,345
91
+ return f"{ total :,} " # e.g., ' 12,345'
51
92
52
93
def 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
+ """
53
102
svg_content = SVG_BADGE_TEMPLATE .format (count = count )
54
103
with open (output_path , "w" ) as f :
55
104
f .write (svg_content )
0 commit comments