Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
def fetch_xanthi_weather_data(url):
|
| 7 |
+
headers = {
|
| 8 |
+
'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'
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
response = requests.get(url, headers=headers)
|
| 13 |
+
response.raise_for_status()
|
| 14 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 15 |
+
|
| 16 |
+
data = {}
|
| 17 |
+
|
| 18 |
+
# Extracting Latest Values (Handle potential None)
|
| 19 |
+
date_time_span = soup.find_all('span')[10]
|
| 20 |
+
data['last'] = date_time_span.text.strip()
|
| 21 |
+
|
| 22 |
+
# Extracting Temperature (Handle potential None)
|
| 23 |
+
temp_element = soup.find('span', string='Θερμοκρασία')
|
| 24 |
+
if temp_element:
|
| 25 |
+
temp_value = soup.find('div', class_='lright').find('span')
|
| 26 |
+
data['temperature'] = temp_value.text.strip()
|
| 27 |
+
else:
|
| 28 |
+
data['temperature'] = 'N/A'
|
| 29 |
+
|
| 30 |
+
# Extracting Humidity (Handle potential None)
|
| 31 |
+
humidity_element = soup.find('span', string='Υγρασία')
|
| 32 |
+
if humidity_element:
|
| 33 |
+
humidity_div = humidity_element.find_next('div', class_='lright').find('span')
|
| 34 |
+
data['humidity'] = humidity_div.text.strip()
|
| 35 |
+
else:
|
| 36 |
+
data['humidity'] = 'N/A'
|
| 37 |
+
|
| 38 |
+
# Extracting Wind Speed (Handle potential None)
|
| 39 |
+
wind_element = soup.find('span', string='Άνεμος')
|
| 40 |
+
if wind_element:
|
| 41 |
+
wind_div = wind_element.find_next('div', class_='lright').find('span')
|
| 42 |
+
data['wind_speed'] = wind_div.text.strip()
|
| 43 |
+
else:
|
| 44 |
+
data['wind_speed'] = 'N/A'
|
| 45 |
+
# Extracting Wind Direction (Handle potential None)
|
| 46 |
+
sd_element = soup.find('span', string='Σημείο Δρόσου')
|
| 47 |
+
if sd_element:
|
| 48 |
+
sd_div = sd_element.find_next('div', class_='lright').find('span')
|
| 49 |
+
data['sd'] = sd_div.text.strip()
|
| 50 |
+
else:
|
| 51 |
+
data['sd'] = 'N/A'
|
| 52 |
+
|
| 53 |
+
# Extracting Pressure (Handle potential None)
|
| 54 |
+
pressure_element = soup.find('span', string='Βαρόμετρο')
|
| 55 |
+
if pressure_element:
|
| 56 |
+
pressure_div = pressure_element.find_next('div', class_='lright').find('span')
|
| 57 |
+
data['pressure'] = pressure_div.text.strip()
|
| 58 |
+
else:
|
| 59 |
+
data['pressure'] = 'N/A'
|
| 60 |
+
|
| 61 |
+
# Extracting Rainfall (Handle potential None)
|
| 62 |
+
rain_element = soup.find('span', string='Ραγδαιότητα')
|
| 63 |
+
if rain_element:
|
| 64 |
+
rain_div = rain_element.find_next('div', class_='lright').find('span')
|
| 65 |
+
data['rainfall'] = rain_div.text.strip()
|
| 66 |
+
else:
|
| 67 |
+
data['rainfall'] = 'N/A'
|
| 68 |
+
|
| 69 |
+
return data
|
| 70 |
+
|
| 71 |
+
except requests.exceptions.RequestException as e:
|
| 72 |
+
return {"error": f"Error fetching data: {e}"}
|
| 73 |
+
except Exception as e: #Catch all other exceptions.
|
| 74 |
+
return {"error": f"Error parsing data: {e}"}
|
| 75 |
+
|
| 76 |
+
def display_weather():
|
| 77 |
+
url = 'https://penteli.meteo.gr/stations/xanthi/'
|
| 78 |
+
weather_data = fetch_xanthi_weather_data(url)
|
| 79 |
+
|
| 80 |
+
if "error" in weather_data:
|
| 81 |
+
return weather_data["error"]
|
| 82 |
+
|
| 83 |
+
output = f"""
|
| 84 |
+
<div style="background-color: #ffffff; padding: 40px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
|
| 85 |
+
<h1>Last values: {weather_data.get('last', 'N/A')}</h1>
|
| 86 |
+
<h1>Temperature: {weather_data.get('temperature', 'N/A')}</h1>
|
| 87 |
+
<h1>Humidity: {weather_data.get('humidity', 'N/A')}</h1>
|
| 88 |
+
<h1>SD: {weather_data.get('sd', 'N/A')}</h1>
|
| 89 |
+
<h1>Pressure: {weather_data.get('pressure', 'N/A')}</h1>
|
| 90 |
+
<h1>Rainfall: {weather_data.get('rainfall', 'N/A')}</h1>
|
| 91 |
+
</div>
|
| 92 |
+
"""
|
| 93 |
+
time.sleep(1)
|
| 94 |
+
return output
|
| 95 |
+
|
| 96 |
+
iface = gr.Interface(
|
| 97 |
+
fn=display_weather,
|
| 98 |
+
inputs=None,
|
| 99 |
+
outputs=gr.HTML(label="Xanthi Weather"),
|
| 100 |
+
title="Μετεωρολογικός σταθμός Ξάνθης",
|
| 101 |
+
description=" "
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
iface.launch()
|