-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstar_watcher.py
58 lines (45 loc) · 1.49 KB
/
star_watcher.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
#!/usr/bin/python3
import requests
import os
import sys
filename = "./.star_count"
push_url = ""
def star_counter(username: str, token: "") -> int:
all_repos_url = "https://api.github.com/users/{}/repos?per_page=100".format(username)
header = {} if token == "" else {"Authorization": "bearer {}".format(token)}
res = requests.get(all_repos_url, header)
repos = res.json()
count = 0
for repo in repos:
count += repo["stargazers_count"]
return count
def load_count() -> int:
try:
with open(filename, 'r') as f:
count = int(f.read())
except IOError:
count = 0
return count
def save_count(count: int):
with open(filename, 'w') as f:
f.write(str(count))
def send_message(msg: str):
requests.get("{}{}".format(push_url, msg))
def main():
if len(sys.argv) != 4:
print("Error! This script requires three arguments: GITHUB_USERNAME GITHUB_TOKEN PUSH_URL")
return
username = sys.argv[1]
token = sys.argv[2]
global push_url
push_url = sys.argv[3]
last_count = load_count()
current_count = star_counter(username, token)
if current_count != last_count:
if current_count > last_count:
send_message("Star 数量增加了,哈哈!{} --> {}!".format(last_count, current_count))
else:
send_message("Star 数量减少了,淦!{} --> {}!".format(last_count, current_count))
save_count(current_count)
if __name__ == '__main__':
main()