-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_sms.py
111 lines (81 loc) · 3.87 KB
/
send_sms.py
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
from twilio.rest import Client
from twilio.http.http_client import TwilioHttpClient
import requests
import os
from datetime import datetime, timedelta
def get_weather_dict(api_key, city_id='6176823'):
'''
Requests weather from OpenWeatherMap using the API key, api_key, and
city ID, city_id. Assumes object is valid JSON and returns a Python
dictionary of the JSON.
Inputs: Valid API key
'''
parameters = {'id': city_id, 'appid': api_key, 'units': 'metric'}
resp = requests.get('https://api.openweathermap.org/data/2.5/weather', parameters)
resp_json = resp.json()
return resp_json
def get_weather_info(weather_data):
temp = weather_data['main']['temp']
city = weather_data['name']
description = weather_data['weather'][0]['description']
wind_speed = weather_data['wind']['speed']
return temp, city, description, wind_speed
def get_nhl_dict():
'''
Requests NHL info from NHL Stats API. No API key required.
'''
yesterday = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
link = 'https://statsapi.web.nhl.com/api/v1/schedule/?date=' + yesterday
resp = requests.get(link)
resp_json = resp.json()
return resp_json
def get_nhl_message(nhl_data):
num_games = nhl_data['totalItems']
message = ''
for i in range(0, num_games):
away_team = nhl_data['dates'][0]['games'][i]['teams']['away']['team']['name']
home_team = nhl_data['dates'][0]['games'][i]['teams']['home']['team']['name']
away_score = nhl_data['dates'][0]['games'][i]['teams']['away']['score']
home_score = nhl_data['dates'][0]['games'][i]['teams']['home']['score']
home_wins = nhl_data['dates'][0]['games'][i]['teams']['home']['leagueRecord']['wins']
home_loss = nhl_data['dates'][0]['games'][i]['teams']['home']['leagueRecord']['losses']
if home_score > away_score:
message += "{} beat {} {}-{}. ".format(home_team, away_team, home_score, away_score)
else:
message += "{} beat {} {}-{}. ".format(away_team, home_team, away_score, home_score)
series = 'lead'
if home_wins == 4 or home_loss == 4:
series = 'win'
if home_wins > home_loss:
message += "{} {} the series {}-{}. ".format(home_team, series, home_wins, home_loss)
elif home_wins != home_loss:
message += "{} {} the series {}-{}. ".format(away_team, series, home_loss, home_wins)
else:
message += "The series is tied {0}-{0}. ".format(home_wins)
message += "\n"
return message
def send_to_sms(receive_number, message, send_number, account_sid, auth_token):
proxy_client = TwilioHttpClient()
proxy_client.session.proxies = {"http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080"}
client = Client(account_sid, auth_token, http_client=proxy_client)
client.messages.create(body=message, from_=send_number, to=receive_number)
if __name__ == '__main__':
account_sid = '' # Twilio account ID
auth_token = '' # Twilio authentication token
my_api = '' # API key from OpenWeather
my_city = '' # City ID
my_msg = ''
# Weather portion of message
weather_data = get_weather_dict(my_api, my_city)
my_name = 'Lucas'
temp, city, description, wind_speed = get_weather_info(weather_data)
my_msg += "Good Morning {}!\nThe weather in {} is {}. The current temperature" \
" is {} with a wind speed of {}.\n".format(my_name, city, description, temp, wind_speed)
# NHL portion of message, get_nhl_message function returns a string with
# message and skips weather steps due to complexity of data
nhl_data = get_nhl_dict()
my_msg += get_nhl_message(nhl_data)
recipient = '' # Your number
sender = '' # Verified Twilio number
print(my_msg)
send_to_sms(recipient, my_msg, sender, account_sid, auth_token)