Skip to content

Commit 302d9e8

Browse files
Updated urlwatcher and screenshot scripts
1 parent 627f157 commit 302d9e8

File tree

3 files changed

+165
-69
lines changed

3 files changed

+165
-69
lines changed

tools/screenshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# options.add_argument( "--screenshot test.jpg http://google.com/" )
1717
driver = webdriver.Firefox( firefox_options=options )
1818
driver.get(url)
19-
time.sleep(1)
19+
time.sleep(2)
2020
el = driver.find_element_by_tag_name('body')
2121
el.screenshot(file)
2222
#driver.save_screenshot(file)

tools/screenshot.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
/usr/bin/google-chrome --headless --disable-gpu --window-size=1920,3000 --screenshot=$1 $2 --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36"

urlwatcher/urlwatcher.sh

Lines changed: 162 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ IMAGES_DIRECTORY="images"
88
# We are in NotifierBot/urlwatcher
99
TELEGRAM_SCRIPT="$(pwd)/../tools/telegram.sh"
1010
# Screenshot script.
11-
SCREENSHOT_SCRIPT="$(pwd)/../tools/screenshot.py"
11+
#SCREENSHOT_SCRIPT="$(pwd)/../tools/screenshot.py" # When reactivating, change calls to "python3.6 "$SCREENSHOT_SCRIPT""
12+
SCREENSHOT_SCRIPT="$(pwd)/../tools/screenshot.sh"
1213
# Read the first line from the BOT_TOKEN file
1314
TELEGRAM_BOT_TOKEN=$(head -n 1 ../BOT_TOKEN)
1415

@@ -25,6 +26,116 @@ if [ ! -d "$IMAGES_DIRECTORY" ]; then
2526
mkdir "$IMAGES_DIRECTORY"
2627
fi
2728

29+
function reportChange {
30+
local NAME="$1"
31+
local IMAGE="$2"
32+
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
33+
-i $IMAGE "$NAME has changed"
34+
}
35+
36+
function reportError {
37+
local NAME="$1"
38+
39+
echo "Error taking screenshot. Notifying user..."
40+
if [ ! -f errored ]; then
41+
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
42+
"Error creating a screenshot for '$NAME'"
43+
touch errored
44+
fi
45+
}
46+
47+
function reportErroredResume {
48+
local NAME="$1"
49+
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
50+
"Screenshot creation resumed for '$NAME'"
51+
}
52+
53+
function reportNew {
54+
local NAME="$1"
55+
local IMAGE="$2"
56+
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" -N \
57+
-f "$IMAGE" "Added '$NAME'"
58+
}
59+
60+
function takeScreenshot {
61+
local URL="$1"
62+
"$SCREENSHOT_SCRIPT" latest.png "$URL"
63+
64+
# On Error, retry once
65+
if [ ! -f latest.png ]; then
66+
echo "Error taking screenshot. Retrying..."
67+
sleep 0.2
68+
"$SCREENSHOT_SCRIPT" latest.png "$URL"
69+
fi
70+
}
71+
72+
function rollBack {
73+
# Roll back the old screenshot
74+
if [ -f old.png ]; then
75+
mv old.png latest.png
76+
fi
77+
}
78+
79+
function rollBackAndReportError {
80+
# Roll back the old screenshot
81+
rollBack
82+
83+
# NOTIFY ERROR, if not already done
84+
reportError "$NAME"
85+
}
86+
87+
function cropScreenshot {
88+
local IMAGE="$1"
89+
90+
# Crop the screenshot
91+
if [ "$WIDTH" != "0" ] && [ "$HEIGHT" != "0" ]; then
92+
# Neither width nor height is zero, crop the image
93+
convert "$IMAGE" -crop "${WIDTH}x${HEIGHT}+$X+$Y" "$IMAGE"
94+
fi
95+
}
96+
97+
function screenshotsMatch {
98+
local IMAGE_OLD="$1"
99+
local IMAGE_LATEST="$2"
100+
101+
HASH_OLD=$(identify -quiet -format "%#" "$IMAGE_OLD")
102+
HASH_LATEST=$(identify -quiet -format "%#" "$IMAGE_LATEST")
103+
104+
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
105+
# The screenshots are not identical!
106+
echo "Possible change detected. Confirming..."
107+
108+
# Take another screenshot to confirm it's not just a one-time loading error
109+
# We first have to delete the changed screenshot (otherwise we cannot confirm that taking the second screenshot was a success)
110+
rm -f latest.png
111+
sleep 1
112+
takeScreenshot "$URL" latest.png
113+
# If no luck taking the second screenshot, abort
114+
if [ ! -f latest.png ]; then
115+
# Roll back the screenshot and report an error
116+
rollBackAndReportError
117+
118+
# Skip this entry
119+
cd ../..
120+
continue
121+
fi
122+
123+
cropScreenshot "latest.png"
124+
125+
# Compare the two cropped screenshots
126+
HASH_OLD=$(identify -quiet -format "%#" old.png)
127+
HASH_LATEST=$(identify -quiet -format "%#" latest.png)
128+
129+
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
130+
# Return false, as the screenshots do not match
131+
return 1
132+
fi
133+
fi
134+
135+
# If statement above didn't exit, that means we have no mismatching hashes and therefore the screenshots match (return true)
136+
return 0
137+
}
138+
28139
# Iterate over the lines of the urls.list
29140
while IFS='' read -r line || [ -n "${line}" ]; do
30141
# Load the configuration
@@ -38,105 +149,88 @@ while IFS='' read -r line || [ -n "${line}" ]; do
38149

39150
echo "Checking $URL"
40151

152+
# Create the image directory, if it does not exist yet
41153
if [ ! -d "$IMAGES_DIRECTORY/$NAME" ]; then
42154
mkdir "$IMAGES_DIRECTORY/$NAME"
43155
fi
156+
157+
# Go into it
44158
cd "images/$NAME"
45-
# Move the old file, if it exists
159+
160+
# Rename the old screenshot file, if it exists
46161
if [ -f latest.png ]; then
47162
mv latest.png old.png
48163
fi
49164

50-
# Take the screenshot
51-
python3 "$SCREENSHOT_SCRIPT" latest.png "$URL"
165+
#######################
166+
# TAKE THE SCREENSHOT #
167+
#######################
52168

53-
# On Error, retry
54-
if [ ! -f latest.png ]; then
55-
echo "Error taking screenshot. Retrying..."
56-
python3 "$SCREENSHOT_SCRIPT" latest.png "$URL"
57-
fi
169+
# Take the screenshot
170+
takeScreenshot "$URL"
58171

59-
# If still no luck taking the screenshot, abort
172+
# If no luck taking the screenshot, abort
60173
if [ ! -f latest.png ]; then
61-
echo "Error taking screenshot. Notifying user..."
174+
rollBackAndReportError
62175

63-
# Roll back the old screenshot
64-
if [ -f old.png ]; then
65-
mv old.png latest.png
66-
fi
67-
# NOTIFY ERROR
68-
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
69-
"Error creating a screenshot for '$NAME'"
70176
# Skip this entry
71177
cd ../..
72178
continue
73179
fi
74180

75-
# Crop the new screenshot
76-
if [ "$WIDTH" != "0" ] && [ "$HEIGHT" != "0" ]; then
77-
# Neither width nor height is zero, crop the image
78-
convert latest.png -crop "${WIDTH}x${HEIGHT}+$X+$Y" latest.png
79-
fi
181+
# We now have a valid file latest.png and possibly old.png
182+
183+
##########################
184+
# PREPARE THE SCREENSHOT #
185+
##########################
186+
187+
cropScreenshot "latest.png"
80188

81189
# If no old screenshot exists, there is no need to compare anything
82190
if [ ! -f old.png ]; then
83191
# Send without notification
84-
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" -N \
85-
-f latest.png "Added '$NAME'"
192+
reportNew "$NAME" "latest.png"
193+
194+
# Clear errored file if it exists (this is a new screenshot instance)
195+
if [ -f errored ]; then
196+
# Notify no error anymore
197+
reportErroredResume "$NAME"
198+
rm -f errored
199+
fi
200+
201+
# No need to compare, we are done.
86202
cd ../..
87203
continue
88204
fi
89205

90-
# Compare the two cropped screenshots
91-
HASH_OLD=$(identify -quiet -format "%#" old.png)
92-
HASH_LATEST=$(identify -quiet -format "%#" latest.png)
93-
94-
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
95-
# The screenshots are not identical!
96-
echo "Possible change detected. Confirming..."
97-
# Take another one to confirm it's not just a one-time loading error
98-
python3 "$SCREENSHOT_SCRIPT" latest.png "$URL"
206+
# We now have a valid latest.png and old.png screenshot file
99207

100-
# On Error, retry
101-
if [ ! -f latest.png ]; then
102-
echo "Error taking screenshot. Retrying..."
103-
python3 "$SCREENSHOT_SCRIPT" latest.png "$URL"
104-
fi
208+
###########################
209+
# COMPARE THE SCREENSHOTS #
210+
###########################
105211

106-
# If still no luck taking the second screenshot, abort
107-
if [ ! -f latest.png ]; then
108-
echo "Error taking screenshot. Notifying user..."
109-
110-
# Roll back the old screenshot
111-
if [ -f old.png ]; then
112-
mv old.png latest.png
113-
fi
114-
# NOTIFY ERROR
115-
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
116-
"Error creating a screenshot for '$NAME'"
117-
# Skip this entry
118-
cd ../..
119-
continue
120-
fi
212+
# If the new screenshot is all black (some display error), ignore it
213+
mean=$(convert latest.png -format "%[mean]" info:)
214+
if [ "$mean" == "0" ]; then
215+
rollBack
216+
# Skip this entry
217+
cd ../..
218+
continue
219+
fi
121220

122-
# Crop the new screenshot
123-
if [ "$WIDTH" != "0" ] && [ "$HEIGHT" != "0" ]; then
124-
# Neither width nor height is zero, crop the image
125-
convert latest.png -crop "${WIDTH}x${HEIGHT}+$X+$Y" latest.png
126-
fi
221+
# If there was a change
222+
if ! screenshotsMatch "old.png" "latest.png"; then
223+
reportChange "$NAME" "latest.png"
224+
fi
127225

128-
# Compare the two cropped screenshots
129-
HASH_OLD=$(identify -quiet -format "%#" old.png)
130-
HASH_LATEST=$(identify -quiet -format "%#" latest.png)
131-
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
132-
# NOTIFY
133-
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
134-
-i latest.png "$NAME has changed"
135-
fi
226+
# After successfully checking for changes (either no change, or change notified)
227+
if [ -f errored ]; then
228+
# Notify no error anymore
229+
reportErroredResume "$NAME"
230+
rm -f errored
136231
fi
137232

138233
cd ../..
139234
done < "$URL_LIST_FILE"
140235

141236
echo "All checks completed."
142-
killall firefox

0 commit comments

Comments
 (0)