import requests, json, urllib3, time
urllib3.disable_warnings()

URL = "https://mtr.ciapongi.szablix.pl/mtr/api/map/arrivals?dimension=0"

# ID stacji Pabianice z Twojego screena
PAYLOAD = {
    "stationIdsHex": ["1E9D45C1D1AF6548"],
    "maxCountPerPlatform": 5
}

def main():
    print("=== OPERACJA: PODSŁUCH 'ARRIVALS' ===")
    try:
        r = requests.post(URL, json=PAYLOAD, verify=False, timeout=5)
        data = r.json()
        
        server_time = data.get('currentTime', int(time.time() * 1000))
        arrivals = data.get('data', {}).get('arrivals', [])
        
        if not arrivals:
            print("[-] Serwer nic nie zwrócił. Albo brak pociągów, albo zły format.")
            return
            
        print(f"✅ Znaleziono {len(arrivals)} nadchodzących pociągów dla Pabianic!\n")
        
        for idx, arr in enumerate(arrivals):
            dest = arr.get('destination', 'Nieznany')
            route = arr.get('routeNumber', 'Trasa')
            # MTR podaje 'arrival' i 'departure'
            dep_time = arr.get('departure', 0)
            
            # Liczymy różnicę w czasie
            ms_until = dep_time - server_time
            if ms_until < 0:
                print(f"[{idx+1}] {route} -> {dest} | ODJECHAŁ")
            else:
                mins = int(ms_until / 60000)
                secs = int((ms_until % 60000) / 1000)
                print(f"[{idx+1}] {route} -> {dest}")
                print(f"    Dokładny Timestamp: {dep_time}")
                print(f"    Za ile: {mins} min {secs:02d} sek")
            print("-" * 30)
            
    except Exception as e:
        print(f"[-] Błąd krytyczny: {e}")

if __name__ == "__main__":
    main()
