Files
Ingress/ingress.py
Matteo Rosati 1e76403565 update headers
2026-01-13 10:33:02 +01:00

81 lines
2.7 KiB
Python

from typing import List, Optional
import requests
from models import Plext, EventType
class IngressAPI:
BASE_URL = "https://intel.ingress.com/r"
def __init__(self, version: str, cookie: str):
self.version = version
self.headers = {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-language": "it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/json; charset=UTF-8",
"cookie": cookie,
"origin": "https://intel.ingress.com",
"priority": "u=1, i",
"referer": "https://intel.ingress.com/",
"sec-ch-ua": '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
}
# Extract CSRF token from cookie and add to headers
for item in cookie.split(";"):
if "csrftoken" in item:
self.headers["x-csrftoken"] = item.split("=")[1].strip()
break
def get_plexts(
self,
min_lat_e6: int,
min_lng_e6: int,
max_lat_e6: int,
max_lng_e6: int,
min_timestamp_ms: int = -1,
max_timestamp_ms: int = -1,
tab: str = "all",
event_types: Optional[List[EventType]] = None,
player_name: Optional[str] = None,
) -> List[Plext]:
"""
Fetches plexts from the Ingress API.
"""
payload = {
"minLatE6": min_lat_e6,
"minLngE6": min_lng_e6,
"maxLatE6": max_lat_e6,
"maxLngE6": max_lng_e6,
"minTimestampMs": min_timestamp_ms,
"maxTimestampMs": max_timestamp_ms,
"tab": tab,
"v": self.version,
}
response = requests.post(
f"{self.BASE_URL}/getPlexts", json=payload, headers=self.headers
)
response.raise_for_status()
try:
data = response.json()
except requests.exceptions.JSONDecodeError:
print(f"Error decoding JSON: {response.text}")
raise
plexts = [Plext.from_json(item) for item in data["result"]]
if event_types:
plexts = [p for p in plexts if p.get_event_type() in event_types]
if player_name:
plexts = [p for p in plexts if p.get_player_name() == player_name]
return plexts