Created
April 24, 2021 18:15
-
-
Save PsycheShaman/39385ccdb39f6dc1fcf94928835155ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.client | |
from geopy.geocoders import Nominatim | |
import json | |
def geo_code(city): | |
geo_locator = Nominatim(user_agent="medium_article_example") | |
location = geo_locator.geocode(city) | |
print(location.latitude, location.longitude) | |
return (location.latitude, location.longitude) | |
def get_weather(lat, lon): | |
conn = http.client.HTTPSConnection("weatherbit-v1-mashape.p.rapidapi.com") | |
with open('credentials.json') as secret: | |
headers = json.load(secret) | |
conn.request("GET", f"/current?lon={lon}&lat={lat}", headers=headers) | |
res = conn.getresponse() | |
data = res.read() | |
data = json.loads(data) | |
return data['data'][0]['weather']['description'] | |
def lambda_handler(event, context): | |
ret = dict() | |
res = [] | |
try: | |
for city in event["arguments"]: | |
city = city[0] | |
lat, lon = geo_code(city) | |
weather = get_weather(lat,lon) | |
res.append(weather) | |
ret['success'] = True | |
ret['results'] = res | |
except Exception as e: | |
ret['success'] = False | |
ret['error_msg'] = str(e) | |
return json.dumps(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment