Last active
July 3, 2023 09:25
-
-
Save shravanasati/1e2d07ba3ba4e9844ce6769f5899719d to your computer and use it in GitHub Desktop.
A python script to fetch the github release download count for a repo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Demo: | |
Github username: Shravan-1908 | |
Repo name: hydra | |
Release ID: (latest) | |
Download stats for `Shravan-1908/hydra`, release `v2.2.0` | |
--------------------------------------------------------- | |
hydra-darwin-amd64: 2 | |
hydra-linux-amd64: 47 | |
hydra-windows-amd64.exe: 19 | |
Total: 68 | |
""" | |
import requests | |
color_blue = "\033[36m" | |
color_yellow = "\033[33m" | |
color_green = "\033[32m" | |
color_red = "\033[31m" | |
color_purple = "\033[35m" | |
color_end = "\033[0m" | |
def get_release_download_count(username, repo, release_id="latest"): | |
""" | |
Gives the download count for release_id assets on github. | |
""" | |
url = "" | |
if release_id != "latest": | |
url = "https://api.github.com/repos/{}/{}/releases/tags/{}".format( | |
username, repo, release_id) | |
else: | |
url = "https://api.github.com/repos/{}/{}/releases/latest".format( | |
username, repo) | |
r = requests.get(url) | |
if r.status_code == 200: | |
data = r.json() | |
tagname = data["tag_name"] | |
message = ( | |
f"{color_blue}Download stats for `{username}/{repo}`, release `{tagname}`") | |
print(f"{message}\n{'-'*(len(message)-5)}{color_end}") | |
total = 0 | |
for asset in data["assets"]: | |
asset_name = asset["name"] | |
asset_download_count = asset["download_count"] | |
print(f"{color_yellow}{asset_name}: {color_green}{asset_download_count}") | |
total += asset["download_count"] | |
print(f"\n{color_purple}Total: {total}{color_end}") | |
else: | |
print(color_red + "Unable to find release download count." + color_end) | |
if __name__ == "__main__": | |
username = input("Github username: ") | |
repo = input("Repo name: ") | |
release_id = input("Release ID: (latest) ") | |
if release_id.strip() == "": | |
release_id = "latest" | |
print() | |
get_release_download_count(username, repo, release_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment