File size: 2,010 Bytes
e4667e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
import requests
import os
import json
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("OPENAQ_API_KEY")
HEADERS = {"X-API-Key": API_KEY} if API_KEY else {}

def debug_v3():
    print(f"API Key Present: {bool(API_KEY)}")
    
    # 1. Find Location in Delhi
    # Trying radius search around Delhi coordinates (28.6139, 77.2090)
    url_loc = "https://api.openaq.org/v3/locations"
    params_loc = {
        "limit": 5,
        "coordinates": "28.6139,77.2090",
        "radius": 25000, # 25km
        "country_id": 134
    }
    
    print("\n--- 1. Searching Locations (V3) ---")
    try:
        resp = requests.get(url_loc, params=params_loc, headers=HEADERS, timeout=10)
        print(f"Status: {resp.status_code}")
        if resp.status_code == 200:
            data = resp.json()
            results = data.get("results", [])
            print(f"Found {len(results)} locations.")
            if results:
                loc_id = results[0]['id']
                print(f"Selected Location ID: {loc_id} ({results[0]['name']})")
                
                # 2. Get Sensors/Measurements for this Location
                url_sens = f"https://api.openaq.org/v3/locations/{loc_id}/sensors"
                print(f"\n--- 2. Fetching Sensors for Loc {loc_id} ---")
                resp_sens = requests.get(url_sens, headers=HEADERS, timeout=10)
                if resp_sens.status_code == 200:
                    sensors = resp_sens.json().get("results", [])
                    print(f"Found {len(sensors)} sensors.")
                    for s in sensors:
                        print(f" - {s['parameter']['name']}: {s.get('value', 'N/A')} {s['parameter']['units']}")
                    return sensors
                else:
                    print(f"Failed to get sensors: {resp_sens.text}")
        else:
            print(f"Failed to search locations: {resp.text}")
            
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    debug_v3()