Skip to content

Commit 39cf21a

Browse files
Fixed bug that would only cause the first URL to be checked
Removed debug working directory again since it does not work
1 parent 88a787a commit 39cf21a

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ let silentMode: Bool = false
66

77
let fileManager = FileManager.default
88
/// The directory, the executable is in
9-
#if DEBUG
10-
let currentPath = fileManager.currentDirectoryPath
11-
#else
129
let currentPath = Bundle.main.bundlePath
13-
#endif
1410
/// The telegram bot token is read from a file
1511
let telegramBotToken = try String(contentsOfFile: "\(currentPath)/../BOT_TOKEN", encoding: .utf8)
1612
/// The script used to send the telegram messages
@@ -130,53 +126,53 @@ for entry in config {
130126

131127
let tempDiff = "\(entryPath)/diff.temp"
132128
// If the website changed
133-
if let ncc = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) {
134-
guard ncc < nccThreshold else {
135-
print("No change detected. NCC: \(ncc)")
136-
break // Break the if
137-
}
138-
print("Possible change detected (NCC: \(ncc)). Confirming...")
139-
140-
// Take another screenshot to confirm its not just a one-time loading error or inconsistency
141-
// Delete the changed screenshot, otherwise we cannot confirm that taking the screenshot was a success
142-
try fileManager.removeItem(atPath: latestImage)
143-
144-
// Re-do the whole screenshot procedure
145-
guard try screenshot() else {
146-
continue
147-
}
148-
149-
if let newNCC = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) {
150-
guard newNCC < nccThreshold else {
151-
print("Change not confirmed. NCC: \(newNCC)")
152-
break // Break the inner if
153-
}
154-
// If the second screenshot also shows changes, we notify the user
155-
print("Change confirmed. NCC: \(newNCC). Notifying user.")
129+
if let ncc = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff), ncc < nccThreshold {
130+
if ncc < nccThreshold {
131+
print("Possible change detected (NCC: \(ncc)). Confirming...")
156132

157-
// Save the temp file persistently
158-
let diffFile = "\(entryPath)/\(diffFilename)"
159-
if fileManager.fileExists(atPath: diffFile) {
160-
try fileManager.removeItem(atPath: diffFile)
161-
}
162-
try fileManager.moveItem(atPath: tempDiff, toPath: diffFile)
133+
// Take another screenshot to confirm its not just a one-time loading error or inconsistency
134+
// Delete the changed screenshot, otherwise we cannot confirm that taking the screenshot was a success
135+
try fileManager.removeItem(atPath: latestImage)
163136

164-
// Generate detailed NCC information
165-
let nccFile = "\(entryPath)/\(nccFilename)"
166-
if fileManager.fileExists(atPath: nccFile) {
167-
try fileManager.removeItem(atPath: nccFile)
137+
// Re-do the whole screenshot procedure
138+
guard try screenshot() else {
139+
continue
168140
}
169-
try bash("compare", arguments: [
170-
"-verbose",
171-
"-alpha", "deactivate",
172-
"-metric", "NCC",
173-
oldImage,
174-
latestImage,
175-
"/dev/null"
176-
], standardOutput: FileHandle(forWritingAtPath: nccFile))
177141

178-
// Notify the user
179-
try sendTelegramMessage("\(entry.name) has changed. NCC: \(ncc)\(ncc != newNCC ? ", \(newNCC)" : "")", to: Int(entry.chatID), image: latestImage)
142+
if let newNCC = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) {
143+
if newNCC < nccThreshold {
144+
// If the second screenshot also shows changes, we notify the user
145+
print("Change confirmed. NCC: \(newNCC). Notifying user.")
146+
147+
// Save the temp file persistently
148+
let diffFile = "\(entryPath)/\(diffFilename)"
149+
if fileManager.fileExists(atPath: diffFile) {
150+
try fileManager.removeItem(atPath: diffFile)
151+
}
152+
try fileManager.moveItem(atPath: tempDiff, toPath: diffFile)
153+
154+
// Generate detailed NCC information
155+
let nccFile = "\(entryPath)/\(nccFilename)"
156+
if fileManager.fileExists(atPath: nccFile) {
157+
try fileManager.removeItem(atPath: nccFile)
158+
}
159+
try bash("compare", arguments: [
160+
"-verbose",
161+
"-alpha", "deactivate",
162+
"-metric", "NCC",
163+
oldImage,
164+
latestImage,
165+
"/dev/null"
166+
], standardOutput: FileHandle(forWritingAtPath: nccFile))
167+
168+
// Notify the user
169+
try sendTelegramMessage("\(entry.name) has changed. NCC: \(ncc)\(ncc != newNCC ? ", \(newNCC)" : "")", to: Int(entry.chatID), image: latestImage)
170+
} else {
171+
print("Change not confirmed. NCC: \(newNCC)")
172+
}
173+
}
174+
} else {
175+
print("No change detected. NCC: \(ncc)")
180176
}
181177
} else {
182178
print("Error checking screenshot NCC.")

0 commit comments

Comments
 (0)