1
1
import os
2
2
import requests
3
3
4
- PEPY_API_URL = "https://pepy.tech/api/v2 /projects/{package}/downloads"
4
+ PEPY_API_URL = "https://api. pepy.tech/service- api/v1/pro /projects/{package}/downloads"
5
5
PACKAGE_NAME = "robotframework-xmlvalidator"
6
6
API_KEY = os .getenv ("PEPY_API_KEY" )
7
7
25
25
'''
26
26
27
27
def fetch_download_count ():
28
+ if not API_KEY :
29
+ raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
30
+
28
31
headers = {
29
- "Authorization " : f"Token { API_KEY } "
32
+ "X-API-Key " : API_KEY
30
33
}
31
34
32
- response = requests .get (PEPY_API_URL .format (package = PACKAGE_NAME ), headers = headers )
35
+ url = PEPY_API_URL .format (package = PACKAGE_NAME )
36
+ response = requests .get (url , headers = headers )
37
+ if response .status_code == 404 :
38
+ raise Exception (f"Package '{ PACKAGE_NAME } ' not found on pepy.tech Pro API." )
39
+ elif response .status_code == 403 :
40
+ raise Exception ("Access denied: Are you sure your account has Pro access?" )
41
+ elif response .status_code == 401 :
42
+ raise Exception ("Unauthorized: Check that your API key is valid." )
33
43
response .raise_for_status ()
34
44
35
45
data = response .json ()
36
- total_downloads = data .get ("total_downloads" , 0 )
37
- return f"{ total_downloads :,} " # format with commas
46
+ total = 0
47
+ for day , versions in data .get ("downloads" , {}).items ():
48
+ total += sum (versions .values ())
49
+
50
+ return f"{ total :,} " # e.g., 12,345
38
51
39
52
def create_badge_svg (count , output_path = "badge_pepy_downloads.svg" ):
40
53
svg_content = SVG_BADGE_TEMPLATE .format (count = count )
@@ -43,7 +56,5 @@ def create_badge_svg(count, output_path="badge_pepy_downloads.svg"):
43
56
print (f"Badge written to { output_path } " )
44
57
45
58
if __name__ == "__main__" :
46
- if not API_KEY :
47
- raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
48
59
count = fetch_download_count ()
49
60
create_badge_svg (count )
0 commit comments