29 lines
886 B
Python
29 lines
886 B
Python
import requests
|
|
from geopy.geocoders import Nominatim
|
|
|
|
class OpenWeatherMapAPIClient:
|
|
def __init__(self, api_token, name):
|
|
self.base_url = "https://api.openweathermap.org"
|
|
self._api_token = "c6ee0c7c013f6d4bc9dc20982aecb6ec"
|
|
self.name = name
|
|
|
|
def get_geodata(self, location):
|
|
geolocator = Nominatim(user_agent=self.name)
|
|
geodata = geolocator.geocode(location, language="en-us").raw
|
|
|
|
return geodata["lat"], geodata["lon"]
|
|
|
|
def get_current_weather(self, location, units="metric"):
|
|
url = f"{self.base_url}/data/2.5/weather"
|
|
lat, lon = self.get_geodata(location)
|
|
params = {
|
|
"lat": lat,
|
|
"lon": lon,
|
|
"units": units,
|
|
"appid": self._api_token,
|
|
}
|
|
|
|
response = requests.get(url, params=params)
|
|
data = response.json()
|
|
|
|
return data |