packages = ["requests", "pyodide-http"]
import requests
import pyodide_http
from js import getCityName
import datetime
#https://openweathermap.org/current#list
# Patch the Requests library so it works with Pyscript
pyodide_http.patch_all()
def get_weather():
api_key = '78eff36159a29dd4337140fea224081a'
location = getCityName()
url = f'https://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric'
response = requests.get(url)
data = response.json()
if response.status_code == 200:
nimi = data['name']
kuvaus = data['weather'][0]['description']
lampo = str(data['main']['temp'])
kosteus = str(data['main']['humidity'])
pilvi = str(data['clouds']['all'])
#get the current time get the difference works only in summer time!!!
current_time_a_unix = (data['dt']) - 10800
time_difference_seconds = (data['timezone'])
current_time_a = datetime.datetime.fromtimestamp(current_time_a_unix)
current_time_b = current_time_a + datetime.timedelta(seconds=time_difference_seconds)
aika = str((current_time_b))
paragraph = Element("nimi")
paragraph.write(nimi)
paragraph = Element("aika")
paragraph.write(aika)
paragraph = Element("kuvaus")
paragraph.write("Description: " + kuvaus)
paragraph = Element("lampo")
paragraph.write("temperature: " + lampo + "°C")
paragraph = Element("kosteus")
paragraph.write("Humidity: " + kosteus + "%")
paragraph = Element("pilvi")
paragraph.write("Cloudiness: " + pilvi + "%")
x = data['weather'][0]['icon']
global link
link = f'https://openweathermap.org/img/wn/{x}@2x.png'
return link
else:
print('Error: ' , data['message'])