-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathget_daily.py
121 lines (105 loc) · 3.87 KB
/
get_daily.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
import argparse
from github import Github
from daily import MY_STATUS_DICT_FROM_API, MY_STATUS_DICT_FROM_COMMENTS
from daily.config import MONTH_SUMMARY_HEAD, MONTH_SUMMARY_STAT_TEMPLATE
from daily.utils import LABEL_DAILY_DICT, replace_readme_comments
MY_NUMBER_STAT_HEAD = (
"| Name | Status | Streak | Today? | \n | ---- | ---- | ---- | ---- |\n"
)
MY_NUMBER_STAT_TEMPLATE = "| {name} | {total} | {streak} | {today} |\n"
# this is a tricky -> [a, b][False] => [a] [a, b][True] => [b]
NO_OR_YES_LIST = ["NO", "YES"]
def make_stat_str(name, total_str, streak, today_check):
# format
return MY_NUMBER_STAT_TEMPLATE.format(
name=name,
total=total_str,
streak=streak,
today=NO_OR_YES_LIST[today_check],
)
def main(
login_dict,
github_token,
repo_name,
):
my_num_stat_str = MY_NUMBER_STAT_HEAD
# API STAT STR
for name, value_dict in MY_STATUS_DICT_FROM_API.items():
try:
url = value_dict.get("url")
md_name = f"[{name}]({url})"
# maybe a better way?
total_data, streak, today_check = value_dict.get("daily_func")(
*login_dict.get(name, tuple())
)
total_data_str = str(total_data) + value_dict.get("unit_str", "")
my_num_stat_str += make_stat_str(
md_name, total_data_str, streak, today_check
)
# just a tricky code for others for use
except Exception as e:
print(e)
continue
u = Github(github_token)
me = u.get_user().login
# COMMENTS STAT STR
for name, value_dict in MY_STATUS_DICT_FROM_COMMENTS.items():
try:
labels, map_func, reduce_func = LABEL_DAILY_DICT.get(name)
except:
# tricky for mine
continue
func = value_dict.get("daily_func")
if not func:
break
issues = u.get_repo(repo_name).get_issues(labels=labels)
total_data, streak, today_check, url, month_summary_dict = func(
me, issues, map_func, reduce_func
)
# change the issue body for month summary
unit = value_dict.get("unit_str", "")
for i in issues:
body = ""
for b in i.body.splitlines():
# from the summary table
if b.startswith("|"):
break
body += b
body = body + "\r\n" + make_month_summary_str(month_summary_dict, unit)
# edit this issue body
i.edit(body=body)
name = f"[{name}]({url})"
total_data_str = str(total_data) + unit
my_num_stat_str += make_stat_str(name, total_data_str, streak, today_check)
replace_readme_comments("README.md", my_num_stat_str, "my_number")
def make_month_summary_str(month_summary_dict, unit):
s = MONTH_SUMMARY_HEAD
for m, n in month_summary_dict.items():
s += MONTH_SUMMARY_STAT_TEMPLATE.format(
month=str(m) + "月", number=str(int(n)) + f" {unit}"
)
return s
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("github_token", help="github_token")
parser.add_argument("repo_name", help="repo_name")
parser.add_argument("cichang_user_name", help="cichang_user_name")
parser.add_argument("cichang_password", help="cichang_password")
parser.add_argument("forst_email", help="forst_email")
parser.add_argument("forst_password", help="forst_password")
options = parser.parse_args()
# add more login auth info here
login_auth_dict = {
"词场": (options.cichang_user_name, options.cichang_password),
"番茄": (
options.forst_email,
options.forst_password,
options.github_token,
options.repo_name,
),
}
main(
login_auth_dict,
options.github_token,
options.repo_name,
)