fix: add sec-fetch headers and fallback to notification listing on 403

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-17 16:03:30 +08:00
parent 94b151ad9f
commit d6f09a37aa

View File

@@ -21,8 +21,9 @@ NTFY_URL = os.environ.get("NTFY_URL", "https://ntfy.example.com/zipair-alert"
NTFY_TOKEN = os.environ.get("NTFY_TOKEN", "") # optional, if your ntfy requires auth
STATE_FILE = os.environ.get("STATE_FILE", "last_seen.txt")
ZIPAIR_SITEMAP = "https://www.zipair.net/sitemap.xml"
ZIPAIR_NOTIF = "https://www.zipair.net/en/notification/{id}"
ZIPAIR_SITEMAP = "https://www.zipair.net/sitemap.xml"
ZIPAIR_NOTIF_LIST = "https://www.zipair.net/en/notification"
ZIPAIR_NOTIF = "https://www.zipair.net/en/notification/{id}"
# Keywords that must ALL appear (case-insensitive) in a notification page
# to trigger an alert. Tune these as needed.
@@ -36,9 +37,14 @@ HEADERS = {
"Chrome/124.0.0.0 Safari/537.36"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Language": "en-US,en;q=0.9,ja;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Referer": "https://www.zipair.net/",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Upgrade-Insecure-Requests": "1",
}
# ── Helpers ─────────────────────────────────────────────────────────────────
@@ -67,12 +73,22 @@ def get_notification_ids_from_sitemap() -> list[int]:
"""Parse the ZIPAIR sitemap and return all notification IDs found."""
print("Fetching sitemap …")
xml = fetch(ZIPAIR_SITEMAP)
if not xml:
print(" Sitemap unavailable.", file=sys.stderr)
if xml:
ids = [int(m) for m in re.findall(r"/notification/(\d+)", xml)]
if ids:
ids = sorted(set(ids))
print(f" Found {len(ids)} notification IDs in sitemap (max={ids[-1]})")
return ids
# Fallback: scrape the notification listing page
print(" Sitemap unavailable, trying notification listing page …")
html = fetch(ZIPAIR_NOTIF_LIST)
if not html:
print(" Notification listing page also unavailable.", file=sys.stderr)
return []
ids = [int(m) for m in re.findall(r"/notification/(\d+)", xml)]
ids = [int(m) for m in re.findall(r"/notification/(\d+)", html)]
ids = sorted(set(ids))
print(f" Found {len(ids)} notification IDs in sitemap (max={ids[-1] if ids else 'n/a'})")
print(f" Found {len(ids)} notification IDs from listing page (max={ids[-1] if ids else 'n/a'})")
return ids