From a14e4afda78482da43493aa8c199b1414ee9a7af Mon Sep 17 00:00:00 2001 From: Muhan Li Date: Sun, 27 Mar 2022 04:16:16 +0800 Subject: [PATCH] update workflows --- .github/trigger.json | 3 ++ .github/workflows/ci.yml | 3 +- .github/workflows/data.yml | 68 ++++++++++++++++++++++++++++++++++++++ .github/workflows/ics.yml | 34 ------------------- README.md | 2 +- crawler.py | 13 +++++--- 6 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 .github/trigger.json create mode 100644 .github/workflows/data.yml delete mode 100644 .github/workflows/ics.yml diff --git a/.github/trigger.json b/.github/trigger.json new file mode 100644 index 0000000..0985cd5 --- /dev/null +++ b/.github/trigger.json @@ -0,0 +1,3 @@ +{ + "trigger": 0 +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index acb96c1..23b9698 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,8 +6,7 @@ on: - "**" pull_request: branches: - - main - - dev + - "**" jobs: build: diff --git a/.github/workflows/data.yml b/.github/workflows/data.yml new file mode 100644 index 0000000..104cd51 --- /dev/null +++ b/.github/workflows/data.yml @@ -0,0 +1,68 @@ +name: Update Calendar Data + +on: + workflow_dispatch: + schedule: + - cron: "0 * * * *" + push: + branches: + - "**" + +jobs: + main: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + fetch-depth: 2 + + - name: Set up Python environment + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - name: Run crawler + shell: bash + run: python crawler.py + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 + + - name: Go Build + run: go build -o . main + + - name: Generate ICS + run: ./main + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + file_pattern: docs/*.html docs/*.ics data/*.txt + commit_message: update calendar data automatically + commit_user_name: bugstop + commit_user_email: github@umm.one + + - name: Get last commit message + id: last-commit-message + run: | + echo "::set-output name=msg::$(git log -1 --pretty=%s)" + + - name: Update README + uses: stefanzweifel/git-auto-commit-action@v4 + with: + file_pattern: README.md + commit_message: ${{ steps.last-commit-message.outputs.msg }} + commit_user_name: bugstop + commit_user_email: github@umm.one + commit_options: '--amend --no-edit' + push_options: '--force' + skip_fetch: true diff --git a/.github/workflows/ics.yml b/.github/workflows/ics.yml deleted file mode 100644 index f6b0124..0000000 --- a/.github/workflows/ics.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: ICS Generator - -on: - pull_request: - push: - branches: - - "**" - -jobs: - commit: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - with: - ref: ${{ github.head_ref }} - - - name: Setup Go - uses: actions/setup-go@v2 - with: - go-version: 1.17 - - - name: Go Build - run: go build -o . main - - - name: Generate ICS - run: ./main - - - uses: stefanzweifel/git-auto-commit-action@v4 - with: - file_pattern: docs/*.html docs/*.ics - commit_message: generate .ics file automatically - commit_user_name: bugstep - commit_user_email: github@isaacx.com diff --git a/README.md b/README.md index 90dfd84..9977803 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ subscription link of public holidays in mainland China -> data updated at: March 27, 2022 +> Calendar data updated at: March 27, 2022. ## Demo diff --git a/crawler.py b/crawler.py index 2b0708d..a495b7a 100644 --- a/crawler.py +++ b/crawler.py @@ -1,6 +1,6 @@ import os import re -from datetime import datetime +from datetime import datetime, timezone, timedelta from typing import Iterator, Tuple import requests @@ -24,16 +24,16 @@ def main(): with open(file, 'w') as f: f.write( - f"{comments[0]} ({datetime.now().strftime('%-m/%-d/%Y')})\n" + f"{comments[0]} ({beijing_time().strftime('%-m/%-d/%Y')})\n" f"{comments[1]}\n\n// source: {link}\n\n{holidays}" ) - update_info = "> data updated at: " + update_info = "> Calendar data updated at: " with open('./README.md', 'r') as f: content = f.read().split('\n') for i in range(len(content)): if content[i].startswith(update_info): - content[i] = update_info + datetime.now().strftime("%B %-d, %Y") + content[i] = update_info + beijing_time().strftime("%B %-d, %Y") with open('./README.md', 'w') as f: f.write('\n'.join(content)) @@ -102,5 +102,10 @@ def source() -> Iterator[Tuple[str, str]]: yield match.group('year'), match.group('link') +def beijing_time() -> datetime: + utc_time = datetime.utcnow().replace(tzinfo=timezone.utc) + return utc_time.astimezone(timezone(timedelta(hours=8))) + + if __name__ == '__main__': main()