-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert.py
More file actions
122 lines (94 loc) · 4.42 KB
/
alert.py
File metadata and controls
122 lines (94 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""Alert script"""
from datetime import date
from model import connect_to_db, User, AlertType, IndividualAlerts
import requests, crud
import json
# import jsonify
import os
import twilio
from twilio.rest import Client
from pprint import pprint
os.system("source secrets.sh")
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
twilio_phone_number = os.environ['TWILIO_PHONE_NUMBER']
openweatherkey = os.environ['OPENWEATHER_KEY']
def get_user():
"""Gets users, calls API, alerts users."""
# retrieve opted-in user location (city) data using SQLAlchemy
users = User.query.filter_by(opted_in="Yes").all()
# a set of all the users' unique cities
cities = set()
for user in users:
cities.add(user.city)
results = dict()
for city in cities:
payload = {'q': city,
'appid': openweatherkey,
'units': 'imperial',
'cnt': 8}
response = requests.get('http://api.openweathermap.org/data/2.5/forecast?',
params=payload)
weather = response.json()
pprint(weather)
# extract data returned from API call
weather_results = weather['list']
heat_indexes = []
# loop over the 40 weather results to get timestamp, temp, and humidity (for each city)
for i in range(len(weather_results)):
temp = weather_results[i]['main'].get('temp')
humidity = weather_results[i]['main'].get('humidity')
heat_index = crud.calculate_heat_index(temp, humidity) # run calculate_heat_index for each timestamp
heat_indexes.append(heat_index)
results[city] = max(heat_indexes)
# Twilio API calls: loop over users - if they are in city, send message
i = 0
date_sent = date.today()
print(date_sent)
for user in users:
if user.city in results:
if results[user.city] >= 40:
message = client.messages.create(
body=AlertType.query.filter_by(temp_range="129.3+").first().alert_text,
from_=twilio_phone_number,
to=user.phone
)
print(message.sid)
alert = AlertType.query.filter_by(temp_range="129.3+").first()
individual_alert = crud.create_individual_alert(user.user_id, alert.alert_type_id, date_sent) # create individual alert record
elif results[user.city] >= 30:
message = client.messages.create(
body=AlertType.query.filter_by(temp_range="105.9-129.2").first().alert_text,
from_=twilio_phone_number,
to=user.phone
)
print(message.sid)
alert = AlertType.query.filter_by(temp_range="105.9-129.2").first()
individual_alert = crud.create_individual_alert(user.user_id, alert.alert_type_id, date_sent) # create individual alert record
elif results[user.city] >= 20:
message = client.messages.create(
body=AlertType.query.filter_by(temp_range="89.7-105.8").first().alert_text,
from_=twilio_phone_number,
to=user.phone
)
print(message.sid)
alert = AlertType.query.filter_by(temp_range="89.7-105.8").first()
individual_alert = crud.create_individual_alert(user.user_id, alert.alert_type_id, date_sent) # create individual alert record
elif results[user.city] >= 10:
message = client.messages.create(
body=AlertType.query.filter_by(temp_range="78.8-89.6").first().alert_text,
from_=twilio_phone_number,
to=user.phone
)
print(message.sid)
alert = AlertType.query.filter_by(temp_range="89.7-105.8").first()
individual_alert = crud.create_individual_alert(user.user_id, alert.alert_type_id, date_sent) # create individual alert record
i += 1
else:
return
if __name__ == "__main__":
# DebugToolbarExtension(app)
from server import app
connect_to_db(app, 'heat-resilience-app')
get_user()