File: gh-release-stats.py

package info (click to toggle)
manuskript 0.16.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,228 kB
  • sloc: javascript: 54,334; python: 25,835; sh: 253; xml: 174; makefile: 86
file content (84 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# --!-- coding: utf8 --!--

import argparse
import json

url = "https://api.github.com/repos/{user}/{repo}/releases"

parser = argparse.ArgumentParser(description='Get download count for github releases.')
parser.add_argument('user', type=str, help='The github user.')
parser.add_argument('repo', type=str, help='The repo of given user.')
parser.add_argument("-d", "--details", action="store_true")

args = parser.parse_args()

url = url.format(user=args.user,
                 repo=args.repo)

def getJSON(URL):
    import urllib.request
    with urllib.request.urlopen(URL) as url:
        data = json.loads(url.read().decode())
        return data

def humanReadable(n):
    s = ["", "K", "M", "B", "T"]
    f = 1000.
    hrs = n
    i = 0
    while hrs > 500:
        hrs = hrs / f
        i += 1
    hrs = round(hrs, 1)
    return "{}{}".format(hrs, s[i])


def humanReadableSize(size):
    s = ["B", "KB", "MB", "GB", "TB"]
    f = 1024.
    hrs = size
    i = 0
    while hrs > 500:
        hrs = hrs / f
        i += 1
    hrs = round(hrs, 1)
    return "{} {}".format(hrs, s[i])

releases = getJSON(url)

total = 0

for r in releases:
    name = r["name"]
    tag = r["tag_name"]
    author = r["author"]["login"]
    time = r["created_at"]
    
    name = "{} ({})".format(name, tag) if name else tag
     
    tot = 0
    details = []
    for a in r["assets"]:
        nameA = a["name"]
        size = a["size"]
        download_count = a["download_count"]
        tot += download_count
        
        details.append("  *  {} ({}): {} hits".format(
            nameA, humanReadableSize(size), humanReadable(download_count))
        )
    
    txt = "{}: {} hits".format(name, humanReadable(tot))
    print(txt)
    if args.details:
        print("-" * len(txt))
        [print(d) for d in details]
        print("")
        
    total += tot

txt = "Total downloads: {} hits".format(humanReadable(total))
print("=" * len(txt))
print(txt)
print("=" * len(txt))