-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_up.py
75 lines (62 loc) · 2.17 KB
/
get_up.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
import argparse
import requests
import pendulum
from main import login
GET_UP_ISSUE_NUMBER = 3
GET_UP_MESSAGE_TEMPLATE = (
"今天的起床时间是--{get_up_time}.\r\n\r\n 起床啦,喝杯咖啡,背个单词,去跑步。\r\n\r\n 今天的一句诗:\r\n {sentence}"
)
SENTENCE_API = "https://v1.jinrishici.com/all"
DEFAULT_SENTENCE = "赏花归去马如飞\r\n去马如飞酒力微\r\n酒力微醒时已暮\r\n醒时已暮赏花归\r\n"
TIMEZONE = "Asia/Shanghai"
def get_one_sentence():
try:
r = requests.get(SENTENCE_API)
if r.ok:
return r.json().get("content", DEFAULT_SENTENCE)
return DEFAULT_SENTENCE
except:
print("get SENTENCE_API wrong")
return DEFAULT_SENTENCE
def get_today_get_up_status(issue):
comments = list(issue.get_comments())
if not comments:
return False
latest_comment = comments[-1]
now = pendulum.now(TIMEZONE)
latest_day = pendulum.instance(latest_comment.created_at).in_timezone(
"Asia/Shanghai"
)
is_today = (latest_day.day == now.day) and (latest_day.month == now.month)
return is_today
def make_get_up_message():
sentence = get_one_sentence()
now = pendulum.now(TIMEZONE)
is_get_up_early = 4 <= now.hour <= 8
get_up_time = now.to_datetime_string()
body = GET_UP_MESSAGE_TEMPLATE.format(get_up_time=get_up_time, sentence=sentence)
return body, is_get_up_early
def get_get_up_issue(repo):
return repo.get_issue(GET_UP_ISSUE_NUMBER)
def main(github_token, repo_name):
u = login(github_token)
repo = u.get_repo(repo_name)
issue = get_get_up_issue(repo)
is_toady = get_today_get_up_status(issue)
if is_toady:
print("Today I have recorded the wake up time")
return
body, is_get_up_early = make_get_up_message()
if is_get_up_early:
issue.create_comment(body)
else:
print("You wake up late")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("github_token", help="github_token")
parser.add_argument("repo_name", help="repo_name")
options = parser.parse_args()
main(
options.github_token,
options.repo_name,
)