Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import time
- import json
- from datetime import datetime, timedelta
- from requests.adapters import HTTPAdapter
- from urllib3.util.retry import Retry
- # ================= CONFIG =================
- BASE_URL = "https://emrtds.nepalpassport.gov.np/iups-api/timeslots/79/{}/false"
- START_DATE = datetime(2025, 11, 28)
- END_DATE = datetime(2025, 12, 3)
- TELEGRAM_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXX"
- CHAT_ID = "XXXXXXXXXXXXXXX"
- CHECK_INTERVAL_MINUTES = 5
- HEADERS = {
- "Host": "emrtds.nepalpassport.gov.np",
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:145.0) Gecko/20100101 Firefox/145.0",
- "Accept": "application/json, text/plain, */*",
- "Accept-Language": "en-US,en;q=0.5",
- "Accept-Encoding": "gzip, deflate, br",
- "Connection": "close",
- }
- # ================= SETUP SESSIONS =================
- # Passport session with retries
- session = requests.Session()
- session.headers.update(HEADERS)
- retries = Retry(
- total=5,
- backoff_factor=0.3,
- status_forcelist=[500, 502, 503, 504]
- )
- session.mount("https://", HTTPAdapter(max_retries=retries))
- # Telegram session (clean, separate headers)
- telegram_session = requests.Session()
- # ================= GLOBAL CACHE =================
- # Stores last known timeslot data per date
- last_results = {}
- # ================= FUNCTIONS =================
- def send_telegram(text):
- try:
- telegram_session.post(
- f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage",
- json={"chat_id": CHAT_ID, "text": text},
- timeout=10
- )
- except Exception as e:
- print("Telegram error:", e)
- def process_api_response(data, date_str):
- """Detect any change in the timeslot data and send notification."""
- global last_results
- # Convert to stable JSON string for comparison
- current_json = json.dumps(data, sort_keys=True)
- if date_str not in last_results:
- last_results[date_str] = current_json
- print(f"[{date_str}] Initial data stored.")
- return
- if last_results[date_str] != current_json:
- print(f"[{date_str}] 🔔 CHANGE DETECTED!")
- send_telegram(
- f"🔔 Timeslot CHANGE detected for {date_str}\n"
- f"New data:\n{current_json}"
- )
- last_results[date_str] = current_json
- else:
- print(f"[{date_str}] No change.")
- def check_date(date_str):
- """Check timeslots for a single date."""
- url = BASE_URL.format(date_str)
- try:
- resp = session.get(url, timeout=10)
- try:
- data = resp.json()
- except json.JSONDecodeError:
- print(f"[{date_str}] Invalid JSON returned.")
- return
- process_api_response(data, date_str)
- except Exception as e:
- print(f"[{date_str}] Request error:", e)
- def run_check_once():
- """Loop through all dates in the range and check."""
- current = START_DATE
- while current <= END_DATE:
- ds = current.strftime("%Y-%m-%d")
- print(f"\n=== Checking {ds} ===")
- check_date(ds)
- current += timedelta(days=1)
- def main():
- print("🚀 Passport Timeslot Monitor Started")
- while True:
- run_check_once()
- print(f"\nWaiting 10 sec before next check...\n")
- time.sleep(10)
- # ================= RUN SCRIPT =================
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment