From 5233ff368c61a4b37ae5a9b6c1e30167be7e30e5 Mon Sep 17 00:00:00 2001 From: Swapnanil Dutta <47251193+swapnanildutta@users.noreply.github.com> Date: Mon, 25 May 2020 17:40:18 +0530 Subject: [PATCH 1/3] Created weatherforecast.py Added weatherforecast.py to retrieve weather information of a location and return dictionary values. --- web_programming/weatherforecast.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 web_programming/weatherforecast.py diff --git a/web_programming/weatherforecast.py b/web_programming/weatherforecast.py new file mode 100644 index 000000000000..0afc0084af64 --- /dev/null +++ b/web_programming/weatherforecast.py @@ -0,0 +1,32 @@ +import json +import urllib.request, urllib.parse, urllib.error +import ssl + +ctx=ssl.create_default_context() +ctx.check_hostname=False +ctx.verify_mode=ssl.CERT_NONE +serviceurl="http://api.openweathermap.org/data/2.5/weather?q=" +key="&APPID=" #ADD YOUR KEY +Valid="True" +while Valid.lower()== "true": + address=input("Enter the location:") + if len(address)<1: exit(0) + url=serviceurl+address+key + + print("Retrieving",url) + uh=urllib.request.urlopen(url,context=ctx) + data=uh.read().decode() + print("Retrieved",len(data),'characters') + + try: + js=json.loads(data) + except: + js=None + + if not js or 'cod' not in js or js['cod']!=200: + print("===========Failure to Retrieve===========") + print(data) + exit(0) + continue + print(js) + Valid=input('Do you want to continue?') From c097dc7bbfc08fa2ac8a144161edb12a3a589ba7 Mon Sep 17 00:00:00 2001 From: Swapnanil Dutta <47251193+swapnanildutta@users.noreply.github.com> Date: Mon, 25 May 2020 17:49:28 +0530 Subject: [PATCH 2/3] Update weatherforecast.py --- web_programming/weatherforecast.py | 54 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/web_programming/weatherforecast.py b/web_programming/weatherforecast.py index 0afc0084af64..4a8a00e5de4c 100644 --- a/web_programming/weatherforecast.py +++ b/web_programming/weatherforecast.py @@ -2,31 +2,35 @@ import urllib.request, urllib.parse, urllib.error import ssl -ctx=ssl.create_default_context() -ctx.check_hostname=False -ctx.verify_mode=ssl.CERT_NONE -serviceurl="http://api.openweathermap.org/data/2.5/weather?q=" -key="&APPID=" #ADD YOUR KEY -Valid="True" -while Valid.lower()== "true": - address=input("Enter the location:") - if len(address)<1: exit(0) - url=serviceurl+address+key +def weather_forecast() + ctx=ssl.create_default_context() + ctx.check_hostname=False + ctx.verify_mode=ssl.CERT_NONE + serviceurl="http://api.openweathermap.org/data/2.5/weather?q=" + key="&APPID=" #ADD YOUR KEY + Valid="True" + while Valid.lower()== "true": + address=input("Enter the location:") + if len(address)<1: exit(0) + url=serviceurl+address+key - print("Retrieving",url) - uh=urllib.request.urlopen(url,context=ctx) - data=uh.read().decode() - print("Retrieved",len(data),'characters') + print("Retrieving",url) + uh=urllib.request.urlopen(url,context=ctx) + data=uh.read().decode() + print("Retrieved",len(data),'characters') - try: - js=json.loads(data) - except: - js=None + try: + js=json.loads(data) + except: + js=None - if not js or 'cod' not in js or js['cod']!=200: - print("===========Failure to Retrieve===========") - print(data) - exit(0) - continue - print(js) - Valid=input('Do you want to continue?') + if not js or 'cod' not in js or js['cod']!=200: + print("===========Failure to Retrieve===========") + print(data) + exit(0) + continue + print(js) + Valid=input('Do you want to continue?') + +if __name__=="__main__": + weather_forecast() From eefe96540d96542345e35a19531c2d520df3b6fd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 30 May 2020 17:24:27 +0200 Subject: [PATCH 3/3] Update and rename weatherforecast.py to current_weather.py --- web_programming/current_weather.py | 19 ++++++++++++++++ web_programming/weatherforecast.py | 36 ------------------------------ 2 files changed, 19 insertions(+), 36 deletions(-) create mode 100644 web_programming/current_weather.py delete mode 100644 web_programming/weatherforecast.py diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py new file mode 100644 index 000000000000..a35fff2d4615 --- /dev/null +++ b/web_programming/current_weather.py @@ -0,0 +1,19 @@ +from pprint import pprint + +import requests + +APPID = "" # <-- Put your OpenWeatherMap appid here! +URL_BASE = "http://api.openweathermap.org/data/2.5/weather" + + +def current_weather(location: str = "Chicago", appid: str = APPID) -> dict: + return requests.get(URL_BASE, params={"appid": appid, "q": location}).json() + + +if __name__ == "__main__": + while True: + location = input("Enter a location:").strip() + if location: + pprint(current_weather(location)) + else: + break diff --git a/web_programming/weatherforecast.py b/web_programming/weatherforecast.py deleted file mode 100644 index 4a8a00e5de4c..000000000000 --- a/web_programming/weatherforecast.py +++ /dev/null @@ -1,36 +0,0 @@ -import json -import urllib.request, urllib.parse, urllib.error -import ssl - -def weather_forecast() - ctx=ssl.create_default_context() - ctx.check_hostname=False - ctx.verify_mode=ssl.CERT_NONE - serviceurl="http://api.openweathermap.org/data/2.5/weather?q=" - key="&APPID=" #ADD YOUR KEY - Valid="True" - while Valid.lower()== "true": - address=input("Enter the location:") - if len(address)<1: exit(0) - url=serviceurl+address+key - - print("Retrieving",url) - uh=urllib.request.urlopen(url,context=ctx) - data=uh.read().decode() - print("Retrieved",len(data),'characters') - - try: - js=json.loads(data) - except: - js=None - - if not js or 'cod' not in js or js['cod']!=200: - print("===========Failure to Retrieve===========") - print(data) - exit(0) - continue - print(js) - Valid=input('Do you want to continue?') - -if __name__=="__main__": - weather_forecast()