Guest User

Untitled

a guest
Dec 1st, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import requests
  2. import time
  3. import json
  4. from datetime import datetime, timedelta
  5. from requests.adapters import HTTPAdapter
  6. from urllib3.util.retry import Retry
  7.  
  8. # ================= CONFIG =================
  9. BASE_URL = "https://emrtds.nepalpassport.gov.np/iups-api/timeslots/79/{}/false"
  10.  
  11. START_DATE = datetime(2025, 11, 28)
  12. END_DATE = datetime(2025, 12, 3)
  13.  
  14. TELEGRAM_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXX"
  15. CHAT_ID = "XXXXXXXXXXXXXXX"
  16.  
  17. CHECK_INTERVAL_MINUTES = 5
  18.  
  19. HEADERS = {
  20. "Host": "emrtds.nepalpassport.gov.np",
  21. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0",
  22. "Accept": "application/json, text/plain, */*",
  23. "Accept-Language": "en-US,en;q=0.5",
  24. "Accept-Encoding": "gzip, deflate, br",
  25. "Connection": "close",
  26. }
  27.  
  28. # ================= SETUP SESSIONS =================
  29. # Passport session with retries
  30. session = requests.Session()
  31. session.headers.update(HEADERS)
  32. retries = Retry(
  33. total=5,
  34. backoff_factor=0.3,
  35. status_forcelist=[500, 502, 503, 504]
  36. )
  37. session.mount("https://", HTTPAdapter(max_retries=retries))
  38.  
  39. # Telegram session (clean, separate headers)
  40. telegram_session = requests.Session()
  41.  
  42. # ================= GLOBAL CACHE =================
  43. # Stores last known timeslot data per date
  44. last_results = {}
  45.  
  46. # ================= FUNCTIONS =================
  47. def send_telegram(text):
  48. try:
  49. telegram_session.post(
  50. f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage",
  51. json={"chat_id": CHAT_ID, "text": text},
  52. timeout=10
  53. )
  54. except Exception as e:
  55. print("Telegram error:", e)
  56.  
  57.  
  58. def process_api_response(data, date_str):
  59. """Detect any change in the timeslot data and send notification."""
  60. global last_results
  61.  
  62. # Convert to stable JSON string for comparison
  63. current_json = json.dumps(data, sort_keys=True)
  64.  
  65. if date_str not in last_results:
  66. last_results[date_str] = current_json
  67. print(f"[{date_str}] Initial data stored.")
  68. return
  69.  
  70. if last_results[date_str] != current_json:
  71. print(f"[{date_str}] 🔔 CHANGE DETECTED!")
  72. send_telegram(
  73. f"🔔 Timeslot CHANGE detected for {date_str}\n"
  74. f"New data:\n{current_json}"
  75. )
  76. last_results[date_str] = current_json
  77. else:
  78. print(f"[{date_str}] No change.")
  79.  
  80.  
  81. def check_date(date_str):
  82. """Check timeslots for a single date."""
  83. url = BASE_URL.format(date_str)
  84. try:
  85. resp = session.get(url, timeout=10)
  86. try:
  87. data = resp.json()
  88. except json.JSONDecodeError:
  89. print(f"[{date_str}] Invalid JSON returned.")
  90. return
  91.  
  92. process_api_response(data, date_str)
  93.  
  94. except Exception as e:
  95. print(f"[{date_str}] Request error:", e)
  96.  
  97.  
  98. def run_check_once():
  99. """Loop through all dates in the range and check."""
  100. current = START_DATE
  101. while current <= END_DATE:
  102. ds = current.strftime("%Y-%m-%d")
  103. print(f"\n=== Checking {ds} ===")
  104. check_date(ds)
  105. current += timedelta(days=1)
  106.  
  107.  
  108. def main():
  109. print("🚀 Passport Timeslot Monitor Started")
  110. while True:
  111. run_check_once()
  112. print(f"\nWaiting 10 sec before next check...\n")
  113. time.sleep(10)
  114.  
  115.  
  116. # ================= RUN SCRIPT =================
  117. if __name__ == "__main__":
  118. main()
  119.  
Add Comment
Please, Sign In to add comment