Skip to content

Commit 1776ae1

Browse files
committed
initial commit
Signed-off-by: multimokia <[email protected]>
0 parents  commit 1776ae1

File tree

7 files changed

+585
-0
lines changed

7 files changed

+585
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# created by virtualenv automatically
2+
bin/
3+
lib/
4+
__pycache__
5+
.env

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Dependencies:
2+
- requests
3+
- pytz

autoatmoschange/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import requests

autoatmoschange/requests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import api, types

autoatmoschange/requests/api.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import requests
2+
from .types import WeatherInfo, GeoLocation
3+
4+
#Main request url
5+
WEATHER_INFO_ENDPOINT = "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={apikey}"
6+
7+
#Geocoder url
8+
GEOCODE_INFO_ENDPOINT = "http://api.openweathermap.org/geo/1.0/direct?q={cityname}&limit=100&appid={apikey}"
9+
10+
def fetch_geolocation(cityname, apikey: str) -> list[GeoLocation]:
11+
"""
12+
Fetches geo info based on cityname, will return multiple values if multiple cities by the same name exist.
13+
14+
IN:
15+
cityname - Name of the city to get GeoLocation info for
16+
apikey - api key to perform the query
17+
18+
OUT:
19+
A list of GeoLocation objects
20+
"""
21+
rv: list[GeoLocation] = list()
22+
23+
resp = requests.get(
24+
GEOCODE_INFO_ENDPOINT.format(
25+
cityname=cityname,
26+
apikey=apikey
27+
)
28+
)
29+
30+
#Since there's likely more than one, we convert all
31+
for item in resp.json():
32+
rv.append(GeoLocation(**item))
33+
34+
return rv
35+
36+
def fetch_weather_info(lat: float, lon: float, apikey) -> WeatherInfo:
37+
"""
38+
Fetches weather info given latitude and longitude and an api key
39+
40+
IN:
41+
lat - Latitude of the location to get weather for
42+
lon - Longitude of the location to get weather for
43+
apikey - Api key to perform the query
44+
45+
OUT:
46+
WeatherInfo object representing the weather at the specified location
47+
"""
48+
resp = requests.get(
49+
WEATHER_INFO_ENDPOINT.format(
50+
lat=lat,
51+
lon=lon,
52+
apikey=apikey
53+
)
54+
)
55+
56+
return WeatherInfo.from_json(resp.json())

0 commit comments

Comments
 (0)