緯度経度、天気を取得するように
# location_utils2.py
import requests
from geopy.geocoders import Nominatim
def get_current_location_and_address():
# APIキーとZIPコードは予め設定しておく必要があります。
API_key = "APIキー"
zip_place = "437-郵便番号,JP"
# OpenWeatherMap API URL
url = f"https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&lang=ja&appid={API_key}"
# データの取得
response = requests.get(url)
jsondata = response.json()
# 緯度と経度の取得
latitude = jsondata['coord']['lat']
longitude = jsondata['coord']['lon']
#天気の取得
weather_description = jsondata['weather'][0]['description']
# 住所の取得(オプショナル)
geolocator = Nominatim(user_agent="geoapiExercises")
location = geolocator.reverse((latitude, longitude), language='ja')
address = location.address if location else None
return latitude, longitude, address, weather_description
へコード変更
location_utils2.py
を変更したので
それに伴い
# weather_check.py
import location_utils2
import requests
def get_weather_data(api_key, latitude, longitude):
# OpenWeather One Call API の URL
url = f"https://api.openweathermap.org/data/3.0/onecall?lat={latitude}&lon={longitude}&exclude=minutely,daily&appid={api_key}&units=metric"
response = requests.get(url)
return response.json()
def check_for_rain(weather_data):
hourly_forecast = weather_data.get('hourly', [])[:1] # 次の1時間の予報だけをチェック
for hour in hourly_forecast:
for weather in hour['weather']:
if weather['main'] == 'Rain':
return True
return False
# APIキーを設定
api_key = 'APIキー'
# location_utils2から緯度と経度を取得
latitude, longitude, _ , weather_description = location_utils2.get_current_location_and_address()
# 天気データを取得
weather_data = get_weather_data(api_key, latitude, longitude)
# 雨が予報されているかどうかをチェック
if check_for_rain(weather_data):
print("Alert: Rain is expected within the next hour!")
else:
print("No rain expected in the next hour.")
としたが
雨が降りそうだが降らないと出る
No rain expected in the next hour.
import location_utils2
# 緯度、経度、住所(無視)、天気を取得
latitude, longitude, _, weather_description = location_utils2.get_current_location_and_address()
# 緯度、経度、天気を表示
print(f"Latitude: {latitude}, Longitude: {longitude}")
print("Weather:", weather_description)
で一度
緯度経度と天気を出したら
Latitude: 緯度, Longitude: 経度
Weather: 小雨
となっている
import requests
import json
from pprint import pprint
# url = "https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&appid={API_key}"
url = "https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&lang=ja&appid={API_key}"
# xxxxx
url = url.format(zip_place = "437-郵便番号,JP", API_key = "APIキー")
jsondata = requests.get(url).json()
pprint(jsondata)
# 緯度経度のデータを抽出
latitude = jsondata['coord']['lat']
longitude = jsondata['coord']['lon']
# 緯度経度を表示
print("緯度:", latitude)
print("経度:", longitude)
で詳細のJSONを出しても
日本語になっている
{'base': 'stations',
'clouds': {'all': 99},
'cod': 200,
'coord': {'lat': 緯度, 'lon': 経度},
'dt': 1714511699,
'id': 0,
'main': {'feels_like': 18.27,
'grnd_level': 1001,
'humidity': 80,
'pressure': 1002,
'sea_level': 1002,
'temp': 18.3,
'temp_max': 18.3,
'temp_min': 18.3},
'name': 'Kawai',
'rain': {'1h': 0.32},
'sys': {'country': 'JP',
'id': 2092872,
'sunrise': 1714507077,
'sunset': 1714555965,
'type': 2},
'timezone': 32400,
'visibility': 10000,
'weather': [{'description': '小雨', 'icon': '10d', 'id': 500, 'main': 'Rain'}],
'wind': {'deg': 284, 'gust': 4.82, 'speed': 2.68}}
ただし mainではrain
次に別の日に
weather.py
を実行
天気は曇り
{'base': 'stations',
'clouds': {'all': 100},
'cod': 200,
'coord': {'lat': 緯度, 'lon': 経度},
'dt': 1714595140,
'id': 0,
'main': {'feels_like': 13.04,
'grnd_level': 1012,
'humidity': 90,
'pressure': 1013,
'sea_level': 1013,
'temp': 13.3,
'temp_max': 13.3,
'temp_min': 13},
'name': 'Kawai',
'sys': {'country': 'JP',
'id': 2092872,
'sunrise': 1714593416,
'sunset': 1714642414,
'type': 2},
'timezone': 32400,
'visibility': 10000,
'weather': [{'description': '厚い雲',
'icon': '04d',
'id': 804,
'main': 'Clouds'}],
'wind': {'deg': 87, 'gust': 6.7, 'speed': 3.31}}
つまりweather の main の部分を取得して表示すればOK
descriptionにしてしまうと
小雨とか分類が多くなってしまう
なので
#天気の取得
# weather_description = jsondata['weather'][0]['description']
weather_description = jsondata['weather'][0]['main']
というようにすれば
現在地の天気は
Weather: Clouds
となる
あとは一時間後の天気を取得する部分だけど
これはGPTのコードではできているけど
ほんとにあっているか検証する