Created
October 10, 2017 22:42
-
-
Save casouri/c9a95c537292537c73a950b4d1239981 to your computer and use it in GitHub Desktop.
rain-warning
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
#! /Users/yuan/p/morning/env/bin/python3 | |
from twilio.rest import Client | |
from pyowm import OWM | |
import requests | |
import json | |
accountSID = 'AC4101e654b8cb7e5f9a22a4676ad65bf8' | |
authToken = '5ac4db2c7bd0a020cff1cf92d28af926' | |
destinationNumbers = ['+12675910741'] | |
TwilioNumber = '+12157743353' | |
location = 'Abington,us' | |
owmKey = 'fbd404dd3ee40fbca47887fa8d0b911a' | |
weatherURL = 'http://api.openweathermap.org/data/2.5/weather?q={location}&APPID={key}' | |
forecastURL = 'http://api.openweathermap.org/data/2.5/forecast?q={location}&APPID={key}' | |
weatherDict = { | |
'300': 'light intensity drizzle', | |
'301': 'drizzle', | |
'302': 'heavy intensity drizzle', | |
'310': 'light intensity drizzle rain', | |
'311': 'drizzle rain', | |
'312': 'heavy intensity drizzle rain', | |
'313': 'shower rain and drizzle', | |
'314': 'heavy shower rain and drizzle', | |
'321': 'shower drizzle', | |
'500': 'light rain', | |
'501': 'moderate rain', | |
'502': 'heavy intensity rain', | |
'503': 'very heavy rain', | |
'504': 'extreme rain', | |
'511': 'freezing rain', | |
'520': 'light intensity shower rain', | |
'521': 'shower rain', | |
'522': 'heavy intensity shower rain', | |
'531': 'ragged shower rain' | |
} | |
def fetchWeather(location, url): | |
response = requests.get(url.format(location=location, key=owmKey)) | |
data = json.loads(response.text) | |
weatherConditionID = data['list'][0]['weather'][0]['id'] | |
if weatherConditionID in weatherDict: | |
text = '''There will be {}'''.format(weatherDict[weatherConditionID]) | |
return text | |
return None | |
def sendSMS(msg, toNumber): | |
client = Client(accountSID, authToken) | |
message = client.messages.create(body=msg, from_=TwilioNumber, to=toNumber) | |
def main(): | |
# sendSMS('test\nnewline\nanother newline', destinationNumbers[0]) | |
text = fetchWeather(location, forecastURL) | |
if text: | |
sendSMS(text, destinationNumbers[0]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment