diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9e6465386..2f46b59b4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,44 +2,36 @@ name: "Daily Fudan" on: schedule: # scheduled at (UTC+8) everyday - - cron: "14 17 * * *" - - cron: "36 10 * * *" + - cron: "0 11 * * *" + - cron: "0 23 * * *" workflow_dispatch: env: RUN_ENV: 'prod' TZ: 'Asia/Shanghai' - FUCK_GH_PAT: ${{ secrets.GH_PAT }} - + +permissions: + contents: read + 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 + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 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 + python-version: "3.10" + - name: auto edit everyday + run: python3 ./edit.py + - name: daily fudan run: python3 ./run.py '${{ secrets.FUDAN }}' + - name: push + run: | + git config user.name 'Github Actions' + git config user.email 'github-actions@github.com' + git add . + git commit -m 'update by github actions' + git push diff --git a/.gitignore b/.gitignore index b6e47617d..b97131ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ + +.idea/ diff --git a/edit.py b/edit.py new file mode 100644 index 000000000..2cca5e676 --- /dev/null +++ b/edit.py @@ -0,0 +1,5 @@ +import random + +if __name__ == '__main__': + with open("trash", 'w') as f: + f.write(str(random.Random().random())) diff --git a/edit_actions.py b/edit_actions.py deleted file mode 100644 index 0c06df717..000000000 --- a/edit_actions.py +++ /dev/null @@ -1,141 +0,0 @@ -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()) diff --git a/git_base.py b/git_base.py deleted file mode 100644 index e793ab637..000000000 --- a/git_base.py +++ /dev/null @@ -1,124 +0,0 @@ -from datetime import datetime -import subprocess -import traceback -import locale -from sys import argv as sys_argv - -def fuck_windows(std): - lines = std.readlines() - try: - return ''.join(line.decode('utf-8') for line in lines) - except UnicodeDecodeError: - encoding = locale.getpreferredencoding() - return ''.join(line.decode(encoding) for line in lines) - -def fuck_cmd(cmd): - child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) - child.stdin.close() - ret = child.wait() - val = fuck_windows(child.stdout) - if val: - val += '\n' + fuck_windows(child.stderr) - else: - val = fuck_windows(child.stderr) - return ret, val - -subprocess.getstatusoutput = fuck_cmd - -def try_call(call, *arg, **kw): - try: - call(*arg, **kw) - except: - print(traceback.format_exc()) - -def run_pythonfile(f): - ret, val = subprocess.getstatusoutput(f"python3 ./{f}") - print(ret, val) - if ret: - ret, val = subprocess.getstatusoutput(f"python ./{f}") - print(ret, val) - return ret - -def cmd_lines(lines): - for line in lines: - ret, val = subprocess.getstatusoutput(line) - if ret: - print(ret, val) - return ret - -def git_setIdentity(): - lines = [ - 'git config --global user.name "github-actions"', - 'git config --global user.email "github-actions@github.com"' - ] - return cmd_lines(lines) - -def git_rm_upstream(): - lines = [ - "git remote rm upstream" - ] - return cmd_lines(lines) - -def git_add_upstream(url): - try_call(git_rm_upstream) - lines = [ - "git config --global pull.rebase true", - "git config --global merge.ours.driver true", - f"git remote add upstream {url}" - ] - return cmd_lines(lines) - -def git_revoke(): - lines = [ - 'git reset --hard "HEAD^"', - 'git push origin main --force' - ] - return cmd_lines(lines) - -def git_c2upstream(): - lines = [ - 'git fetch upstream --depth=1', - 'git checkout upstream/main' - ] - return cmd_lines(lines) - -def git_push(): - ret, val = subprocess.getstatusoutput("git status") - if ret or "noting to commit" in val: - print(ret, val) - return 1 - lines = [ - "git add .", - 'git commit -m "autocreated by git_base.py"', - "git push origin main --force" - ] - return cmd_lines(lines) - -def run_pythonfile_arg(f, arg): - ret, val = subprocess.getstatusoutput(f'python3 ./{f} "{arg}"') - print(ret, val) - if ret: - ret, val = subprocess.getstatusoutput(f'python ./{f} "{arg}"') - print(ret, val) - return ret - -def get_arg(): - if (len(sys_argv) != 2) or (not sys_argv[1]): - print('no arg') - return '' - return sys_argv[1] - -def pip_install(): - lines = [ - 'python3 -m pip install --upgrade pip', - 'pip3 install -r requirements.txt' - ] - ret = cmd_lines(lines) - if ret: - lines = [ - 'python -m pip install --upgrade pip', - 'pip install -r requirements.txt' - ] - return cmd_lines(lines) - -try_call(git_setIdentity) diff --git a/run.py b/run.py index feabffa78..3020dd51b 100644 --- a/run.py +++ b/run.py @@ -1,8 +1,8 @@ from git_base import * from sys import exit as sys_exit -daily_fudan_core = r'https://github.com/Limour-dev/daily_fudan_core.git' -daily_fudan_actions = r'https://github.com/Limour-dev/daily_fudan_actions.git' +daily_fudan_core = r'https://github.com/Ricardo-Evans/daily_fudan_core.git' +daily_fudan_actions = r'https://github.com/Ricardo-Evans/daily_fudan_actions.git' try_call(git_add_upstream, daily_fudan_core) diff --git a/trash b/trash new file mode 100644 index 000000000..e69de29bb