-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsiteUpdateTracker.py
131 lines (115 loc) · 3.49 KB
/
websiteUpdateTracker.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
#websiteUpdateTracker.py
import os.path
import os
import datetime
import requests
import sys
import time
import datetime
import threading
url1 = "https://www.gnrhealth.com/no-appts-available/"
url2 = "https://www.gnrhealth.com/covid-vaccine-scheduling/"
def lastReadFileExists(lastReadFile):
if os.path.isfile(lastReadFile):
#print ("Last read file exist")
return True
else:
print ("Last read file not exist")
return False
def createLastReadFile(filePath, url):
with open(filePath, "wt") as fileOut:
try:
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers = headers)
except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
print("Caught exception while trying to create last read file")
return
fileOut.write(response.text)
def getTime():
timeFormat = "%H:%M:%S"
return datetime.datetime.now().strftime(timeFormat)
def getTimeForFile():
timeFormat = "%H-%M-%S"
return datetime.datetime.now().strftime(timeFormat)
def getLatest(num, url):
if num == 1:
newLastReadFilePrefix = "websiteUpdateTracker_1_"
url = url1
else:
newLastReadFilePrefix = "websiteUpdateTracker_2_"
url = url2
newLastReadFileSuffix = ".txt"
newLastReadFile = newLastReadFilePrefix + getTimeForFile() + newLastReadFileSuffix
createLastReadFile(newLastReadFile, url)
return newLastReadFile
def areDiff(file1, file2):
with open(file1, "rt") as fileIn1:
dataIn1 = fileIn1.readlines()
with open(file2, "rt") as fileIn2:
dataIn2 = fileIn2.readlines()
if len(dataIn1) != len(dataIn2):
print(file1 + " len = " + str(len(dataIn1)))
print("!=")
print(file2 + " len = " + str(len(dataIn2)))
index = 0
if len(dataIn1) < len(dataIn2):
for lineX in dataIn1:
if dataIn2[index] != lineX:
print("At line " + str(index) + ":")
print(dataIn2[index])
print("!=")
print(lineX)
return True
else:
index += 1
else :
for lineX in dataIn2:
if dataIn1[index] != lineX:
print("At line " + str(index) + ":")
print(dataIn1[index])
print("!=")
print(lineX)
return True
else:
index += 1
return True
return False
def playsound(soundFile):
os.system("afplay " + soundFile)
def deletePrevRun():
path = '.'
files = os.listdir(path)
for f in files:
if "websiteUpdateTracker_1_" in f or "websiteUpdateTracker_2_" in f:
#print("Deleting " + f + "...")
os.remove(f)
#main
def main(args):
lastReadFile1 = "websiteUpdateTracker_1.txt"
lastReadFile2 = "websiteUpdateTracker_2.txt"
alarmFilePath = "codeChallenges/challenge7.mp3"
if not lastReadFileExists(lastReadFile1):
createLastReadFile(lastReadFile1, url1)
if not lastReadFileExists(lastReadFile2):
createLastReadFile(lastReadFile2, url2)
while(True):
#wait 3 minutes
#thread = threading.main_thread()
minutes = 3
print("Waiting for " + str(minutes) + " minutes, starting at " + getTime() + "...")
time.sleep(minutes * 60)
#delete previous runs
deletePrevRun()
#get latest
newLastReadFile1 = getLatest(1, url1)
newLastReadFile2 = getLatest(2, url2)
#compare to last read
if areDiff(lastReadFile1, newLastReadFile1) or areDiff(lastReadFile2, newLastReadFile2):
if areDiff(lastReadFile1, newLastReadFile1):
print(lastReadFile1 + " != " + newLastReadFile1)
else:
print(lastReadFile2 + " != " + newLastReadFile2)
playsound(alarmFilePath)
return
if __name__ == '__main__':
main(sys.argv)