pythonrestamazon-ec2esri

Wildfire Current Perimeter Pulls


I have the following code that works great! I run it locally and no problem.

import requests
import os
from datetime import datetime

# Step 1: Define URL and parameters
url = "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/WFIGS_Interagency_Perimeters_Current/FeatureServer/0/query"
params = {
    "where": "1=1",
    "outFields": "*",
    "f": "geojson",
    "outSR": "4326",
    "resultRecordCount": 2000
}

# Step 2: Create folder to store files
output_folder = "wildfire_polygons"
os.makedirs(output_folder, exist_ok=True)

# Step 3: Generate dated filename
today_str = datetime.today().strftime("%Y-%m-%d")
output_filename = f"fire_perimeters_{today_str}.geojson"
output_path = os.path.join(output_folder, output_filename)

# Step 4: Download and save only if not already downloaded
if not os.path.exists(output_path):
    print(f"๐Ÿ“ฅ Downloading fire perimeter data for {today_str}...")
    response = requests.get(url, params=params)
    response.raise_for_status()

    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(response.text)

    print(f"โœ… Saved to {output_path}")
else:
    print(f"๐Ÿ“‚ File already exists for today: {output_path}")

However when I run this in an EC2 I get this error: ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) I know this is likely an ESRI blocking issue. I've seen it recommended that I switch the IP, but that's not really an option.

What would be the best way to run this? At this point it looks like I'm going to have to manually download this (https://data-nifc.opendata.arcgis.com/datasets/d1c32af3212341869b3c810f1a215824_0/explore?location=4.089341%2C135.040488%2C2.51). The ESRI source is as follows: https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/WFIGS_Interagency_Perimeters_Current/FeatureServer


Solution

  • This worked. Kovy had the right idea; adding headers resolved the issue.

    import requests
    import os
    from datetime import datetime
    
    # Step 1: Define URL and parameters
    url = "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/WFIGS_Interagency_Perimeters_Current/FeatureServer/0/query"
    params = {
        "where": "1=1",
        "outFields": "*",
        "f": "geojson",
        "outSR": "4326",
        "resultRecordCount": 2000
    }
    
    # Add realistic headers to simulate a browser request
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'en-US,en;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Connection': 'keep-alive',
        'Referer': 'https://www.arcgis.com/',
        'Origin': 'https://www.arcgis.com'
    }
    
    # Step 2: Create folder to store files
    output_folder = "wildfire_polygons"
    os.makedirs(output_folder, exist_ok=True)
    
    # Step 3: Generate dated filename
    today_str = datetime.today().strftime("%Y-%m-%d")
    output_filename = f"fire_perimeters_{today_str}.geojson"
    output_path = os.path.join(output_folder, output_filename)
    
    # Step 4: Download and save only if not already downloaded
    if not os.path.exists(output_path):
        print(f"Downloading fire perimeter data for {today_str}...")
    
        response = requests.get(url, params=params, headers=headers, timeout=30)
        response.raise_for_status()
    
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(response.text)
    
        print(f"Saved to {output_path}")
    else:
        print(f"File already exists for today: {output_path}")