-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
327 lines (299 loc) · 12.6 KB
/
actions.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import socket
import time
from smtplib import SMTP, SMTP_SSL, SMTP_SSL_PORT
import smtplib, ssl
from imap_tools import MailBox, AND
import email
import credLib
from urllib.request import Request, urlopen
import requests
from bs4 import BeautifulSoup
import html2text
import wikipedia
import roku
import shodan
import os
import json
from email.message import EmailMessage
from roku import Roku
from mac_vendor_lookup import MacLookup
import icloudaccess
import sys
from pygooglenews import GoogleNews
#screen /dev/cu.raspberrypi-SerialPort 115200
sys.path.insert(0, './wyze_sdk_main')
sys.path.insert(0, './gmailapi')
import wisewrapper
import gmailfunctions
#network functions
def networkscan():
devices = []
for device in os.popen('arp -a'):
devices.append(device)
return(devices)
def sendUDP(ip, message):
UDP_IP = "127.0.0.1"
if(ip != ""):
UDP_IP = ip
UDP_PORT = 5005
MESSAGE = message
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(bytes(MESSAGE, "utf-8"), (UDP_IP, UDP_PORT))
return("COMP")
def TV_SCAN():
return(Roku.discover())
def TV_POWR(TV):
roku = Roku(TV)
#print(roku)
roku.power()
return("COMP")
def TV_APP_INIT(TVIP):
roku = Roku(TVIP)
applist = roku.apps
return(applist)
def TV_SEARCH(TVIP, APPSTRING):
roku = Roku(TVIP)
roku.search()
retval = roku.literal(APPSTRING)
#end network functions
#weather functions
def getCurrentTemp(state,city):
url = "https://www.wunderground.com/weather/us/" + state + "/" + city
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
webpage= str(webpage)
y = (webpage.find('wu-value-to" style="color:#feae3c;">'))
z = len('wu-value-to" style="color:#feae3c;">')+y
webpage = webpage[z:]
y2 = webpage.find('</span> <span _n')
currenttemp=webpage[y2-2:y2]
return(currenttemp)
def getPrecipitation(state,city):
url = "https://www.wunderground.com/precipitation/us/" + state + "/" + city
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
webpage= str(webpage)
x = webpage.find('class="statement-text"')
x = x + 23
webpage=webpage[x:]
y = webpage.find("</span>")
webpage = webpage[:y]
return(webpage)
#end weather functions
#communications
def getMail():
mail = gmailfunctions.readMail(credLib.returnbykey("email","emailU"))
return(mail)
def sendTextMail(message, endpoint):
return(gmailfunctions.sendTextMail(message,endpoint, credLib.returnbykey("email","emailU")))
#end communications
#news and queries
def brokengetHeadlines():
headlines = []
url = "https://www.reuters.com/world/"
req = Request(url, headers ={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
webpage= str(webpage)
webpage = html2text.html2text(webpage)
for x in range(0, 11):
x = webpage.find('media-story-card')
webpage = webpage[x+len('class="MediaStoryCard__playlist___3Zn6uF story-card"')+1:]
f = webpage
findex = f.find('</span>')
f = f[:findex]
f = f.replace("\\n", "")
f = f. replace("'", "")
f = f. replace(";", "")
f = f.replace("\\t", "")
f = f.replace("\\xe2", "")
f = f.replace("\\x99s", "")
f = f.replace("x99", "")
f = f.replace("x98", "")
f = f.replace("\\x80", "")
f = f.replace("\\", "")
f = f.replace("\\n", "")
f = f.replace("\\t", "")
webpage = webpage[findex:len(webpage)]
headlines.append(f)
return(headlines)
def getHeadlines():
gn = GoogleNews(lang = 'en', country = 'US')
top = gn.top_news()
return(top)
def summarize(endpoint):
return(wikipedia.summary(endpoint))
#end news and queries
def shodanHostQuery(host):
SHODAN_API_KEY = ""
api = shodan.Shodan(SHODAN_API_KEY)
host = api.host(host)
# Print general info
print("""IP: {}Organization: {}Operating System: {}""".format(host['ip_str'], host.get('org', 'n/a'), host.get('os', 'n/a')))
for item in host['data']:
print("""
Port: {}
Banner: {}
""".format(item['port'], item['data']))
# Print all banners
#locale functions
#distance = 0.01549275362
def latlonglookup(lat,long):
us_state_to_abbrev = {"Alabama": "AL","Alaska": "AK","Arizona": "AZ","Arkansas": "AR","California": "CA","Colorado": "CO","Connecticut": "CT","Delaware": "DE","Florida": "FL","Georgia": "GA","Hawaii": "HI","Idaho": "ID","Illinois": "IL","Indiana": "IN","Iowa": "IA","Kansas": "KS","Kentucky": "KY","Louisiana": "LA","Maine": "ME","Maryland": "MD","Massachusetts": "MA","Michigan": "MI","Minnesota": "MN","Mississippi": "MS","Missouri": "MO","Montana": "MT","Nebraska": "NE","Nevada": "NV","New Hampshire": "NH","New Jersey": "NJ","New Mexico": "NM","New York": "NY","North Carolina": "NC","North Dakota": "ND","Ohio": "OH","Oklahoma": "OK","Oregon": "OR","Pennsylvania": "PA","Rhode Island": "RI","South Carolina": "SC","South Dakota": "SD","Tennessee": "TN","Texas": "TX","Utah": "UT","Vermont": "VT","Virginia": "VA","Washington": "WA","West Virginia": "WV","Wisconsin": "WI","Wyoming": "WY","District of Columbia": "DC","American Samoa": "AS","Guam": "GU","Northern Mariana Islands": "MP","Puerto Rico": "PR","United States Minor Outlying Islands": "UM","U.S. Virgin Islands": "VI"}
print(str(lat))
print(str(long))
response = requests.get("https://api.mapbox.com/geocoding/v5/mapbox.places/"+str(long)+","+str(lat)+".json?limit=1&types=country%2Cregion%2Caddress%2Cplace%2Clocality%2Cpoi&language=en&access_token=pk.eyJ1IjoiY2xlb21hcCIsImEiOiJjbDF0cjN0ZmswNGRtM2pwYWJmcnBxNnRuIn0.PI9vXaY42dMAGkNU0JBBWA")
address = response.json()['features'][0]['place_name_en']
for key in us_state_to_abbrev:
if(key in address):
address = address.replace(key, us_state_to_abbrev[key])
print(address)
addressfirst = address.split(",")
country = addressfirst[len(addressfirst)-1]
statezipsplit = addressfirst[len(addressfirst)-2].split(" ")
print(statezipsplit)
faddress = addressfirst[0], addressfirst[1].replace(" ", ""), statezipsplit[1].replace(" ", ""), statezipsplit[2].replace(" ", ""), country
return(faddress)
#print(response.content['features']['place_name_en'])
def load_map(lat, long, zoom):
response = requests.get("https://api.mapbox.com/styles/v1/cleomap/cl1tw9s9n002t14r6mu7t1b3v/static/"+str(long)+","+str(lat)+","+str(zoom)+",0/300x300@2x?access_token=pk.eyJ1IjoiY2xlb21hcCIsImEiOiJjbDF0cjN0ZmswNGRtM2pwYWJmcnBxNnRuIn0.PI9vXaY42dMAGkNU0JBBWA")
from io import BytesIO
from PIL import Image
img = Image.open(BytesIO(response.content))
img.show()
def get_incidents(lat, long, radius):
incidents = []
url = "https://citizen.com/api/incident/trending?lowerLatitude=" + str(lat-radius) + "&lowerLongitude=" + str(long-radius) + "&upperLatitude=" + str(lat+radius) + "&upperLongitude=" + str(long+radius)+ "&fullResponse=true&limit=20"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req)
data = json.loads(webpage.read().decode(webpage.info().get_param('charset') or 'utf-8'))
for i in data["results"]:
text = i["raw"]
timestamp = i["ts"]
rawloc = i["rawLocation"]
lat = i["latitude"]
long = i["longitude"]
severity = i["severity"]
key = i["key"]
police = i["police"]
timestamp = time.strftime('%Y-%m-%d %I:%M:%S %p', time.localtime(timestamp/1000))
incidents.append([text, timestamp, rawloc, lat, long, severity, key, police])
return(incidents)
def get_atlas_obscura(lat,long,page):
areas_of_interest = {}
url = "https://www.atlasobscura.com/search?utf8=%E2%9C%93&q=&location=&nearby=true&lat="+str(lat)+"&lng="+str(long)+"&page="+str(page)
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
webpage= str(webpage, 'utf-8')
webpage = html2text.html2text(webpage)
webpage = webpage[webpage.find('Nearby Places'):webpage.find("#### Get Our Email Newsletter")]
webpage = webpage[webpage.find('######'):]
invariant = False
intervals = []
webslice = webpage
x = 0
while(invariant==False):
if(webslice.find('######',x+1)==-1):
invariant=True
else:
#slice up webpage and pull the pieces
slice_interval_a = webslice.find('######',x)
slice_interval_b = webslice.find('######',x+1)
intervals.append(webslice[slice_interval_a:slice_interval_b])
webslice=webslice[slice_interval_b:]
#flesh out the intervals into areas of interest
#take title
select_from_intervals = intervals[len(intervals)-1]
title_start = select_from_intervals.find('######')+6
title_end = select_from_intervals.find('\n')+2
title = select_from_intervals[title_start+1:title_end]
title = title.rstrip("\n")
areas_of_interest[title]={}
#cut title
select_from_intervals[select_from_intervals.find(title)+len(title):]
#take url
#print(select_from_intervals)
url_start = select_from_intervals.find("/places/")
url_end = select_from_intervals.find(")")
url = select_from_intervals[url_start:url_end]
urlsaver = url
url = "https://www.atlasobscura.com" + url.replace("\n", '')
areas_of_interest[title]["url"]=url
#collect more information
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
current_page = urlopen(req).read()
current_page= str(current_page, 'utf-8')
current_page = html2text.html2text(current_page)
#print(current_page)
print("============================")
c_slice_interval_a = current_page.find(title)
current_page = current_page[c_slice_interval_a:]
c_slice_interval_a = current_page.find("* * *",3)
c_slice_interval_b = current_page.find("Near This Place")
current_page = current_page[c_slice_interval_a:c_slice_interval_b]
c_slice_interval_a=current_page.find("[See All _ _ ]")
c_slice_interval_b=current_page.find("View on Google Maps")
current_page = current_page[c_slice_interval_a:c_slice_interval_b]
c_slice_interval_a=current_page.find(")")+1
c_slice_interval_b = current_page.find("######")
description = current_page[c_slice_interval_a:c_slice_interval_b]
print(title)
print(" ")
print(current_page)
print("============================")
def track_device(device_index):
cLoc = icloudaccess.getLocation(device_index)
return([cLoc['latitude'], cLoc['longitude']])
def get_device_info(device_index):
return(icloudaccess.get_status(device_index))
def get_devices():
return(icloudaccess.get_devices())
#end locale functions
#home functions
#[B0,B1,B2,...,Bn]
#[on/off, color, brightness, temp]
def wyze_command(command):
if(credLib.returnbykey('wyze', 'email')!="" and credLib.returnbykey('wyze', 'password')!=""):
client = wisewrapper.lights_ww(email=credLib.returnbykey('wyze', 'email'), password=credLib.returnbykey('wyze', 'password'))
for x in range(0, len(command_list)):
if(command[x]=='off'):
client.all_off()
if(command[x]=='on'):
client.all_on()
#end home functions
def testbench():
#returns a list of devices
print(networkscan())
print("##################")
print("##################")
#should return an int
print(getCurrentTemp("MA", "Brookline"))
print("##################")
print("##################")
#should return a string
print(getPrecipitation("MA", "Brookline"))
print("##################")
print("##################")
#should return device list
device_list = get_devices()
print(device_list)
print("##################")
print("##################")
for x in range(0, len(device_list)):
#should return information json
location = track_device(x)
print(location)
#should return lat long
print(get_device_info(x))
print(latlonglookup(location[0], location[1]))
load_map(location[0], location[1], 15)
print("##################")
print("##################")
#testbench()
#print(getHeadlines())
#get_phone_info()
#get_devices()
#print(getHeadlines())
#location = track_phone()
#print(get_incidents(location[0],location[1],0.01549275362))
#get_atlas_obscura(location[0],location[1],1)
#simplisafe jam for band