Skip to content

Instantly share code, notes, and snippets.

@mamori017
Last active July 22, 2017 04:14
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 mamori017/2ea930398a09d8c4f33b611f712837b5 to your computer and use it in GitHub Desktop.
Save mamori017/2ea930398a09d8c4f33b611f712837b5 to your computer and use it in GitHub Desktop.
OpenWeatherMap weather forcast tweet from AWS Lambda
#coding:utf-8
from requests_oauthlib import OAuth1Session
from datetime import datetime
import json, urllib2
CK = '*****'
CS = '*****'
AT = '*****'
AS = '*****'
twUrl = 'https://api.twitter.com/1.1/statuses/update.json'
owmUrl = 'http://api.openweathermap.org/data/2.5/weather?id=1862415&APPID=*****'
def handler(event, context):
try:
ret = urllib2.urlopen(owmUrl)
root = json.loads(ret.read())
temp = root['main']['temp']
temp = str(temp - 273.15)
weather = root['weather']
city = root['name']
country = root['sys']['country']
for details in weather:
status = details['main']
desc = details['description']
dt = datetime.now().strftime("%Y/%m/%d %H:%M")
msg = ''
msg = msg + 'Weather Forecast' + ' (' + dt + ')' +'\r\n'
msg = msg + city + ',' + country + '\r\n'
msg = msg + 'Weather : ' + status + ' (' + desc + ')' + '\r\n'
msg = msg + 'Temp : ' + temp + u'℃' + '\r\n'
params = {"status": msg }
twitter = OAuth1Session(CK, CS, AT, AS)
req = twitter.post(twUrl, params = params)
if req.status_code == 200:
return msg
else:
return req.status_code
finally:
ret.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment