-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (122 loc) · 4.82 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import argparse
import json
import os
from datetime import datetime
from lib.extractor import GitMetric
from lib.loader import load_config
from lib.logger import logger
def dump_json(data: dict, filename: str):
with open(filename, "w") as outfile:
json.dump(data, outfile, indent=4)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--token", help="GitHub Personal Access Token")
args = parser.parse_args()
script_path = os.path.realpath(".")
config_path = os.path.join(script_path, "config", "config.yml")
output_path = os.path.join(script_path, "data", "stats.json")
config = load_config(config_path)
with open(output_path, "r") as outfile:
try:
stats_data = json.load(outfile)
except json.decoder.JSONDecodeError:
stats_data = {}
for project in config["metrics"]:
owner = project["profile"]
for repo in project["repos"]:
if repo not in stats_data:
stats_data[repo] = {}
if args.token:
token = args.token
metrics = GitMetric(owner, repo, token)
else:
logger.error("Please provide a GitHub Personal Access Token with the `-t` flag")
raise SystemExit
today = datetime.today().strftime("%Y-%m-%d")
views = metrics.get_views()
view_sources = metrics.get_referrers()
clones = metrics.get_clones()
forks = metrics.get_forks()
for clone in clones["clones"]:
cur_time = datetime.strptime(clone["timestamp"].split("T")[0], "%Y-%m-%d").date().strftime("%Y-%m-%d")
if cur_time in stats_data[repo]:
stats_data[repo][cur_time] = {
**stats_data[repo][cur_time],
"clones": {
"unique": clone["uniques"],
"count": clone["count"]
}
}
else:
stats_data[repo][cur_time] = {
"clones": {
"unique": clone["uniques"],
"count": clone["count"]
},
"traffic": {
"count": 0,
"unique": 0
},
"referrer": {}
}
for view in views["views"]:
cur_time = datetime.strptime(view["timestamp"].split("T")[0], "%Y-%m-%d").date().strftime("%Y-%m-%d")
if cur_time in stats_data[repo]:
stats_data[repo][cur_time] = {
**stats_data[repo][cur_time],
"traffic": {
"count": view["count"],
"unique": view["uniques"]
}
}
else:
stats_data[repo][cur_time] = {
"traffic": {
"count": view["count"],
"unique": view["uniques"]
},
"clones": {
"unique": 0,
"count": 0
},
"referrer": {}
}
referrers = {}
for ref in view_sources:
referrers[ref["referrer"]] = {
"count": ref["count"],
"unique": ref["uniques"]
}
if today in stats_data[repo]:
stats_data[repo][today] = {
**stats_data[repo][today],
"referrer": referrers
}
else:
stats_data[repo][today] = {
"referrer": referrers,
"clones": {
"unique": 0,
"count": 0,
},
"traffic": {
"count": 0,
"unique": 0,
},
}
stats_data[repo]["forks"] = len(forks)
total_clones = 0
total_unique_clones = 0
for entry in stats_data[repo]:
if entry != "forks":
clones = stats_data[repo][entry]["clones"]["count"]
uniques = stats_data[repo][entry]["clones"]["unique"]
total_clones += int(clones)
total_unique_clones += int(uniques)
stats_data[repo]["totals"] = {
"clones": {
"count": total_clones,
"unique": total_unique_clones
}
}
dump_json(stats_data, output_path)