I tried to make my small weather app, following the tutorial, but faced with a problem: OpenWeatherMap wouldn't give me a name of the day (watch the tutorial from my timecode to see my wanted result).
The tutorial: https://www.youtube.com/watch?v=2OEyYTdEah4&t=2921s (timecode 56:35)
Parts of my code:
The main one
def _bot_data():
_bot_data = []
#range is a number of days for forecast => limited by the API key
for index in range(1, 8):
_bot_data.append(
Row(
spacing=5,
alignment="spaceBetween",
controls=[
Row(
expand=1,
alignment="start",
controls=[
Container(
alignment=alignment.center,
content=Text(
#week day display
datetime.datetime.weekday(
datetime.datetime.fromtimestamp(
response['daily'][index]['dt']
)
)
)
)
]
)
]
)
)
return _bot_data
def _bottom():
_bot_column = Column(
alignment="center",
horizontal_alignment="center",
spacing=25,
)
for data in _bot_data():
_bot_column.controls.append(data)
bottom = Container(
padding=padding.only(top=280, left=20, right=20, bottom=20),
content=_bot_column
)
return bottom`
The requests part
API_KEY = "my API key"
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
CITY = "Minsk"
COUNTRY = "BY"
days_en = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
days = days_en
url = BASE_URL + "appid=" + API_KEY + "&q=" + CITY
response = requests.get(url).json()
My libraries
import datetime
import flet as ft
from flet import *
import requests
import datetime as dt
import math
And the error, what I have got:
File "C:\Users\bande\Documents\PycharmProjects\weather_app\main.py", line 290, in _bot_data
response['daily'][index]['dt']
~~~~~~~~^^^^^^^^^
KeyError: 'daily'
My main question is: how to fix my code (or just a request) to get days names from OpenWeatherMap (for example: today is Saturday and I wanna get the next list - Saturday, Sunday, Monday and etc.). Exactly from OpenWeatherMap, not from datetime library.
When I tried to change the request to responce['dt']
, I have got days what I need, but in the list of days each day was repeated a large number of times. I'm upset and tired :(
Finally I found a solution to the problem. OpenWeatnerMap has several API calls forms. I requested using the name of the city, but to get a forecast for several days you need to request using latitude and longitude (another API call form).
The solution should looks like this: Requesting latitude and longitude through the name of the city, and then write this data into variables and paste it into another API request form. Voila.
Example of API call:
https://api.openweathermap.org/data/2.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid={API key}
The source: https://openweathermap.org/api/one-call-api