Skip to content

Commit 5da988c

Browse files
Michael HallikMichael Hallik
Michael Hallik
authored and
Michael Hallik
committed
Fix incorrect pepy end point.
1 parent 61417d5 commit 5da988c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

fetch_pepy_downloads.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import 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"
55
PACKAGE_NAME = "robotframework-xmlvalidator"
66
API_KEY = os.getenv("PEPY_API_KEY")
77

@@ -25,16 +25,29 @@
2525
'''
2626

2727
def 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

3952
def 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

4558
if __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

Comments
 (0)