-
Notifications
You must be signed in to change notification settings - Fork 3
/
collect_stats.py
102 lines (85 loc) · 2.76 KB
/
collect_stats.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import re
from pprint import pprint
import sys
from datetime import datetime, timezone, date
import requests
def downloads(verbose=False):
r = requests.get(
"https://api.github.com/repos/ActivityWatch/activitywatch/releases?per_page=100"
)
d = r.json()
downloads = 0
for release in d:
if verbose:
print("Release: ", release["tag_name"])
for asset in release["assets"]:
platform = re.findall("(macos|darwin|linux|windows)", asset["name"])[0]
count = asset["download_count"]
if verbose:
print(" - {}: {}".format(platform, count))
downloads += asset["download_count"]
return downloads
def stars():
r = requests.get("https://api.github.com/repos/ActivityWatch/activitywatch")
d = r.json()
return d["stargazers_count"]
def clones():
# TODO: Needs push access to the repository
r = requests.get(
"https://api.github.com/repos/ActivityWatch/activitywatch/traffic/clones?per=day"
)
d = r.json()
pprint(d)
def twitter():
# TODO: Needs API key
r = requests.get(
"https://api.twitter.com/1.1/users/show.json?screen_name=ActivityWatchIt"
)
d = r.json()
pprint(d)
followers = d["followers_count"]
print("Followers: ", followers)
def releases():
"""
Fetch all releases and their dates from the GitHub API.
NOTE: Needs to use the commit date, not the release date, for some releases that had to be re-released (like v0.10.0).
"""
r = requests.get(
"https://api.github.com/repos/ActivityWatch/activitywatch/releases?per_page=100"
)
r.raise_for_status()
d = r.json()
releases: dict[str, date] = {}
for release in d:
# We need to fetch the commit of the tag and use its date
r = requests.get(
"https://api.github.com/repos/ActivityWatch/activitywatch/commits/"
+ release["tag_name"]
)
r.raise_for_status()
commit = r.json()
releases[release["tag_name"]] = (
datetime.strptime(
commit["commit"]["committer"]["date"],
"%Y-%m-%dT%H:%M:%SZ",
)
.replace(tzinfo=timezone.utc)
.date()
)
return releases
if __name__ == "__main__":
if "--releases" in sys.argv:
if "--csv" in sys.argv:
print("date,tag")
for tag, d in sorted(releases().items(), key=lambda x: x[1]):
print(f"{d},{tag}")
else:
pprint(releases())
else:
s = stars()
d = downloads()
if "--csv" in sys.argv:
print(f"{datetime.now(tz=timezone.utc).isoformat()},{d},{s}")
else:
print("Downloads: ", d)
print("Stars: ", s)