forked from Limour-dev/daily_fudan_actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_actions.py
141 lines (120 loc) · 3.23 KB
/
edit_actions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from git_base import *
from random import randint
from datetime import datetime
from sys import exit as sys_exit
import traceback
schedule_template = '''name: "Daily Fudan"
on:
schedule: # scheduled at (UTC+8) everyday
- cron: "%s"
- cron: "%s"
workflow_dispatch:
env:
RUN_ENV: 'prod'
TZ: 'Asia/Shanghai'
FUCK_GH_PAT: ${{ secrets.GH_PAT }}
jobs:
build:
runs-on: ubuntu-latest
# if: github.ref == 'refs/heads/master'
steps:
- name: Checkout master with GH_PAT
if: ${{ env.FUCK_GH_PAT }}
uses: actions/checkout@v2
with:
fetch-depth: 2
ref: main
token: ${{ secrets.GH_PAT }}
- name: Checkout master without GH_PAT
if: ${{ !env.FUCK_GH_PAT }}
uses: actions/checkout@v2
with:
fetch-depth: 0
ref: main
- name: Set up python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: edit_actions.py
if: ${{ env.FUCK_GH_PAT }}
run: python3 ./edit_actions.py '${{ secrets.SCHEDULE }}'
- name: run.py
run: python3 ./run.py '${{ secrets.FUDAN }}'
'''
am_inf=19
am_sup=22
pm_inf = 5
pm_sup = 8
t_inf = 4
t_sup = 15
def getRandCron_am():
mins = randint(0,59)
hours = randint(am_inf,am_sup) % 24
return f'{mins} {hours} * * *'
def getRandCron_pm():
mins = randint(0,59)
hours = randint(pm_inf,pm_sup) % 24
return f'{mins} {hours} * * *'
def is_pm():
uctnow = datetime.utcnow()
uctnow = uctnow.hour
if uctnow < t_inf:
uctnow += 24
return t_inf < uctnow < t_sup
def get_schedule():
return schedule_template%(getRandCron_am(),getRandCron_pm())
GMT_FORMAT = 'Date: %a %b %d %H:%M:%S %Y +0800'
def is_today_created(val):
date = val.split('\n')[2]
date = datetime.strptime(date, GMT_FORMAT)
date = (datetime.now()-date).total_seconds()//3600
print('timedelta', date)
return date <= 12
def is_autocreated():
ret, val = subprocess.getstatusoutput("git log -1")
if ret:
print(ret, val)
return False
if 'autocreated by git_base.py' in val:
if is_today_created(val):
sys_exit(0)
return True
return False
def update_schedule():
with open(r'./.github/workflows/main.yml','w',encoding='utf-8') as f:
f.write(get_schedule())
def get_my_arg():
print('Please get token from https://github.com/settings/tokens')
arg = get_arg()
if not arg:
print('use default SCHEDULE')
return
SCHEDULE = arg.strip().split(' ')
global am_inf, am_sup, pm_inf, pm_sup, t_inf, t_sup
if len(SCHEDULE) == 6:
am_inf = int(SCHEDULE[0])
am_sup = int(SCHEDULE[1])
pm_inf = int(SCHEDULE[2])
pm_sup = int(SCHEDULE[3])
t_inf = int(SCHEDULE[4])
t_sup = int(SCHEDULE[5])
else:
print('wrong SCHEDULE. use default SCHEDULE')
print(am_inf, am_sup, pm_inf, pm_sup, t_inf, t_sup)
def main():
if is_autocreated():
if git_revoke():
return
update_schedule()
git_push()
try:
get_my_arg()
except:
print(traceback.format_exc())
if is_pm():
try:
main()
except SystemExit:
pass
except:
print(traceback.format_exc())