Skip to content

Commit ec50b11

Browse files
Added additional NCC information and diff image to urlwatcher.sh
Added /diff command to show the diff image and extended information in Telegram Added error handling for non-JFBotError errors
1 parent d716f06 commit ec50b11

File tree

5 files changed

+73
-14
lines changed

5 files changed

+73
-14
lines changed

NotifierBot/Sources/Notifier/Commands/Command.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ extension Command {
4848
try self.run(update: update, context: context)
4949
} catch let error as JFBotError {
5050
JFErrorHandler.shared.handle(error, update: update)
51+
} catch let error {
52+
// Other errors
53+
if let chatID = update.message?.chat.id {
54+
_ = try? bot.sendMessage("An unknown error occurred while executing this command. Please check the console for more information.", to: chatID)
55+
}
56+
print("Error: \(error)")
5157
}
5258
})
5359
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// DiffCommand.swift
3+
//
4+
//
5+
// Created by Jonas Frey on 04.05.21.
6+
//
7+
8+
import Foundation
9+
import Telegrammer
10+
11+
struct DiffCommand: Command {
12+
13+
let name = "Diff"
14+
let commands = ["/diff"]
15+
let syntax = "/diff <name>"
16+
let description = "Returns a visual representation of the changed parts of the image."
17+
let permission = BotPermission.mod
18+
19+
func run(update: Update, context: BotContext?) throws {
20+
let chatID = try update.chatID()
21+
let args = try update.args()
22+
guard args.count > 0 else {
23+
try showUsage(chatID)
24+
return
25+
}
26+
let name = args.joined(separator: " ")
27+
// Check if an entry with this name exists
28+
let config = try ConfigParser.getConfig()
29+
guard config.contains(where: { $0.name.lowercased() == name.lowercased() && $0.chatID == chatID }) else {
30+
try bot.sendMessage("There is no entry with the name '\(name)'", to: chatID)
31+
return
32+
}
33+
// Send the diff file and the contents of the ncc file
34+
let nccInfo = try String(contentsOfFile: "\(mainDirectory!)/images/\(name)/\(nccFile)")
35+
JFUtils.sendImage(path: "\(mainDirectory!)/images/\(name)/\(diffFile)", chatID: chatID, text: nccInfo)
36+
}
37+
}

NotifierBot/Sources/Notifier/JFUtils.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ struct JFUtils {
3939
}
4040
}
4141

42-
static func sendFile(path: String, chatID: Int64) {
43-
sendImageOrFile(path: path, chatID: chatID, isFile: true)
42+
static func sendFile(path: String, chatID: Int64, text: String? = nil) {
43+
sendImageOrFile(path: path, chatID: chatID, isFile: true, text: text)
4444
}
4545

46-
static func sendImage(path: String, chatID: Int64) {
47-
sendImageOrFile(path: path, chatID: chatID, isFile: false)
46+
static func sendImage(path: String, chatID: Int64, text: String? = nil) {
47+
sendImageOrFile(path: path, chatID: chatID, isFile: false, text: text)
4848
}
4949

50-
static private func sendImageOrFile(path: String, chatID: Int64, isFile: Bool) {
51-
shell("\(kTelegramScript) -t \(token!) -c \(chatID) -\(isFile ? "f" : "i") \(path)")
50+
static private func sendImageOrFile(path: String, chatID: Int64, isFile: Bool, text: String? = nil) {
51+
var command = "\(kTelegramScript) -t \(token!) -c \(chatID) -\(isFile ? "f" : "i") \(path)"
52+
if let text = text {
53+
command += " \(text)"
54+
}
55+
shell(command)
5256
}
5357

5458
static func entryList(_ entries: [URLEntry], listArea: Bool, listURLs: Bool, listAll: Bool = false) -> String {

NotifierBot/Sources/Notifier/main.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,19 @@ let kPythonPath = "/usr/bin/python3"
3434
// The convert binary from the imagemagick package
3535
let kConvertPath = "/usr/bin/convert"
3636

37-
// Disable Notifications by default
38-
let defaultParameters = ["disable_notification": true, "disable_web_page_preview": true]
37+
// The file containing the detailed NCC information, produced by the urlwatcher.sh script
38+
let nccFile = "ncc"
39+
// The diff image
40+
let diffFile = "diff.png"
3941

4042
/* END BOT SETTINGS */
4143

4244
// An ordered list of all commands to register
4345
// The order defines the order of commands in the /help list
4446
let allCommands: [Command] = [
45-
HelpCommand(), AddCommand(), RemoveCommand(), ListCommand(), ListURLsCommand(), ListAllCommand(), UpdateCommand(),
46-
CheckCommand(), FetchCommand(), FetchURLCommand(), GetPermissionsCommand(), SetPermissionsCommand(), MyIDCommand()
47+
HelpCommand(), ListCommand(), ListURLsCommand(), MyIDCommand(),
48+
AddCommand(), RemoveCommand(), UpdateCommand(), FetchCommand(), FetchURLCommand(), DiffCommand(),
49+
ListAllCommand(), CheckCommand(), GetPermissionsCommand(), SetPermissionsCommand()
4750
]
4851

4952
var token: String!

urlwatcher/urlwatcher.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ SCREENSHOT_SCRIPT="$(pwd)/../tools/screenshot.sh"
1414
TELEGRAM_BOT_TOKEN=$(head -n 1 ../BOT_TOKEN)
1515
# The threshold when to consider two images matching. If two images have a normalized cross correllation >= this value, they are considered identical
1616
NCC_THRESHOLD="0.99"
17+
# The file where the diff image is saved to
18+
DIFF_FILE="diff.png"
19+
# The file where the NCC value is saved to
20+
NCC_FILE="ncc"
1721

1822
# Set the PATH variable for the python script below, so it finds the geckodriver executable when executed from cron
1923
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@@ -31,8 +35,9 @@ fi
3135
function reportChange {
3236
local NAME="$1"
3337
local IMAGE="$2"
38+
local NCC="$3"
3439
"$TELEGRAM_SCRIPT" -t "$TELEGRAM_BOT_TOKEN" -c "$CHAT_ID" \
35-
-i $IMAGE "$NAME has changed"
40+
-i $IMAGE "$NAME has changed. NCC: $NCC"
3641
}
3742

3843
function reportError {
@@ -101,7 +106,7 @@ function screenshotsMatch {
101106
local IMAGE_LATEST="$2"
102107

103108
# Calculate the normalized cross correllation between both images
104-
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST")
109+
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST" "$DIFF_FILE")
105110

106111
if [ "NCC" -lt "$NCC_THRESHOLD" ]; then
107112
# The screenshots are not identical
@@ -126,9 +131,13 @@ function screenshotsMatch {
126131

127132
# Compare the two cropped screenshots again
128133
# Calculate the normalized cross correllation between both images
129-
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST")
134+
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST" "$DIFF_FILE")
130135

131136
if [ "NCC" -lt "$NCC_THRESHOLD" ]; then
137+
# The screenshots do not match. The website has changed
138+
# Write detailed NCC information to a file (and don't overwrite the diff file)
139+
compare -verbose -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST" /dev/null > "$NCC_FILE"
140+
132141
# Return false, as the screenshots do not match
133142
# In bash: 1 == false
134143
return 1
@@ -224,7 +233,7 @@ while IFS='' read -r line || [ -n "${line}" ]; do
224233

225234
# If there was a change
226235
if ! screenshotsMatch "old.png" "latest.png"; then
227-
reportChange "$NAME" "latest.png"
236+
reportChange "$NAME" "latest.png" $(cat "$NCC_FILE")
228237
fi
229238

230239
# After successfully checking for changes (either no change, or change notified)

0 commit comments

Comments
 (0)