Paper_metadata_download #302
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: Paper_metadata_download | |
on: | |
schedule: | |
- cron: '45 7 * * 1-5' # 每周一到周五 UTC 7:45 (北京时间 15:45) 运行 | |
workflow_dispatch: # 允许手动触发 | |
inputs: | |
date: | |
description: '指定要下载的历史数据日期 (YYYY-MM-DD格式)' | |
required: false | |
type: string | |
jobs: | |
download: | |
runs-on: ubuntu-latest | |
outputs: | |
download_status: ${{ steps.download.outputs.status }} | |
date: ${{ steps.set-date.outputs.date }} | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.11' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
- name: Set date | |
id: set-date | |
run: | | |
if [ "${{ github.event.inputs.date }}" != "" ]; then | |
echo "date=${{ github.event.inputs.date }}" >> $GITHUB_OUTPUT | |
else | |
echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT | |
fi | |
# 立即保存日期,不需要等待检查结果 | |
echo "${{ steps.set-date.outputs.date }}" > date.txt | |
- name: Download paper metadata | |
id: download | |
env: | |
TARGET_DATE: ${{ steps.set-date.outputs.date }} | |
run: | | |
python Paper_metadata_download.py --date $TARGET_DATE | |
DOWNLOAD_STATUS=$? | |
if [ $DOWNLOAD_STATUS -eq 0 ]; then | |
if [ -f "Paper_metadata_download/$TARGET_DATE.json" ]; then | |
echo "status=success" >> $GITHUB_OUTPUT | |
else | |
echo "status=no_data" >> $GITHUB_OUTPUT | |
fi | |
else | |
echo "status=error" >> $GITHUB_OUTPUT | |
fi | |
- name: Check download result | |
id: check | |
run: | | |
if [ "${{ steps.download.outputs.status }}" == "success" ]; then | |
echo "Papers downloaded successfully" | |
echo "status=success" >> $GITHUB_OUTPUT | |
elif [ "${{ steps.download.outputs.status }}" == "no_data" ]; then | |
echo "No papers available today" | |
echo "status=no_data" >> $GITHUB_OUTPUT | |
else | |
echo "Download failed" | |
echo "status=error" >> $GITHUB_OUTPUT | |
exit 1 | |
fi | |
- name: Upload workflow data | |
uses: actions/upload-artifact@v4 | |
with: | |
name: workflow-data | |
path: date.txt | |
retention-days: 1 | |
- name: Commit and push if there are changes | |
if: steps.check.outputs.status == 'success' | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
# 获取最新的更改 | |
git fetch origin main | |
# 重置工作区 | |
git reset --hard origin/main | |
# 添加所有更改 | |
git add Paper_metadata_download/ | |
# 检查是否有更改需要提交 | |
if git diff --staged --quiet; then | |
echo "No changes to commit" | |
exit 0 | |
fi | |
# 提交更改 | |
git commit -m "Update paper metadata for ${{ steps.set-date.outputs.date }}" | |
# 推送更改(如果失败则重试) | |
n=0 | |
until [ $n -ge 3 ] | |
do | |
git push origin main && break | |
n=$[$n+1] | |
if [ $n -lt 3 ]; then | |
git fetch origin main | |
git reset --hard origin/main | |
git add Paper_metadata_download/ | |
git commit -m "Update paper metadata for ${{ steps.set-date.outputs.date }}" | |
fi | |
sleep 5 | |
done | |