r1rk

Nyanpass_predicted

Oct 8th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | Source Code | 0 0
  1. import asyncio
  2. import aiohttp
  3. import sys
  4. from collections import deque
  5. from datetime import datetime
  6.  
  7. API_URL = "https://nyanpass.com/api/get_count"
  8. INTERVAL = 0.1
  9.  
  10. short_history = deque(maxlen=int(60 / INTERVAL))
  11. medium_history = deque(maxlen=int(3600 / INTERVAL))
  12. long_history = deque(maxlen=int(86400 / INTERVAL))
  13.  
  14. async def fetch_count(session):
  15.     try:
  16.         async with session.get(API_URL) as resp:
  17.             data = await resp.json()
  18.             count = int(data.get("count", 0))
  19.             return count
  20.     except Exception:
  21.         return None
  22.  
  23. def calculate_rate(history):
  24.     if len(history) < 2:
  25.         return 0
  26.     old_time, old_count = history[0]
  27.     new_time, new_count = history[-1]
  28.     elapsed_sec = (new_time - old_time).total_seconds()
  29.     delta_count = new_count - old_count
  30.     return delta_count / elapsed_sec if elapsed_sec > 0 else 0
  31.  
  32. async def display_loop():
  33.     async with aiohttp.ClientSession() as session:
  34.         while True:
  35.             count = await fetch_count(session)
  36.             now = datetime.now()
  37.             if count is not None:
  38.                 short_history.append((now, count))
  39.                 medium_history.append((now, count))
  40.                 long_history.append((now, count))
  41.  
  42.                 rate_short = calculate_rate(short_history)
  43.                 rate_medium = calculate_rate(medium_history)
  44.                 rate_long = calculate_rate(long_history)
  45.  
  46.                 per_minute = rate_short * 60
  47.                 per_hour = rate_medium * 3600
  48.                 per_day = rate_long * 86400
  49.  
  50.                 sys.stdout.write(
  51.                     f"\rにゃんぱすーされた回数: {count:,}回 | "
  52.                     f"予測: 1分 {per_minute:,.1f}回 / "
  53.                     f"1時間 {per_hour:,.0f}回 / "
  54.                     f"1日 {per_day:,.0f}回"
  55.                 )
  56.                 sys.stdout.flush()
  57.             await asyncio.sleep(INTERVAL)
  58.  
  59. if __name__ == "__main__":
  60.     try:
  61.         asyncio.run(display_loop())
  62.     except KeyboardInterrupt:
  63.         print("\n終了しました。")
Tags: python python3
Advertisement
Add Comment
Please, Sign In to add comment