Skip to content

Commit 88a787a

Browse files
Fixed wrongly interpreted return value of compare command
Fixed working directory for debugging
1 parent f4ea587 commit 88a787a

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

urlwatcher/.swiftpm/xcode/xcshareddata/xcschemes/urlwatcher.xcscheme

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
ReferencedContainer = "container:">
5252
</BuildableReference>
5353
</BuildableProductRunnable>
54+
<EnvironmentVariables>
55+
<EnvironmentVariable
56+
key = "PATH"
57+
value = "/Users/jonasfrey/tools:/usr/local/opt/gnu-getopt/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/local/bin:/Users/jonasfrey/scripts:/Users/jonasfrey/flutter/bin"
58+
isEnabled = "YES">
59+
</EnvironmentVariable>
60+
</EnvironmentVariables>
5461
</LaunchAction>
5562
<ProfileAction
5663
buildConfiguration = "Release"

urlwatcher/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ let package = Package(
1717
// Targets can depend on other targets in this package, and on products in packages this package depends on.
1818
.target(
1919
name: "urlwatcher",
20-
dependencies: []),
20+
dependencies: [], swiftSettings: [.define("DEBUG")]),
2121
]
2222
)

urlwatcher/Sources/urlwatcher/ConfigParser.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class ConfigParser {
2727
let config = (try? String(contentsOfFile: urlListFile)) ?? ""
2828
var entries: [URLEntry] = []
2929
for line in config.components(separatedBy: .newlines) {
30-
if line.isEmpty {
30+
if line.trimmingCharacters(in: .whitespaces).isEmpty
31+
|| line.trimmingCharacters(in: .whitespaces).starts(with: "#") {
3132
continue
3233
}
3334
let components = line.components(separatedBy: ",")

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func sendTelegramMessage(_ message: String, to chatID: Int, image: String? = nil
8484
}
8585

8686
func handleScreenshotError(entry: URLEntry) throws {
87+
print("Handling error.")
8788
let errorFile = "\(directory(for: entry))/error"
8889
// If the errorReportMinutes are not set, we immediately notify the user
8990
guard errorReportMinutes > 0 else {
@@ -122,6 +123,9 @@ func notifyNew(entry: URLEntry, file: String) throws {
122123

123124
func rollBack(_ oldImage: String, to latestImage: String) throws {
124125
if fileManager.fileExists(atPath: oldImage) {
126+
if fileManager.fileExists(atPath: latestImage) {
127+
try fileManager.removeItem(atPath: latestImage)
128+
}
125129
try fileManager.moveItem(atPath: oldImage, toPath: latestImage)
126130
}
127131
}
@@ -138,7 +142,9 @@ func screenshotNCC(_ oldImage: String, _ latestImage: String, diffFile: String)
138142
diffFile
139143
], standardOutput: nccPipe, standardError: nccPipe)
140144

141-
guard result == .success else {
145+
// The compare command returns 0, if the images are similar, 1 if they are dissimilar and something else on an error
146+
if case .failure(let code) = result,
147+
code != 1 && code != 0 {
142148
return nil
143149
}
144150

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ 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
912
let currentPath = Bundle.main.bundlePath
10-
/// The telegram bot token it read from a file
13+
#endif
14+
/// The telegram bot token is read from a file
1115
let telegramBotToken = try String(contentsOfFile: "\(currentPath)/../BOT_TOKEN", encoding: .utf8)
1216
/// The script used to send the telegram messages
1317
let telegramScript = "\(currentPath)/../tools/telegram.sh"
@@ -126,9 +130,12 @@ for entry in config {
126130

127131
let tempDiff = "\(entryPath)/diff.temp"
128132
// If the website changed
129-
if let ncc = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff),
130-
ncc < nccThreshold {
131-
print("Possible change detected. Confirming...")
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...")
132139

133140
// Take another screenshot to confirm its not just a one-time loading error or inconsistency
134141
// Delete the changed screenshot, otherwise we cannot confirm that taking the screenshot was a success
@@ -139,9 +146,13 @@ for entry in config {
139146
continue
140147
}
141148

142-
if let newNCC = try screenshotNCC(oldImage, latestImage, diffFile: tempDiff),
143-
newNCC < nccThreshold {
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+
}
144154
// If the second screenshot also shows changes, we notify the user
155+
print("Change confirmed. NCC: \(newNCC). Notifying user.")
145156

146157
// Save the temp file persistently
147158
let diffFile = "\(entryPath)/\(diffFilename)"
@@ -167,6 +178,10 @@ for entry in config {
167178
// Notify the user
168179
try sendTelegramMessage("\(entry.name) has changed. NCC: \(ncc)\(ncc != newNCC ? ", \(newNCC)" : "")", to: Int(entry.chatID), image: latestImage)
169180
}
181+
} else {
182+
print("Error checking screenshot NCC.")
183+
try handleScreenshotError(entry: entry)
184+
try rollBack(oldImage, to: latestImage)
170185
}
171186

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

0 commit comments

Comments
 (0)