11import os
22import requests
33
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"
55PACKAGE_NAME = "robotframework-xmlvalidator"
66API_KEY = os .getenv ("PEPY_API_KEY" )
77
2525'''
2626
2727def fetch_download_count ():
28+ if not API_KEY :
29+ raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
30+
2831 headers = {
29- "Authorization " : f"Token { API_KEY } "
32+ "X-API-Key " : API_KEY
3033 }
3134
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." )
3343 response .raise_for_status ()
3444
3545 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
3851
3952def create_badge_svg (count , output_path = "badge_pepy_downloads.svg" ):
4053 svg_content = SVG_BADGE_TEMPLATE .format (count = count )
@@ -43,7 +56,5 @@ def create_badge_svg(count, output_path="badge_pepy_downloads.svg"):
4356 print (f"Badge written to { output_path } " )
4457
4558if __name__ == "__main__" :
46- if not API_KEY :
47- raise EnvironmentError ("PEPY_API_KEY not set in environment variables." )
4859 count = fetch_download_count ()
4960 create_badge_svg (count )
0 commit comments