Skip to content

Instantly share code, notes, and snippets.

@qlrd
Last active May 12, 2025 02:38
Show Gist options
  • Save qlrd/f65afbccf15c68d9c1853028b5e5d9ad to your computer and use it in GitHub Desktop.
Save qlrd/f65afbccf15c68d9c1853028b5e5d9ad to your computer and use it in GitHub Desktop.
Measure some download stats for selfcustody projects
import json
import requests
import argparse
HEADERS = {
"Accept": 'application/vnd.github+json',
"X-GitHub-Api-Version": "2022-11-28",
}
parser = argparse.ArgumentParser(
prog='measure-selfcustody-downloads',
description='measure the release\'s download in selfcustody')
parser.add_argument('-p', '--project', default="krux", help="the project to be summarized (default: krux)")
parser.add_argument('-u', '--user', default="selfcustody", help="the user to search for the project (default: selfcustody)")
parser.add_argument('-f', '--format', default="json", help="the format to be printed [json, csv] (default: json)")
args = parser.parse_args()
def make_url(user: str, project: str) -> str:
"""Generate Github API url for selected user / project"""
return f"https://api.github.com/repos/{user}/{project}/releases"
COUNTS = {}
ignore_extensions = (".txt", ".sig", ".py", ".gpg", ".sha256")
URL = make_url(args.user, args.project)
req = requests.get(URL, headers=HEADERS)
releases = req.json()
for release in releases:
for asset in release["assets"]:
release_name = asset["name"]
if not asset["name"].endswith(ignore_extensions):
COUNTS[release_name] = asset["download_count"]
if args.format == "json":
data = json.dumps(COUNTS, indent=4, sort_keys=True)
print(data)
if args.format == "csv":
print("NAME,DOWNLOAD_COUNT")
for key, val in COUNTS.items():
print(f"{key},{val}")
@tadeubas
Copy link

tadeubas commented Apr 29, 2024

Regex are complicated and doesn't always give the result we were expecting, I was having problems when using on seedsigner repo, here is what worked:

good test cases:

  • -u seedsigner -p seedsigner
  • -u cryptoadvance -p specter-diy
import requests
import argparse
import json
from datetime import datetime

HEADERS = {
    "Accept": 'application/vnd.github+json',
    "X-GitHub-Api-Version": "2022-11-28",
}

parser = argparse.ArgumentParser(
                    prog='measure-selfcustody-downloads',
                    description='measure the release\'s download in selfcustody')

parser.add_argument('-p', '--project', default="krux", help="the project to be summarized (default: krux)")
parser.add_argument('-u', '--user', default="selfcustody", help="the user to search for the project (default: selfcustody)")
args = parser.parse_args()

def make_url(user: str, project: str) -> str:
    """Generate Github API url for selected user / project"""
    return f"https://api.github.com/repos/{user}/{project}/releases"


ignore_extensions = (".txt", ".sig", ".py", ".gpg", ".sha256")
URL = make_url(args.user, args.project)
req = requests.get(URL, headers=HEADERS)
releases = req.json()

# Save the releases JSON
# with open("releases.json", "w") as f:
#     f.write(json.dumps(releases, indent=4))

print("===========================================================================")
print(f"DOWNLOAD SUMMARY of {URL}")
count_set = {}
total = 0
for release in releases:
    date_obj = datetime.strptime(release["published_at"], "%Y-%m-%dT%H:%M:%SZ")
    formatted_date = date_obj.strftime("%m/%Y")
    if formatted_date not in count_set:
        count_set[formatted_date] = 0
    for asset in release["assets"]:
        release_name = asset["name"]
        if not asset["name"].endswith(ignore_extensions):
            count_set[release_name] = asset["download_count"]
            count_set[formatted_date] += asset["download_count"]
            total += asset["download_count"]

for key, val in count_set.items():
    print(f"{key}: {val}")

print("TOTAL: %d" % total)        
print("===========================================================================")

@qlrd
Copy link
Author

qlrd commented Apr 29, 2024

updated with some adds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment