From 3e9eeb62a6294ed93987a827662ff10bd3e8ad24 Mon Sep 17 00:00:00 2001 From: Ben Welsh Date: Sun, 9 Jul 2023 19:45:45 +0000 Subject: [PATCH] Fix 403s --- calfire_wildfires/__init__.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/calfire_wildfires/__init__.py b/calfire_wildfires/__init__.py index 8f8b5d7..d2dff86 100644 --- a/calfire_wildfires/__init__.py +++ b/calfire_wildfires/__init__.py @@ -9,20 +9,19 @@ def get_active_fires(): Returns GeoJSON with point geometry """ - - print("Requesting active Fires") # Request data headers = { - "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36", # noqa - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", # noqa - "Accept-Language": "en-US,en;q=0.9", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", # noqa } - r = requests.get( - "https://www.fire.ca.gov/umbraco/api/IncidentApi/GeoJsonList?inactive=false", - headers=headers, - ) - if r.status_code != 200: + url = "https://www.fire.ca.gov/umbraco/api/IncidentApi/GeoJsonList?inactive=false" + r = requests.get(url, headers=headers) + + # Make sure get a good response + try: + assert r.ok + except AssertionError: raise Exception(f"Request for data failed with {r.status_code} status code") + # Return it return r.json() @@ -33,8 +32,17 @@ def get_all_fires(): Returns GeoJSON with point geometry """ # Request data - r = requests.get("https://www.fire.ca.gov/umbraco/api/IncidentApi/GeoJsonList") - if r.status_code != 200: + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", # noqa + } + url = "https://www.fire.ca.gov/umbraco/api/IncidentApi/GeoJsonList" + r = requests.get(url, headers=headers) + + # Make sure the response works + try: + assert r.ok + except AssertionError: raise Exception(f"Request for data failed with {r.status_code} status code") + # Return it return r.json()