-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextWeather.py
157 lines (133 loc) · 5.02 KB
/
textWeather.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! python3.11
import subprocess
import sys
import json
import datetime
import weatherCreds as c
try:
import requests
from twilio.rest import Client
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
finally:
import requests
try:
from twilio.rest import Client
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "twilio"])
finally:
from twilio.rest import Client
# daily response object
class DailyWeatherResponse:
def __init__(self):
self.number = '',
self.name = '',
self.isDaytime = '',
self.temperature = 0,
self.temperatureUnit = '',
self.temperatureTrend = '',
self. probabilityOfPrecipitation = {},
self.relativeHumidity = {},
self.windSpeed = '',
self.windDirection = '',
self.shortForecast = '',
self.detailedForecast = '',
# Hourly weather response object
class HourlyWeatherResponse:
def __init__(self):
self.number = '',
self.name = '',
self.startTime = '',
self.endTime = '',
self.isDaytime = '',
self.temperature = 0,
self.temperatureUnit = '',
self.temperatureTrend = '',
self.probabilityOfPrecipitation = {},
self.relativeHumidity = {},
self.windSpeed = '',
self.windDirection = '',
self.icon = '',
self.shortForecast = '',
self.detailedForecast = '',
# class Weather returns requested weather object
class Weather:
def __init__(self):
self.API_BASE = "https://api.weather.gov"
self.HEADERS = {"personalPythonApp": "[email protected]"}
self.DAILY = f"{self.API_BASE}/gridpoints/SHV/39,67/forecast"
self.HOURLY = f"{self.API_BASE}/gridpoints/SHV/39,67/forecast/hourly"
self.dailyObjects = []
self.hourlyObjects = []
def _makeRequest(self, request_type, headers):
self.weather = requests.get(request_type, headers=headers)
while self.weather.status_code != 200:
self.weather = requests.get(request_type, headers=headers)
return self.weather
# DailyRequest request daily weather
def _dailyRequest(self):
request = self._makeRequest(self.DAILY, self.HEADERS)
weather = json.loads(request.text)
for day in weather['properties']['periods']:
r = DailyWeatherResponse()
r.number = day['number']
r.name = day['name']
r.isDaytime = day['isDaytime']
r.temperature = day['temperature']
r.temperatureUnit = day['temperatureUnit']
r.temperatureTrend = day['temperatureTrend']
r.probabilityOfPrecipitation = day['probabilityOfPrecipitation']
r.relativeHumidity = day['relativeHumidity']
r.windSpeed = day['windSpeed']
r.windDirection = day['windDirection']
r.shortForecast = day['shortForecast']
r.detailedForecast = day['detailedForecast']
self.dailyObjects.append(r)
# HourlyRequest request hourly weather
def _hourlyRequest(self):
request = self._makeRequest(self.HOURLY, self.HEADERS)
weather = json.loads(request.text)
for hour in weather['properties']['periods']:
r = HourlyWeatherResponse()
r.number = hour['number']
r.name = hour['name']
r.startTime = hour['startTime']
r.endTime = hour['endTime']
r.isDaytime = hour['isDaytime']
r.temperature = hour['temperature']
r.temperatureUnit = hour['temperatureUnit']
r.temperatureTrend = hour['temperatureTrend']
r.probabilityOfPrecipitation = hour['probabilityOfPrecipitation']
r.relativeHumidity = hour['relativeHumidity']
r.windSpeed = hour['windSpeed']
r.windDirection = hour['windDirection']
r.icon = hour['icon']
r.shortForecast = hour['shortForecast']
r.detailedForecast = hour['detailedForecast']
self.hourlyObjects.append(r)
def TextWeather(self):
self._dailyRequest()
self._hourlyRequest()
text = ""
for i in self.dailyObjects:
if i.isDaytime == True:
message = f"{i.name}:\n{i.temperature:4}{i.temperatureUnit:2}~ {i.shortForecast}\n"
text += message
else:
message = f"{i.temperature:4}{i.temperatureUnit:2}~ {i.shortForecast}\n"
text += message
text += "\n"
for i in self.hourlyObjects:
today = datetime.date.today()
if str(today) in i.startTime:
message = f"{i.startTime[11:16]} :{i.temperature:4}{i.temperatureUnit:2}~ {i.shortForecast}\n"
text += message
client = Client(c.API_KEY, c.API_SECRET, c.ACCOUNT_SID)
client.messages.create(
from_ = c.FROM,
body = text,
to = c.TO
)
if __name__ == "__main__":
w = Weather()
w.TextWeather()