Spaces:
Sleeping
Sleeping
| 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() | |