Run yfinance Stock Data Collector #54
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
#2024/06/18 18:43:44 GMT+08:00 | |
name: Run yfinance Stock Data Collector | |
on: | |
workflow_dispatch: # 使用 workflow_dispatch 触发方式 | |
schedule: | |
- cron: '0 1 * * 6' # 每周六北京时间上午9点执行一次 | |
jobs: | |
collect_stock_data: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
# 使用 actions/checkout@v4 以更新到 Node.js 20 | |
uses: actions/checkout@v4 | |
- name: Set up Python | |
# 使用 actions/[email protected] 以更新到 Node.js 20 | |
uses: actions/[email protected] | |
with: | |
python-version: '3.11' # 指定Python版本为3.11 | |
- name: Install dependencies | |
# 安装所需的Python依赖项 | |
run: pip install yfinance pandas openpyxl | |
- name: Remove old data directory | |
# 删除旧的数据目录 ./data/ | |
run: rm -rf ./data/ | |
- name: Get Start Time | |
# 获取脚本开始运行时间并保存到环境变量 START_TIME 中 | |
id: get_start_time | |
run: echo "START_TIME=$(date -u '+%s')" >> $GITHUB_ENV | |
- name: Run yfinance Stock Data Collector | |
# 运行Python脚本获取股票数据 | |
id: run_script | |
run: | | |
python 获取行情数据yuk2修正1.py | |
working-directory: ./ # 指定工作目录为当前目录 | |
- name: Get End Time and Duration | |
# 获取脚本结束时间并计算运行时长,转换时间为北京时间 | |
id: get_end_time | |
run: | | |
END_TIME=$(date -u '+%s') | |
DURATION=$((END_TIME - START_TIME)) | |
EXECUTION_TIME=$(date -u -d @$START_TIME '+%Y-%m-%d %H:%M:%S UTC') | |
REPORT_DATE=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %T') | |
echo "END_TIME=$END_TIME" >> $GITHUB_ENV | |
echo "DURATION=$(date -u -d @$DURATION '+%H:%M:%S')" >> $GITHUB_ENV | |
echo "EXECUTION_TIME=$EXECUTION_TIME" >> $GITHUB_ENV | |
echo "REPORT_DATE=$REPORT_DATE" >> $GITHUB_ENV | |
- name: Get IP Address | |
# 获取公共IP地址并保存到环境变量 IP_ADDRESS 中 | |
id: get_ip | |
continue-on-error: true # 确保即使获取IP地址失败,工作流也会继续执行 | |
run: | | |
set +x | |
IP_ADDRESS=$(curl -s https://api64.ipify.org?format=json | jq -r '.ip') | |
echo "IP_ADDRESS=$IP_ADDRESS" >> $GITHUB_ENV | |
set -x | |
- name: Send email notification | |
# 发送包含数据获取状态、执行时间、运行时长和IP地址的邮件通知 | |
uses: dawidd6/action-send-mail@v3 | |
with: | |
server_address: ${{ secrets.MAIL_ADDRESS }} | |
server_port: ${{ secrets.MAIL_PORT }} | |
username: ${{ secrets.MAIL_USERNAME }} | |
password: ${{ secrets.MAIL_PASSWORD }} | |
subject: 通知UT (${{ env.REPORT_DATE }}) # 指定北京时间作为邮件标题 | |
to: ${{ secrets.MAIL_TO }} | |
from: 数据通知/UT | |
html_body: | | |
数据获取状态: ${{ steps.run_script.outcome }}<br> | |
工作流执行时间: ${{ env.EXECUTION_TIME }}<br> | |
工作流运行时长: ${{ env.DURATION }}<br> | |
IP地址: ${{ env.IP_ADDRESS }} | |
continue-on-error: true # 确保即使邮件发送失败,工作流也会继续执行 | |
- name: Commit and push changes | |
# 提交并推送更改到GitHub仓库,确保仅在脚本成功运行时执行 | |
if: success() | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add . | |
git commit -m "Update stock data" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |