Skip to content

Commit 93466ea

Browse files
Fixed bug where error file would be deleted immediately after creation
Preventing the user from being notified about any errors Temporary diff files are now cleaned up after a successful compare
1 parent 7cea3bf commit 93466ea

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ for entry in config {
7070
if fileManager.fileExists(atPath: latestImage) {
7171
try fileManager.moveItem(atPath: latestImage, toPath: oldImage)
7272
}
73-
73+
7474
/// Takes a screenshot, prepares it and checks for any errors
7575
/// - Returns: Whether the execution succeeded, if the execution failed, this entry should be skipped
7676
func screenshot() throws -> Bool {
@@ -129,59 +129,74 @@ for entry in config {
129129
// MARK: Compare the Screenshots
130130

131131
let tempDiff = "\(entryPath)/diff.temp"
132+
133+
guard let ncc = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) else {
134+
print("Error checking screenshot NCC.")
135+
try handleScreenshotError(entry: entry)
136+
try rollBack(oldImage, to: latestImage)
137+
// Continue with the next entry
138+
continue
139+
}
140+
132141
// If the website changed
133-
if let ncc = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) {
134-
if ncc < nccThreshold {
135-
print("Possible change detected (NCC: \(ncc)). Confirming...")
136-
137-
// Take another screenshot to confirm its not just a one-time loading error or inconsistency
138-
// Delete the changed screenshot, otherwise we cannot confirm that taking the screenshot was a success
139-
try fileManager.removeItem(atPath: latestImage)
142+
if ncc < nccThreshold {
143+
print("Possible change detected (NCC: \(ncc)). Confirming...")
144+
145+
// Take another screenshot to confirm its not just a one-time loading error or inconsistency
146+
// Delete the changed screenshot, otherwise we cannot confirm that taking the screenshot was a success
147+
try fileManager.removeItem(atPath: latestImage)
148+
149+
// Re-do the whole screenshot procedure
150+
guard try screenshot() else {
151+
continue
152+
}
153+
154+
guard let newNCC = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) else {
155+
// Error while confirming screenshot
156+
print("Error confirming screenshot NCC.")
157+
try handleScreenshotError(entry: entry)
158+
try rollBack(oldImage, to: latestImage)
159+
// Continue with the next entry
160+
continue
161+
}
162+
163+
if newNCC < nccThreshold {
164+
// If the second screenshot also shows changes, we notify the user
165+
print("Change confirmed. NCC: \(newNCC). Notifying user.")
140166

141-
// Re-do the whole screenshot procedure
142-
guard try screenshot() else {
143-
continue
167+
// Save the temp file persistently
168+
let diffFile = "\(entryPath)/\(diffFilename)"
169+
if fileManager.fileExists(atPath: diffFile) {
170+
try fileManager.removeItem(atPath: diffFile)
144171
}
172+
try fileManager.moveItem(atPath: tempDiff, toPath: diffFile)
145173

146-
if let newNCC = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff) {
147-
if newNCC < nccThreshold {
148-
// If the second screenshot also shows changes, we notify the user
149-
print("Change confirmed. NCC: \(newNCC). Notifying user.")
150-
151-
// Save the temp file persistently
152-
let diffFile = "\(entryPath)/\(diffFilename)"
153-
if fileManager.fileExists(atPath: diffFile) {
154-
try fileManager.removeItem(atPath: diffFile)
155-
}
156-
try fileManager.moveItem(atPath: tempDiff, toPath: diffFile)
157-
158-
// Generate detailed NCC information
159-
let nccFile = "\(entryPath)/\(nccFilename)"
160-
if fileManager.fileExists(atPath: nccFile) {
161-
try fileManager.removeItem(atPath: nccFile)
162-
}
163-
try bash("compare", arguments: [
164-
"-verbose",
165-
"-alpha", "deactivate",
166-
"-metric", "NCC",
167-
oldImage,
168-
latestImage,
169-
"/dev/null"
170-
], standardOutput: FileHandle(forWritingAtPath: nccFile))
171-
172-
// Notify the user
173-
try sendTelegramMessage("\(entry.name) has changed. NCC: \(ncc)\(ncc != newNCC ? ", \(newNCC)" : "")", to: Int(entry.chatID), image: latestImage)
174-
} else {
175-
print("Change not confirmed. NCC: \(newNCC)")
176-
}
174+
// Generate detailed NCC information
175+
let nccFile = "\(entryPath)/\(nccFilename)"
176+
if fileManager.fileExists(atPath: nccFile) {
177+
try fileManager.removeItem(atPath: nccFile)
177178
}
179+
try bash("compare", arguments: [
180+
"-verbose",
181+
"-alpha", "deactivate",
182+
"-metric", "NCC",
183+
oldImage,
184+
latestImage,
185+
"/dev/null"
186+
], standardOutput: FileHandle(forWritingAtPath: nccFile))
187+
188+
// Notify the user
189+
try sendTelegramMessage("\(entry.name) has changed. NCC: \(ncc)\(ncc != newNCC ? ", \(newNCC)" : "")", to: Int(entry.chatID), image: latestImage)
178190
} else {
179-
print("No change detected. NCC: \(ncc)")
191+
print("Change not confirmed. NCC: \(newNCC)")
180192
}
181193
} else {
182-
print("Error checking screenshot NCC.")
183-
try handleScreenshotError(entry: entry)
184-
try rollBack(oldImage, to: latestImage)
194+
print("No change detected. NCC: \(ncc)")
195+
}
196+
197+
// Clean up any temp diff files
198+
if fileManager.fileExists(atPath: tempDiff) {
199+
try fileManager.removeItem(atPath: tempDiff)
185200
}
186201

187202
// Delete the error file if it exists, since we just successfully captured a screenshot

0 commit comments

Comments
 (0)