|
| 1 | +''' |
| 2 | +Uset the Weather Underground and Twilio APIs |
| 3 | +to text your honey the forecast. |
| 4 | +''' |
| 5 | +import json |
| 6 | + |
| 7 | +import requests |
| 8 | +from twilio.rest import TwilioRestClient |
| 9 | + |
| 10 | +# Info for Weather Underground API call |
| 11 | +STATE = '' |
| 12 | +CITY = '' |
| 13 | +WU_KEY = '' |
| 14 | +# Twilio API Keys |
| 15 | +TW_SID = '' |
| 16 | +TW_AUTH = '' |
| 17 | +# Your Twilio phone number |
| 18 | +MY_NUM = '' |
| 19 | +# Recepient's phone number |
| 20 | +HONEY_NUM = '' |
| 21 | + |
| 22 | + |
| 23 | +def current(): |
| 24 | + api_url = 'http://api.wunderground.com/api/{}/geolookup/conditions/q/{}/{}.json'.format(WU_KEY, STATE, CITY) |
| 25 | + request = requests.get(api_url).json() |
| 26 | + current_temp = request['current_observation']['temp_f'] |
| 27 | + return current_temp |
| 28 | + |
| 29 | + |
| 30 | +def forecast(): |
| 31 | + api_url = 'http://api.wunderground.com/api/{}/forecast/q/{}/{}.json'.format(WU_KEY, STATE, CITY) |
| 32 | + request = requests.get(api_url).json() |
| 33 | + high = request['forecast']['simpleforecast']['forecastday'][0]['high']['fahrenheit'] |
| 34 | + low = request['forecast']['simpleforecast']['forecastday'][0]['low']['fahrenheit'] |
| 35 | + return high, low |
| 36 | + |
| 37 | + |
| 38 | +def text(current, forecast): |
| 39 | + twilio = TwilioRestClient(TW_SID, TW_AUTH) |
| 40 | + |
| 41 | + msg = ''' |
| 42 | + Hey honey! Right now, the temperature(F) is {}.\n\n |
| 43 | + Today has a high of {}, and a low of {}.\n\n |
| 44 | + You can't opt-out of these texts.\n\n |
| 45 | + See you tomorrow!\n\n |
| 46 | + #HoneyWeather |
| 47 | + '''.format(current, forecast[0], forecast[1]) |
| 48 | + |
| 49 | + message = twilio.messages.create(body=msg, from_=MY_NUM, to=HONEY_NUM) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + current = current() |
| 54 | + forecast = forecast() |
| 55 | + text(current, forecast) |
0 commit comments