From dba29966e91f946265f38834f30785085de7bffe Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Wed, 6 Oct 2021 17:53:32 +0300 Subject: [PATCH 1/7] capitalised weather --- api.py | 2 +- utilis.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 75f4d03..683f34d 100644 --- a/api.py +++ b/api.py @@ -2,7 +2,7 @@ import requests from requests.structures import CaseInsensitiveDict from CONF import COORDS_KEY, COORDS_KEY_WEATHER - +from utilis import Weather class API(object): """ diff --git a/utilis.py b/utilis.py index 4500407..3d8ecb0 100644 --- a/utilis.py +++ b/utilis.py @@ -1,6 +1,6 @@ -class weather: +class Weather: """Weather object: country : string city : string From 547b2ecf7586238c40c1ea933c2e1f92b8e873e3 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 09:16:50 +0300 Subject: [PATCH 2/7] weather object before final adjustments --- api.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 683f34d..48130a0 100644 --- a/api.py +++ b/api.py @@ -4,6 +4,7 @@ from CONF import COORDS_KEY, COORDS_KEY_WEATHER from utilis import Weather + class API(object): """ management class for the API functionality of the app @@ -36,7 +37,7 @@ def get_loc(self, location): 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) + # print(self.loc_list) return self.loc_list def choose_city(self, index): @@ -48,7 +49,13 @@ 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: data["temp"]["max"]-273.1 for day, data in enumerate(data_dict["daily"])} + self.min_temp = {day: data["temp"]["min"]-273.1 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"])} + # print(self.max_temp) if __name__ == "__main__": From 6046cb909af979426f7e69e1534f947476a7aa78 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 09:25:56 +0300 Subject: [PATCH 3/7] weather object before final adjustments --- utilis.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/utilis.py b/utilis.py index 3d8ecb0..012e810 100644 --- a/utilis.py +++ b/utilis.py @@ -1,6 +1,44 @@ +import json -class Weather: +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: """Weather object: country : string city : string @@ -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 \ No newline at end of file From 98d7306b8c05c3c68dbb09cf9414c9e1ac8688d8 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 10:51:31 +0300 Subject: [PATCH 4/7] API Ready no Error handling --- api.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/api.py b/api.py index 48130a0..0e86d05 100644 --- a/api.py +++ b/api.py @@ -2,7 +2,7 @@ import requests from requests.structures import CaseInsensitiveDict from CONF import COORDS_KEY, COORDS_KEY_WEATHER -from utilis import Weather +# from utilis import Weather class API(object): @@ -11,6 +11,10 @@ class API(object): """ def __init__(self): self.loc_list = [] + self.max_temp = {} + self.min_temp = {} + self.humidity = {} + self.status = {} def get_loc(self, location): """ @@ -50,12 +54,16 @@ def choose_city(self, index): resp = requests.get(url) data_dict = resp.json() - # pprint.pprint(data_dict["daily"]) - self.max_temp = {day: data["temp"]["max"]-273.1 for day, data in enumerate(data_dict["daily"])} - self.min_temp = {day: data["temp"]["min"]-273.1 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"])} - # print(self.max_temp) + pprint.pprint(data_dict["daily"]) + self.max_temp = {day: data["temp"]["max"]-273.1 for + day, data in enumerate(data_dict["daily"])} + self.min_temp = {day: data["temp"]["min"]-273.1 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__": From 29749a0c2affbf2b3985cb465bb18edd0ddee4a1 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 10:53:46 +0300 Subject: [PATCH 5/7] API Ready no Error handling --- api.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index 0e86d05..cf89400 100644 --- a/api.py +++ b/api.py @@ -2,7 +2,6 @@ import requests from requests.structures import CaseInsensitiveDict from CONF import COORDS_KEY, COORDS_KEY_WEATHER -# from utilis import Weather class API(object): @@ -45,6 +44,11 @@ def get_loc(self, location): 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/" \ From ff061e5e6a12fe1c1bd60e017ae93398f408c803 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 11:00:27 +0300 Subject: [PATCH 6/7] API Ready no Error handling --- api.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/api.py b/api.py index cf89400..855148b 100644 --- a/api.py +++ b/api.py @@ -17,13 +17,13 @@ def __init__(self): 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() @@ -59,18 +59,20 @@ def choose_city(self, index): data_dict = resp.json() pprint.pprint(data_dict["daily"]) - self.max_temp = {day: data["temp"]["max"]-273.1 for + self.max_temp = {day: round(data["temp"]["max"]-273.1, 2) for day, data in enumerate(data_dict["daily"])} - self.min_temp = {day: data["temp"]["min"]-273.1 for + 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) From b82b7cc9741b7e54298b7f768db9835c3441f846 Mon Sep 17 00:00:00 2001 From: "ariel.benabu" Date: Thu, 7 Oct 2021 11:01:48 +0300 Subject: [PATCH 7/7] rounding temp to 2 decimals --- api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 855148b..5418fa9 100644 --- a/api.py +++ b/api.py @@ -35,12 +35,12 @@ 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):