TongcyDai

check_if_updated_today.py

Dec 4th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. 檢查今天是否已經更新過維基百科統計
  4. 返回碼:0 = 需要更新, 1 = 已更新
  5. """
  6.  
  7. import sys
  8. from datetime import datetime
  9. from pathlib import Path
  10.  
  11. def check_if_updated_today():
  12.     """
  13.    檢查今天是否已更新
  14.    
  15.    實作方式可以是:
  16.    1. 檢查日誌文件中最後成功的時間戳
  17.    2. 檢查維基百科頁面的最後編輯時間
  18.    3. 檢查本地狀態文件
  19.    
  20.    這裡提供一個基於狀態文件的簡單實作
  21.    """
  22.    
  23.     # 狀態文件路徑
  24.     state_file = Path(__file__).parent / ".last_update_status.txt"
  25.    
  26.     today = datetime.now().strftime("%Y-%m-%d")
  27.    
  28.     try:
  29.         if state_file.exists():
  30.             last_update = state_file.read_text().strip()
  31.             if last_update == today:
  32.                 print(f"今日({today})已完成更新")
  33.                 return True
  34.        
  35.         print(f"今日({today})尚未更新")
  36.         return False
  37.        
  38.     except Exception as e:
  39.         print(f"檢查狀態時出錯: {e}")
  40.         # 發生錯誤時假設未更新,繼續執行
  41.         return False
  42.  
  43. if __name__ == "__main__":
  44.     updated = check_if_updated_today()
  45.     sys.exit(0 if updated else 1)
Advertisement
Add Comment
Please, Sign In to add comment