Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ class API(object):
"""
def __init__(self):
self.loc_list = []
self.max_temp = {}
self.min_temp = {}
self.humidity = {}
self.status = {}

def get_loc(self, location):
"""
get coords for the provided location
get coordinates for the provided location
:param location: a string describing the searched location
:return:
"""
# api url
url = "https://www.mapquestapi.com/geocoding/v1/" \
"address?key=%s"% COORDS_KEY
"address?key=%s" % COORDS_KEY

# setting up headers
headers = CaseInsensitiveDict()
Expand All @@ -31,15 +35,20 @@ def get_loc(self, location):
# request
resp = requests.post(url, headers=headers, data=data)
resp_dict = resp.json()

# filtering out the important data
# pprint.pprint(resp_dict["results"][0])
self.loc_list = [[loc["adminArea5"], loc["adminArea1"],
loc["adminArea3"], loc["adminArea4"], loc["displayLatLng"]]
for loc in resp_dict["results"][0]["locations"]]
print(self.loc_list)

return self.loc_list

def choose_city(self, index):
"""
receive an index for the chosen location in self.loc_list
:param index: the index of the requested city
:return: this object with all the needed data in it's attributes
"""
lat = self.loc_list[index][4]["lat"]
lon = self.loc_list[index][4]["lng"]
url = "https://api.openweathermap.org/data/2.5/" \
Expand All @@ -48,10 +57,22 @@ def choose_city(self, index):

resp = requests.get(url)
data_dict = resp.json()
# pprint.pprint(data_dict["daily"][])

pprint.pprint(data_dict["daily"])
self.max_temp = {day: round(data["temp"]["max"]-273.1, 2) for
day, data in enumerate(data_dict["daily"])}
self.min_temp = {day: round(data["temp"]["min"]-273.1, 2) for
day, data in enumerate(data_dict["daily"])}
self.humidity = {day: data["humidity"] for
day, data in enumerate(data_dict["daily"])}
self.status = {day: data["weather"][0]["main"] for
day, data in enumerate(data_dict["daily"])}

return self


if __name__ == "__main__":
# basic sanity test:
api = API()
api.get_loc("Israel")
api.choose_city(1)
40 changes: 39 additions & 1 deletion utilis.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
import json


def get_capital(state):
"""Function which return capital name of a state, if not found returns None"""
with open('database/country_capital.json') as json_file:
data = json.load(json_file)
for line in data:
if state.lower() == line['country'].lower():
return line['city']


def rename_country(code):
"""Function which returns country name out of code of the country
if not found returns the code"""
with open('database/country_code.json') as json_file:
data = json.load(json_file)
for country in data:
if country['code'] == code:
return country['name']
return code


def loc_list_to_human(loc_list):
"""Function which translate API's loc_list to humans"""
human_loc = []
for loc in loc_list:
append_string = ""
append_order = [0, 3, 2, 1]
if len(loc[1]) < 3:
loc[1] = rename_country(loc[1])
if len(loc[0]) < 2:
loc[0] = get_capital(loc[1])
for i in append_order:
if len(loc[i]) > 0:
append_string += (loc[i] + " ")
human_loc.append(append_string)
return human_loc


class weather:
Expand All @@ -15,4 +53,4 @@ def __init__(self, country, city, district, max_temp, min_temp, humidity, status
self.max_temp = max_temp
self.min_temp = min_temp
self.humidity = humidity
self.status = status
self.status = status