-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgoogle_updates.py
More file actions
50 lines (44 loc) · 1.62 KB
/
google_updates.py
File metadata and controls
50 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import os
from datetime import datetime, timedelta, timezone
from typing import List, Dict
import requests
PRODUCT_ID = "rGHU1u87FJnkP6W2GwMi"
INCIDENTS_URL = "https://status.search.google.com/incidents.json"
UPDATES_FILE = "updates.json"
def fetch_and_store_updates() -> List[Dict]:
"""Fetch incidents for the product and store them locally."""
response = requests.get(INCIDENTS_URL, timeout=10)
response.raise_for_status()
incidents = response.json()
product_incidents = [
inc for inc in incidents
if inc.get("service_key") == PRODUCT_ID
or any(p.get("id") == PRODUCT_ID for p in inc.get("affected_products", []))
]
with open(UPDATES_FILE, "w", encoding="utf-8") as f:
json.dump(product_incidents, f, indent=2)
return product_incidents
def get_recent_updates(days: int = 30) -> List[Dict]:
"""Return updates from the last ``days`` days.
Updates are fetched from Google's Search Status Dashboard and cached
locally in ``updates.json``.
"""
try:
data = fetch_and_store_updates()
except Exception:
if not os.path.exists(UPDATES_FILE):
raise
with open(UPDATES_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
cutoff = datetime.now(timezone.utc) - timedelta(days=days)
recent = []
for inc in data:
when = inc.get("most_recent_update", {}).get("when") or inc.get("begin")
try:
when_dt = datetime.fromisoformat(when.replace("Z", "+00:00"))
except Exception:
continue
if when_dt >= cutoff:
recent.append(inc)
return recent