Skip to content

Collect Traffic Data #2

Collect Traffic Data

Collect Traffic Data #2

Workflow file for this run

name: Collect Traffic Data
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 00:00 執行
workflow_dispatch:
permissions:
contents: write
jobs:
collect-traffic:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: pip install requests
- name: Collect traffic data
run: |
python3 -c "
import json, os
from datetime import datetime, timedelta
import requests
repo = 'ExpTechTW/trem-plugins'
api_url = f'https://api.github.com/repos/{repo}/traffic/views'
headers = {
'Authorization': f'Bearer {os.environ['GITHUB_TOKEN']}',
'Accept': 'application/vnd.github.v3+json'
}
response = requests.get(api_url, headers=headers)
new_data = response.json()
# 加入時間戳記
new_data['collected_at'] = datetime.utcnow().isoformat()
# 讀取現有數據或創建新的
if os.path.exists('traffic.json'):
with open('traffic.json', 'r') as f:
data = json.load(f)
else:
data = []
# 添加新數據
data.append(new_data)
# 只保留最近 30 天的數據
thirty_days_ago = datetime.utcnow() - timedelta(days=30)
data = [d for d in data if datetime.fromisoformat(d['collected_at']) > thirty_days_ago]
# 保存數據
with open('traffic.json', 'w') as f:
json.dump(data, f, indent=2)
"
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
- name: Commit and push if changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add traffic.json
git commit -m "Update traffic data" || exit 0
git push