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
|
#!/usr/bin/env python3
r"""Add or edit github users in the backers file
"""
import sys
import logging
import requests
from pathlib import Path
from ruamel.yaml import YAML # pip install ruamel.yaml
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
def fetch_user(username):
logger.info("fetching %s", username)
resp = requests.get(
f"https://api.github.com/users/{username}",
headers={"Accept": "application/vnd.github.v3+json"},
)
resp.raise_for_status()
return resp.json()
def get_user_data(data):
"""
Get the data to save from the request data
"""
out = {
"username": data["login"],
"avatar": data["avatar_url"],
"name": data["name"],
}
if data["blog"]:
website = data["blog"]
if not website.startswith("http"):
website = "http://" + website
out["website"] = website
return out
def add_entry(opt, filedata, username):
userdata = get_user_data(fetch_user(username))
if opt.top:
userdata["tier"] = "top"
filedata.append(userdata)
def update_entry(opt, filedata, entry):
# entry is an username or an user entry daat
if isinstance(entry, str):
username = entry
entry = [e for e in filedata if e["username"] == username]
if not entry:
raise Exception(f"{username} not found")
entry = entry[0]
else:
username = entry["username"]
userdata = get_user_data(fetch_user(username))
for k, v in userdata.items():
if entry.get("keep_" + k):
continue
entry[k] = v
def main():
opt = parse_cmdline()
logger.info("reading %s", opt.file)
yaml = YAML(typ="rt")
filedata = yaml.load(opt.file)
for username in opt.add or ():
add_entry(opt, filedata, username)
for username in opt.update or ():
update_entry(opt, filedata, username)
if opt.update_all:
for entry in filedata:
update_entry(opt, filedata, entry)
# yamllint happy
yaml.explicit_start = True
logger.info("writing %s", opt.file)
yaml.dump(filedata, opt.file)
def parse_cmdline():
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"--file",
help="the file to update [default: %(default)s]",
default=Path(__file__).parent.parent / "BACKERS.yaml",
type=Path,
)
parser.add_argument(
"--add",
metavar="USERNAME",
nargs="+",
help="add USERNAME to the backers",
)
parser.add_argument(
"--top",
action="store_true",
help="add to the top tier",
)
parser.add_argument(
"--update",
metavar="USERNAME",
nargs="+",
help="update USERNAME data",
)
parser.add_argument(
"--update-all",
action="store_true",
help="update all the existing backers data",
)
opt = parser.parse_args()
return opt
if __name__ == "__main__":
sys.exit(main())
|