custom/weather
Модуль погоды был сгенерирован на сайте https://open-meteo.com/
Вы можете просто подставить свои координаты в скрипт, который приведен ниже:
import openmeteo_requests
import requests_cache
from retry_requests import retry
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
openmeteo = openmeteo_requests.Client(session=retry_session)
# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to
# assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
# координаты указать тут
params = {
"latitude": 52.2978,
"longitude": 104.2964,
"current": ["temperature_2m", "weather_code"],
"forecast_days": 1
}
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple
# locations or weather models
response = responses[0]
# Current values. The order of variables needs to be the same as requested.
current = response.Current()
current_temperature_2m = int(current.Variables(0).Value())
current_weather_code = current.Variables(1).Value()
if current_weather_code == 0:
res = ""
elif 1 <= current_weather_code <= 3:
res = ""
elif (current_weather_code == 45
or current_weather_code == 48):
res = ""
elif (current_weather_code == 51
or current_weather_code == 53
or current_weather_code == 55):
res = ""
elif (current_weather_code == 56
or current_weather_code == 57):
res = ""
elif (current_weather_code == 61
or current_weather_code == 63
or current_weather_code == 65):
res = ""
elif (current_weather_code == 66
or current_weather_code == 67):
res = ""
elif (current_weather_code == 71
or current_weather_code == 73
or current_weather_code == 75):
res = ""
elif current_weather_code == 77:
res = ""
elif 80 <= current_weather_code <= 82:
res = ""
elif 85 <= current_weather_code <= 86:
res = ""
elif current_weather_code == 95:
res = ""
elif (current_weather_code == 96
or current_weather_code == 99):
res = ""
print(f"{current_temperature_2m} {res}")
Для работы требуется установка некоторых зависимостей. Чтобы поставить их только для одного проекта с погодой создадим для проекта виртуальное окружение.
Раз уж мы работаем в python, то создать окружение можно командой:
python3 -m venv venv
Появится папка venv. Дальше окружение можно активировать:
source ./venv/bin/activate
А после уже ставить зависимости, список ниже
attrs==24.2.0
cattrs==24.1.2
certifi==2024.8.30
charset-normalizer==3.3.2
exceptiongroup==1.2.2
flatbuffers==24.3.25
idna==3.10
numpy==2.1.1
openmeteo_requests==1.3.0
openmeteo_sdk==1.16.0
pandas==2.2.3
platformdirs==4.3.6
python-dateutil==2.9.0.post0
pytz==2024.2
requests==2.32.3
requests-cache==1.2.1
retry-requests==2.0.0
six==1.16.0
typing_extensions==4.12.2
tzdata==2024.2
url-normalize==1.4.3
urllib3==2.2.3
Устанавливать их нужно пакетным менеджером python, который назвается pip, чтобы поставить всё одной командой, я перечислил все зависимости в файле requirements.txt. Таким образом, если этот файл присутствует в корне проекта, то можно установить все зависимости одной командой:
pip install -r requirements.txt
Все необходимое можно глянуть и скачать тут.