Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,117 +10,51 @@ import json
|
|
| 10 |
|
| 11 |
def get_raw_data(station_id):
|
| 12 |
"""
|
| 13 |
-
Get raw data from the
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
main_headers = {
|
| 19 |
-
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36',
|
| 20 |
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 21 |
-
'Accept-Language': 'en-US,en;q=0.5',
|
| 22 |
-
'Referer': 'https://www.weather.gov/',
|
| 23 |
-
'DNT': '1',
|
| 24 |
-
'Connection': 'keep-alive',
|
| 25 |
-
'Upgrade-Insecure-Requests': '1'
|
| 26 |
}
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
{
|
| 31 |
-
'url': 'https://www.weather.gov/wrh/timeseries',
|
| 32 |
-
'params': {
|
| 33 |
-
'site': station_id,
|
| 34 |
-
'hours': '720',
|
| 35 |
-
'units': 'english',
|
| 36 |
-
'chart': 'on',
|
| 37 |
-
'headers': 'on',
|
| 38 |
-
'obs': 'tabular',
|
| 39 |
-
'hourly': 'false',
|
| 40 |
-
'pview': 'standard',
|
| 41 |
-
'font': '12',
|
| 42 |
-
'plot': ''
|
| 43 |
-
}
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
'url': 'https://www.weather.gov/wrh/timeseries',
|
| 47 |
-
'params': {
|
| 48 |
-
'site': station_id,
|
| 49 |
-
'hours': '720',
|
| 50 |
-
'units': 'english',
|
| 51 |
-
'format': 'json'
|
| 52 |
-
}
|
| 53 |
-
},
|
| 54 |
-
{
|
| 55 |
-
'url': 'https://www.weather.gov/source/wrh/timeseries/getObs.php',
|
| 56 |
-
'params': {
|
| 57 |
-
'stn': station_id,
|
| 58 |
-
'hours': '720'
|
| 59 |
-
}
|
| 60 |
-
}
|
| 61 |
-
]
|
| 62 |
|
| 63 |
try:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
response = session.get(attempt['url'],
|
| 69 |
-
headers=main_headers,
|
| 70 |
-
params=attempt['params'])
|
| 71 |
-
|
| 72 |
-
print(f"Response status: {response.status_code}")
|
| 73 |
-
print(f"Content type: {response.headers.get('content-type', 'unknown')}")
|
| 74 |
-
|
| 75 |
-
if response.status_code == 200:
|
| 76 |
-
# Try to parse as JSON first
|
| 77 |
-
try:
|
| 78 |
-
data = response.json()
|
| 79 |
-
print("Successfully parsed JSON response")
|
| 80 |
-
return data
|
| 81 |
-
except:
|
| 82 |
-
pass
|
| 83 |
-
|
| 84 |
-
# If not JSON, check if it's CSV
|
| 85 |
-
if 'text/csv' in response.headers.get('content-type', '').lower():
|
| 86 |
-
print("Found CSV data")
|
| 87 |
-
return {'csv_data': response.text}
|
| 88 |
-
|
| 89 |
-
# Check for specific data markers in HTML
|
| 90 |
-
markers = ['Date/Time', 'Snow Depth', 'Temperature']
|
| 91 |
-
if any(marker in response.text for marker in markers):
|
| 92 |
-
print("Found data markers in response")
|
| 93 |
-
|
| 94 |
-
# Try to extract the data section
|
| 95 |
-
from bs4 import BeautifulSoup
|
| 96 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 97 |
-
|
| 98 |
-
# Look for content in both table and pre tags
|
| 99 |
-
data_containers = (
|
| 100 |
-
soup.find_all('table') +
|
| 101 |
-
soup.find_all('pre') +
|
| 102 |
-
soup.find_all('div', class_=['data', 'observations'])
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
for container in data_containers:
|
| 106 |
-
text = container.get_text()
|
| 107 |
-
if any(marker in text for marker in markers):
|
| 108 |
-
print("\nFound data container:")
|
| 109 |
-
print(text[:1000])
|
| 110 |
-
|
| 111 |
-
# Also print any relevant scripts
|
| 112 |
-
scripts = soup.find_all('script')
|
| 113 |
-
print(f"\nChecking {len(scripts)} script tags")
|
| 114 |
-
for script in scripts:
|
| 115 |
-
if script.string and ('var data' in script.string or 'observations' in script.string):
|
| 116 |
-
print("\nFound relevant script:")
|
| 117 |
-
print(script.string[:500])
|
| 118 |
-
|
| 119 |
-
return {'html': response.text}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error fetching data: {e}")
|
| 126 |
print("Full error details:")
|
|
@@ -356,4 +290,4 @@ with gr.Blocks() as demo:
|
|
| 356 |
# Launch the app
|
| 357 |
demo.launch()
|
| 358 |
|
| 359 |
-
#
|
|
|
|
| 10 |
|
| 11 |
def get_raw_data(station_id):
|
| 12 |
"""
|
| 13 |
+
Get raw data from the NWS API
|
| 14 |
"""
|
| 15 |
+
headers = {
|
| 16 |
+
'User-Agent': '(Weather Data Viewer, contact@yourdomain.com)',
|
| 17 |
+
'Accept': 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# Base URL for NWS API
|
| 21 |
+
base_url = "https://api.weather.gov"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# First get the station metadata
|
| 25 |
+
station_url = f"{base_url}/stations/{station_id}"
|
| 26 |
+
print(f"\nFetching station metadata from: {station_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
station_response = requests.get(station_url, headers=headers)
|
| 29 |
+
station_response.raise_for_status()
|
| 30 |
+
|
| 31 |
+
# Get observations with expanded parameters
|
| 32 |
+
obs_url = f"{base_url}/stations/{station_id}/observations"
|
| 33 |
+
params = {
|
| 34 |
+
'limit': 720,
|
| 35 |
+
'start': (datetime.utcnow() - timedelta(hours=720)).isoformat() + 'Z'
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
print(f"\nFetching observations from: {obs_url}")
|
| 39 |
+
print(f"Parameters: {params}")
|
| 40 |
+
|
| 41 |
+
obs_response = requests.get(obs_url, headers=headers, params=params)
|
| 42 |
+
obs_response.raise_for_status()
|
| 43 |
+
|
| 44 |
+
data = obs_response.json()
|
| 45 |
+
|
| 46 |
+
if 'features' in data:
|
| 47 |
+
print(f"\nFound {len(data['features'])} observations")
|
| 48 |
|
| 49 |
+
# Print sample of available fields
|
| 50 |
+
if len(data['features']) > 0:
|
| 51 |
+
print("\nFirst observation fields:")
|
| 52 |
+
properties = data['features'][0]['properties']
|
| 53 |
+
for key, value in properties.items():
|
| 54 |
+
print(f"{key}: {value}")
|
| 55 |
+
|
| 56 |
+
return data
|
| 57 |
+
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error fetching data: {e}")
|
| 60 |
print("Full error details:")
|
|
|
|
| 290 |
# Launch the app
|
| 291 |
demo.launch()
|
| 292 |
|
| 293 |
+
#
|