Fetch #541
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Fetch | |
on: | |
schedule: | |
- cron: '11 11 * * *' | |
workflow_dispatch: | |
inputs: | |
overwrite: | |
description: 'Overwrite the old data' | |
type: boolean | |
default: false | |
required: true | |
bangumi: | |
description: 'Fetch Bangumi data' | |
type: boolean | |
default: true | |
required: true | |
tmdb: | |
description: 'Fetch TMDB data' | |
type: boolean | |
default: true | |
required: true | |
jobs: | |
prepare: | |
name: Prepare branch | |
runs-on: ubuntu-latest | |
outputs: | |
branch: ${{ steps.branch.outputs.branch }} | |
overwrite: ${{ inputs.overwrite || 'false' }} | |
permissions: | |
contents: write | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
- name: Prepare branch | |
id: branch | |
run: | | |
BRANCH=fetch-data/$(date +"%Y-%m-%d") | |
git checkout -B $BRANCH | |
git push -u -f origin $BRANCH | |
echo "branch=$BRANCH" >> $GITHUB_OUTPUT | |
bangumi: | |
name: Fetch Bangumi | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
needs: | |
- prepare | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
- name: Setup pnpm | |
uses: pnpm/[email protected] | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20.x | |
cache: pnpm | |
- name: Install and build | |
run: | | |
pnpm install | |
pnpm build | |
- name: Fetch Bangumi | |
if: ${{ inputs.bangumi || github.event_name == 'schedule' }} | |
run: | | |
pnpm run bgmx fetch bangumi --overwrite=${{ needs.prepare.outputs.overwrite }} | tee run.log | |
grep "^Info:" run.log > bangumi-info.log || true | |
grep "^Error:" run.log > bangumi-error.log || true | |
# Generate markdown summary | |
output=data/bangumi.md | |
echo '# Bangumi' > $output | |
# Output Info | |
info_diff=$(diff --new-line-format="%L" --old-line-format="" --unchanged-line-format="" "data/bangumi-info.log" "bangumi-info.log" || true) | |
if [ -n "$info_diff" ]; then | |
echo '## Fetch new bangumi' >> $output | |
echo "$info_diff" | while IFS= read -r line; do | |
line="${line/Info: /}" | |
echo "- ${line}" >> $output | |
done | |
fi | |
# Output Error | |
error_diff=$(diff --new-line-format="%L" --old-line-format="" --unchanged-line-format="" "data/bangumi-error.log" "bangumi-error.log" || true) | |
if [ -n "$error_diff" ]; then | |
echo '## Error' >> $output | |
echo "$error_diff" | while IFS= read -r line; do | |
line="${line/Error: /}" | |
echo "- ${line}" >> $output | |
done | |
fi | |
# Update | |
grep "^Info:" run.log > data/bangumi-info.log || true | |
grep "^Error:" run.log > data/bangumi-error.log || true | |
- name: Push changes | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d %T") | |
BRANCH=${{ needs.prepare.outputs.branch }} | |
if [[ -n $(git status -s) ]]; then | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
git add data | |
git commit -m "feat(bgmd): update bangumi data at $TIMESTAMP" | |
git push origin $BRANCH | |
fi | |
yuc: | |
name: Fetch yuc.wiki | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
needs: | |
- prepare | |
- bangumi | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
- name: Setup pnpm | |
uses: pnpm/[email protected] | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20.x | |
cache: pnpm | |
- name: Install and build | |
run: | | |
pnpm install | |
pnpm build | |
- name: Fetch Yuc | |
run: | | |
pnpm run bgmx fetch yuc | tee run.log | |
grep "^Info:" run.log > yuc-info.log || true | |
grep "^Error:" run.log > yuc-error.log || true | |
# Generate markdown summary | |
output=data/yuc.md | |
echo '# yuc' > $output | |
# Output info | |
if [ -n "$(cat yuc-info.log)" ]; then | |
echo '## Fetch yuc.wiki' >> $output | |
cat yuc-info.log | while IFS= read -r line; do | |
line="${line/Info: /}" | |
echo "- ${line}" >> $output | |
done | |
fi | |
# Output Error | |
error_diff=$(diff --new-line-format="%L" --old-line-format="" --unchanged-line-format="" "data/yuc-error.log" "yuc-error.log" || true) | |
if [ -n "$error_diff" ]; then | |
echo '## Error' >> $output | |
echo "$error_diff" | while IFS= read -r line; do | |
line="${line/Error: /}" | |
echo "- ${line}" >> $output | |
done | |
fi | |
# Update | |
grep "^Info:" run.log > data/yuc-info.log || true | |
grep "^Error:" run.log > data/yuc-error.log || true | |
- name: Push changes | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d %T") | |
BRANCH=${{ needs.prepare.outputs.branch }} | |
if [[ -n $(git status -s) ]]; then | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
git add data | |
git commit -m "feat(bgmd): update yuc.wiki at $TIMESTAMP" | |
git pull --rebase | |
git push origin $BRANCH | |
fi | |
tmdb: | |
name: Fetch TMDB | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
needs: | |
- prepare | |
- bangumi | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
- name: Setup pnpm | |
uses: pnpm/[email protected] | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 20.x | |
cache: pnpm | |
- name: Install and build | |
run: | | |
pnpm install | |
pnpm build | |
- name: Fetch TMDB | |
if: ${{ inputs.tmdb || github.event_name == 'schedule' }} | |
run: | | |
pnpm run bgmx fetch tmdb --overwrite=${{ needs.prepare.outputs.overwrite }} | tee run.log | |
grep "^Info:" run.log > tmdb-info.log || true | |
grep "^Error:" run.log > tmdb-error.log || true | |
# Generate markdown summary | |
output=data/tmdb.md | |
echo '# tmdb' > $output | |
# Output Info | |
echo '## Fetch new bangumi' >> $output | |
info_diff=$(diff --new-line-format="%L" --old-line-format="" --unchanged-line-format="" "data/tmdb-info.log" "tmdb-info.log" || true) | |
echo "$info_diff" | while IFS= read -r line; do | |
line="${line/Info: /}" | |
line="${line/fetch /}" | |
echo "- ${line}" >> $output | |
done | |
# Output Error | |
echo '## Error' >> $output | |
error_diff=$(diff --new-line-format="%L" --old-line-format="" --unchanged-line-format="" "data/tmdb-error.log" "tmdb-error.log" || true) | |
echo "$error_diff" | while IFS= read -r line; do | |
line="${line/Error: /}" | |
echo "- ${line}" >> $output | |
done | |
# Update | |
if [ "${{ needs.prepare.outputs.overwrite }}" = true ] ; then | |
grep "^Info:" run.log > data/tmdb-info.log || true | |
grep "^Error:" run.log > data/tmdb-error.log || true | |
else | |
grep "^Info:" run.log >> data/tmdb-info.log || true | |
grep "^Error:" run.log > data/tmdb-error.log || true | |
fi | |
- name: Push changes | |
run: | | |
TIMESTAMP=$(date +"%Y-%m-%d %T") | |
BRANCH=${{ needs.prepare.outputs.branch }} | |
if [[ -n $(git status -s) ]]; then | |
git config user.email "[email protected]" | |
git config user.name "GitHub Actions" | |
git add data | |
git commit -m "feat(bgmd): update TMDB data at $TIMESTAMP" | |
git pull --rebase | |
git push origin $BRANCH | |
fi | |
pr: | |
name: Create Pull Request | |
runs-on: ubuntu-latest | |
needs: | |
- prepare | |
- bangumi | |
- yuc | |
- tmdb | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Set Timezone | |
run: sudo timedatectl set-timezone "Asia/Shanghai" | |
- uses: actions/checkout@v4 | |
with: | |
ref: ${{ needs.prepare.outputs.branch }} | |
fetch-depth: 0 | |
- name: Create Pull Request | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
DATE=$(date +"%Y-%m-%d") | |
pr_list=$(gh pr list --label=data) | |
if [[ -n "$pr_list" ]]; then | |
echo "$pr_list" | awk '{print $1}' | xargs gh pr close -d | |
fi | |
pr_body=$(cat data/bangumi.md data/yuc.md data/tmdb.md || 'Update data') | |
if [[ $(git rev-list --count origin/main..origin/${{ needs.prepare.outputs.branch }}) -gt 0 ]]; then | |
gh pr create \ | |
--title "feat(bgmd): update data at $DATE" \ | |
--body "$pr_body" \ | |
--head "${{ needs.prepare.outputs.branch }}" \ | |
--label "data" | |
fi |