-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_crawler.py
77 lines (62 loc) · 2.47 KB
/
post_crawler.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
import datetime
import os
import time
from github import Github
from helpers import DataRepo
from utils import is_major_change, term_diff
DATA = 'data'
INTERVAL_CRAWLER_TASK_RESULT = 'interval-crawler-task-result'
access_token = os.environ['GITHUB_TOKEN']
repository = os.environ['GITHUB_REPOSITORY']
g = Github(access_token)
old_repo = DataRepo(DATA)
new_repo = DataRepo(INTERVAL_CRAWLER_TASK_RESULT)
def is_major_update():
if old_repo.current_term_id_list != new_repo.current_term_id_list:
return True
return False
# disabled
for term_id in new_repo.current_term_id_list:
diff = term_diff(old_repo.terms_dict[term_id], new_repo.terms_dict[term_id])
if any(is_major_change(*x) for x in diff):
return True
return False
def git_push(branch_name: str, force: bool):
os.system(f'git add {INTERVAL_CRAWLER_TASK_RESULT}')
os.system('git commit -am "$(TZ=Asia/Shanghai date)"')
os.system(f'git subtree split --prefix {INTERVAL_CRAWLER_TASK_RESULT} --branch {INTERVAL_CRAWLER_TASK_RESULT}')
os.system(f'git push{" --force" if force else ""} origin {INTERVAL_CRAWLER_TASK_RESULT}:{branch_name}')
def create_pull_request():
git_push(INTERVAL_CRAWLER_TASK_RESULT, force=True)
body_text = ''
repo = g.get_repo(repository)
pulls = repo.get_pulls(state='open', base=DATA, head=INTERVAL_CRAWLER_TASK_RESULT)
edit_flag = False
for i, pull in enumerate(pulls):
if i > 0 or datetime.datetime.now() - pull.created_at > datetime.timedelta(days=15):
pull.edit(state='closed')
else:
pull.edit(body=body_text)
edit_flag = True
if not edit_flag:
repo.create_pull(title='chore: auto update', body=body_text, base=DATA, head=INTERVAL_CRAWLER_TASK_RESULT)
def close_pull_request_and_push_directly():
repo = g.get_repo(repository)
pulls = repo.get_pulls(state='open', base=DATA, head=INTERVAL_CRAWLER_TASK_RESULT)
for pull in pulls:
pull.edit(state='closed')
git_push(DATA, force=False)
if __name__ == '__main__':
if len(new_repo.current_term_id_list) == 0:
print('Current term is empty. Failed to push.')
exit(1)
print('Check if this is a major update or not...')
res = is_major_update()
time.sleep(0.5)
if res:
print('Yes. Create a pull request...')
create_pull_request()
else:
print('No. Push to data branch directly...')
close_pull_request_and_push_directly()
print('Finished.')