File tree Expand file tree Collapse file tree 7 files changed +585
-0
lines changed Expand file tree Collapse file tree 7 files changed +585
-0
lines changed Original file line number Diff line number Diff line change
1
+ # created by virtualenv automatically
2
+ bin /
3
+ lib /
4
+ __pycache__
5
+ .env
Original file line number Diff line number Diff line change
1
+ # Dependencies:
2
+ - requests
3
+ - pytz
Original file line number Diff line number Diff line change
1
+ from . import requests
Original file line number Diff line number Diff line change
1
+ from . import api , types
Original file line number Diff line number Diff line change
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 ())
You can’t perform that action at this time.
0 commit comments