Skip to content

Commit 8fed082

Browse files
Michael HallikMichael Hallik
Michael Hallik
authored and
Michael Hallik
committed
Change update frequency Pepy badge.
1 parent cf904ce commit 8fed082

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

.github/workflows/update_pepy_badge.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Update Pepy Downloads Badge
22

33
on:
44
schedule:
5-
- cron: '0 0 * * 1' # Every Monday at 00:00 UTC
5+
- cron: '0 * * * *' # Every hour
66
workflow_dispatch:
77

88
jobs:

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![License](https://img.shields.io/pypi/l/robotframework-xmlvalidator?cacheSeconds=600)](LICENSE)
33
[![Tests](https://github.com/MichaelHallik/robotframework-xmlvalidator/actions/workflows/test.yml/badge.svg)](https://github.com/MichaelHallik/robotframework-xmlvalidator/actions/workflows/test.yml)
44
[![Total Downloads](https://github.com/MichaelHallik/robotframework-xmlvalidator/raw/main/badge_pepy_downloads.svg)](https://pepy.tech/project/robotframework-xmlvalidator)
5+
[![Run Badge Update](https://github.com/MichaelHallik/robotframework-xmlvalidator/actions/workflows/update_pepy_badge.yml/badge.svg)](https://github.com/MichaelHallik/robotframework-xmlvalidator/actions/workflows/update_pepy_badge.yml)
56

67
<details>
78
<summary><strong>📚 Table of Contents</strong></summary>

fetch_pepy_downloads.py

+54-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
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
116
import os
17+
18+
# Import the requests library to perform HTTP operations
219
import 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
529
PACKAGE_NAME = "robotframework-xmlvalidator"
30+
31+
# Fetch the API key from the environment variable; this must be configured in GitHub Secrets
632
API_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"/>
@@ -22,18 +49,32 @@
2249
<text x="135" y="14">{count}</text>
2350
</g>
2451
</svg>
25-
'''
52+
"""
2653

2754
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+
"""
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

5293
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+
"""
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

Comments
 (0)