File size: 3,910 Bytes
bb3c103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gradio as gr
import requests
from bs4 import BeautifulSoup
import time

def fetch_xanthi_weather_data(url):
    headers = {
        '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'
    }

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')

        data = {}

        # Extracting Latest Values (Handle potential None)
        date_time_span = soup.find_all('span')[10] 
        data['last'] = date_time_span.text.strip()

        # Extracting Temperature (Handle potential None)
        temp_element = soup.find('span', string='Θερμοκρασία')
        if temp_element:
            temp_value = soup.find('div', class_='lright').find('span')
            data['temperature'] = temp_value.text.strip()
        else:
            data['temperature'] = 'N/A'

        # Extracting Humidity (Handle potential None)
        humidity_element = soup.find('span', string='Υγρασία')
        if humidity_element:
            humidity_div = humidity_element.find_next('div', class_='lright').find('span')
            data['humidity'] = humidity_div.text.strip()
        else:
            data['humidity'] = 'N/A'

        # Extracting Wind Speed (Handle potential None)
        wind_element = soup.find('span', string='Άνεμος')
        if wind_element:
            wind_div = wind_element.find_next('div', class_='lright').find('span')
            data['wind_speed'] = wind_div.text.strip()
        else:
            data['wind_speed'] = 'N/A'
        # Extracting Wind Direction (Handle potential None)
        sd_element = soup.find('span', string='Σημείο Δρόσου')
        if sd_element:
            sd_div = sd_element.find_next('div', class_='lright').find('span')
            data['sd'] = sd_div.text.strip()
        else:
            data['sd'] = 'N/A'

        # Extracting Pressure (Handle potential None)
        pressure_element = soup.find('span', string='Βαρόμετρο')
        if pressure_element:
            pressure_div = pressure_element.find_next('div', class_='lright').find('span')
            data['pressure'] = pressure_div.text.strip()
        else:
            data['pressure'] = 'N/A'

        # Extracting Rainfall (Handle potential None)
        rain_element = soup.find('span', string='Ραγδαιότητα')
        if rain_element:
            rain_div = rain_element.find_next('div', class_='lright').find('span')
            data['rainfall'] = rain_div.text.strip()
        else:
            data['rainfall'] = 'N/A'

        return data

    except requests.exceptions.RequestException as e:
        return {"error": f"Error fetching data: {e}"}
    except Exception as e: #Catch all other exceptions.
        return {"error": f"Error parsing data: {e}"}

def display_weather():
    url = 'https://penteli.meteo.gr/stations/xanthi/'
    weather_data = fetch_xanthi_weather_data(url)

    if "error" in weather_data:
        return weather_data["error"]

    output = f"""
    <div style="background-color: #ffffff; padding: 40px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
        <h1>Last values: {weather_data.get('last', 'N/A')}</h1>
        <h1>Temperature: {weather_data.get('temperature', 'N/A')}</h1>
        <h1>Humidity: {weather_data.get('humidity', 'N/A')}</h1>
        <h1>SD: {weather_data.get('sd', 'N/A')}</h1>
        <h1>Pressure: {weather_data.get('pressure', 'N/A')}</h1>
        <h1>Rainfall: {weather_data.get('rainfall', 'N/A')}</h1>
    </div>
    """
    time.sleep(1)
    return output

iface = gr.Interface(
    fn=display_weather,
    inputs=None,
    outputs=gr.HTML(label="Xanthi Weather"),
    title="Μετεωρολογικός σταθμός Ξάνθης",
    description=" "
)

iface.launch()