commit 8f3ae2ba01bdbf054aa4e09c63f4fd39fe88f168 Author: RikuSi Date: Wed Sep 20 23:28:10 2023 +0300 Upload files to "/" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5ff0675 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM ubuntu/nginx:1.18-22.04_beta +ENV TZ=Europe/Helsinki + +RUN apt-get update \ + && apt-get install -y python3-pip \ + && pip3 install geopandas \ + && pip3 install pandas geopy \ + && pip3 install requests + +COPY index.html /var/www/html/index.html + +COPY script.py /var/www/html/script.py + +CMD chmod +x /var/www/html/script.py + +CMD python3 /var/www/html/script.py + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1c406c --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +Säätiedot! Weather! + +Muunna script.py tiedostosta 15 ja 16 rivi. +Laita omat koordinaattisi missä asut, niin säätiedot haetaan sen mukaan. + + +sudo docker build -t weather . + +sudo docker run -d --name wttrin10 -p 8600:80 weather + +sudo docker exec -it wttrin10 /bin/bash + +http://localhost:8600 + +Scripti joka hakee tiedot päivittyy 20 min välein. +Selain päivittyy 10 min välein + diff --git a/index.html b/index.html new file mode 100644 index 0000000..8940327 --- /dev/null +++ b/index.html @@ -0,0 +1,4 @@ + + + +

Odota hetki...

diff --git a/script.py b/script.py new file mode 100644 index 0000000..90f91de --- /dev/null +++ b/script.py @@ -0,0 +1,67 @@ +import pandas as pd +from geopy.distance import geodesic +import requests +import subprocess +import time +from datetime import datetime +import os + +cmd = 'nginx' +os.system(cmd) + +while True: + + # Muuta seuraavat parametrit tarpeen mukaan + LATITUDE = "61.4981" + LONGITUDE = "23.7608" + MAX_RESULTS = 15 + + # Lataa maailman kaupunkien tietokanta + world_cities = pd.read_csv('https://download.geonames.org/export/dump/cities15000.zip', sep='\t', header=None, encoding='utf-8', low_memory=False) + world_cities.columns = ['geonameid', 'name', 'asciiname', 'alternatenames', 'latitude', 'longitude', 'feature_class', 'feature_code', 'country_code', 'cc2', 'admin1_code', 'admin2_code', 'admin3_code', 'admin4_code', 'population', 'elevation', 'dem', 'timezone', 'modification_date'] + + # Laske etäisyys ja valitse lähimmät kaupungit + world_cities['distance'] = world_cities.apply(lambda row: geodesic((LATITUDE, LONGITUDE), (row['latitude'], row['longitude'])).km, axis=1) + nearest_cities = world_cities.nsmallest(MAX_RESULTS, 'distance') + + # Hae säätiedot wttr.in-sivustolta curl-komennolla + weather_data_list = [] + cities_to_fetch = ["Death_valley", "London", "Sydney", "Oimjakon"] + for city_name in cities_to_fetch: + url = f"https://wttr.in/{city_name}?format=%l:+%c+%t+%h+%P+%w+%p+%S+%s" + response = requests.get(url) + weather_data = response.text.strip() + weather_data_list.append(weather_data) + + #Kello + current_time = datetime.now() + formatted_time = current_time.strftime("%H:%M") + + # Tulosta lähimpien kaupunkien nimet ja säätiedot + with open("/var/www/html/index_TEMP.html", "w") as f: # Avaa tiedosto kirjoitustilassa + f.write('\n') + f.write('\n') + f.write(f"

Sää tiedot klo:{formatted_time}, {MAX_RESULTS} lähintä kaupunkia tampereelta päin katsottuna..

\n") + f.write(f"Kaupunki, sää, °C, kosteus, paine, tuuli, sade, auringon nousu ja lasku ajat.\n") + + for index, city in nearest_cities.iterrows(): + if not pd.isnull(city['name']): + url = f"https://wttr.in/{city['name']}?format=%l:+%c+%t+%h+%P+%w+%p+%S+%s" + response = requests.get(url) + weather_data = response.text.strip() + city_name = city['name'] + f.write(f"

{weather_data}

") # Kirjoita tiedostoon säätiedot + + # Kirjoita muutaman kaupungin säätiedot tiedostoon + f.write(f"

Muutama kylmä ja kuuma kaupunki

\n") + f.write(f"Kaupunki, sää, °C, kosteus, paine, tuuli, sade, auringon nousu ja lasku.\n") + + for weather_data in weather_data_list: + f.write(f"

{weather_data}

\n") + + # Siirrä temp-tiedosto lopulliseen tiedostoon + cmd = 'mv /var/www/html/index_TEMP.html /var/www/html/index.html' + os.system(cmd) + + time.sleep(1200) # Odota 20 minuuttia ennen seuraavaa hakuajoa +