TongcyDai

wikipedia_stats_updater.py

Dec 4th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 維基詞典統計自動更新機器人
  5. 從 wikistats.wmcloud.org 的 CSV API 獲取統計數據
  6. """
  7.  
  8. import time
  9. from datetime import datetime, timezone
  10. from stats_updater_base import BaseStatsUpdater
  11.  
  12. class WikipediaStatsUpdater(BaseStatsUpdater):
  13.     """維基詞典統計更新器"""
  14.    
  15.     def __init__(self, username: str, password: str):
  16.         """初始化維基詞典統計更新器"""
  17.         super().__init__(
  18.             username=username,
  19.             password=password,
  20.             site_domain='zh.wikipedia.org',
  21.             target_languages=['pl', 'arz', 'zh', 'ja', 'uk'],
  22.             csv_api_url='https://wikistats.wmcloud.org/api.php?action=dump&table=wikipedias&format=csv',
  23.             page_title='Wikipedia:统计/与邻近条目数量的语言版本比较',
  24.             log_filename='wikipedia_bot.log'
  25.         )
  26.    
  27.     def _get_edit_summary(self, date: str) -> str:
  28.         """獲取編輯摘要"""
  29.         return f"機器人:更新每日統計數據 ({date})"
  30.    
  31.     def _is_bot_account(self) -> bool:
  32.         """是否為機器人帳號"""
  33.         return True  # 維基詞典版本使用機器人帳號
  34.  
  35. def main():
  36.     """主函數"""
  37.     # 機器人帳號憑證
  38.     username = "USERNAME"
  39.     password = "PASSWORD"
  40.    
  41.     # 檢查是否在正確的時間執行(UTC 1:15 之後)
  42.     current_time = datetime.now(timezone.utc)
  43.     target_time = current_time.replace(hour=1, minute=15, second=0, microsecond=0)
  44.    
  45.     if current_time < target_time:
  46.         print(f"當前時間 {current_time.strftime('%H:%M')} UTC,等待到 01:15 UTC 後執行")
  47.         sleep_seconds = (target_time - current_time).total_seconds()
  48.         time.sleep(sleep_seconds)
  49.    
  50.     # 創建並運行機器人
  51.     updater = WikipediaStatsUpdater(username, password)
  52.     success = updater.run()
  53.    
  54.     if not success:
  55.         exit(1)
  56.  
  57. if __name__ == '__main__':
  58.     main()
Advertisement
Add Comment
Please, Sign In to add comment