Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,57 +13,66 @@ def get_raw_data(station_id):
|
|
| 13 |
Get raw data from the WRH timeseries endpoint
|
| 14 |
"""
|
| 15 |
headers = {
|
| 16 |
-
'User-Agent': '(
|
| 17 |
-
'Accept': 'text/html,application/xhtml+xml'
|
| 18 |
}
|
| 19 |
|
| 20 |
-
# Use the WRH timeseries endpoint
|
| 21 |
url = f"https://www.weather.gov/wrh/timeseries"
|
| 22 |
params = {
|
| 23 |
'site': station_id,
|
| 24 |
-
'hours': 72,
|
| 25 |
'units': 'english',
|
|
|
|
|
|
|
| 26 |
'obs': 'tabular',
|
| 27 |
'hourly': 'false',
|
| 28 |
'pview': 'standard',
|
| 29 |
'font': '12',
|
| 30 |
-
'
|
| 31 |
-
'headers': 'on'
|
| 32 |
}
|
| 33 |
|
| 34 |
try:
|
| 35 |
print(f"\nFetching from WRH timeseries: {url}")
|
| 36 |
-
print(f"
|
| 37 |
|
| 38 |
response = requests.get(url, headers=headers, params=params)
|
| 39 |
print(f"Response status: {response.status_code}")
|
| 40 |
|
| 41 |
if response.status_code == 200:
|
|
|
|
| 42 |
from bs4 import BeautifulSoup
|
| 43 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
print(
|
| 56 |
-
print(table.attrs)
|
| 57 |
-
print(f"First few lines of table {i}:")
|
| 58 |
-
print(str(table)[:500])
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
print(
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# For now, return the raw HTML to help with debugging
|
| 67 |
return {'html': response.text}
|
| 68 |
else:
|
| 69 |
print(f"Error response content: {response.text[:500]}")
|
|
@@ -304,4 +313,3 @@ with gr.Blocks() as demo:
|
|
| 304 |
# Launch the app
|
| 305 |
demo.launch()
|
| 306 |
|
| 307 |
-
# requirements.txt
|
|
|
|
| 13 |
Get raw data from the WRH timeseries endpoint
|
| 14 |
"""
|
| 15 |
headers = {
|
| 16 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 17 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# Use the WRH timeseries endpoint with all required parameters
|
| 21 |
url = f"https://www.weather.gov/wrh/timeseries"
|
| 22 |
params = {
|
| 23 |
'site': station_id,
|
| 24 |
+
'hours': 72,
|
| 25 |
'units': 'english',
|
| 26 |
+
'chart': 'on',
|
| 27 |
+
'headers': 'on',
|
| 28 |
'obs': 'tabular',
|
| 29 |
'hourly': 'false',
|
| 30 |
'pview': 'standard',
|
| 31 |
'font': '12',
|
| 32 |
+
'plot': ''
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
try:
|
| 36 |
print(f"\nFetching from WRH timeseries: {url}")
|
| 37 |
+
print(f"Complete URL with parameters: {url}?{'&'.join(f'{k}={v}' for k, v in params.items())}")
|
| 38 |
|
| 39 |
response = requests.get(url, headers=headers, params=params)
|
| 40 |
print(f"Response status: {response.status_code}")
|
| 41 |
|
| 42 |
if response.status_code == 200:
|
| 43 |
+
# Look for pre tags or specific divs that might contain the data
|
| 44 |
from bs4 import BeautifulSoup
|
| 45 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 46 |
|
| 47 |
+
# Try finding pre-formatted text
|
| 48 |
+
pre_elements = soup.find_all('pre')
|
| 49 |
+
print(f"\nFound {len(pre_elements)} pre elements")
|
| 50 |
+
|
| 51 |
+
# Look for divs with specific classes that might contain data
|
| 52 |
+
data_divs = soup.find_all('div', class_=['data', 'observations', 'timeseries'])
|
| 53 |
+
print(f"Found {len(data_divs)} potential data divs")
|
| 54 |
+
|
| 55 |
+
# Look for any script tags that might contain data
|
| 56 |
+
scripts = soup.find_all('script')
|
| 57 |
+
print(f"Found {len(scripts)} script tags")
|
| 58 |
|
| 59 |
+
# Print content from each potential data source
|
| 60 |
+
if pre_elements:
|
| 61 |
+
print("\nContent from first pre element:")
|
| 62 |
+
print(pre_elements[0].text[:1000])
|
| 63 |
|
| 64 |
+
if data_divs:
|
| 65 |
+
print("\nContent from first data div:")
|
| 66 |
+
print(data_divs[0].text[:1000])
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# Look for a specific text pattern that matches the data format
|
| 69 |
+
text = response.text
|
| 70 |
+
start_marker = "Date/Time"
|
| 71 |
+
if start_marker in text:
|
| 72 |
+
print("\nFound data marker, extracting data section:")
|
| 73 |
+
data_section = text[text.index(start_marker):]
|
| 74 |
+
print(data_section[:1000])
|
| 75 |
|
|
|
|
| 76 |
return {'html': response.text}
|
| 77 |
else:
|
| 78 |
print(f"Error response content: {response.text[:500]}")
|
|
|
|
| 313 |
# Launch the app
|
| 314 |
demo.launch()
|
| 315 |
|
|
|