Skip to content

Instantly share code, notes, and snippets.

@PsycheShaman
Created April 24, 2021 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PsycheShaman/39385ccdb39f6dc1fcf94928835155ef to your computer and use it in GitHub Desktop.
Save PsycheShaman/39385ccdb39f6dc1fcf94928835155ef to your computer and use it in GitHub Desktop.
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