Skip to content

Commit d312e35

Browse files
Fixed DummyFormatter
Reduced website timeout to 30 seconds Fixed error message on missing bot token
1 parent 32b59c2 commit d312e35

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

Shared/Sources/Shared/SharedUtils.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ public struct SharedUtils {
2424
var rest = ti
2525
let years = rest / (365 * 24 * 60 * 60)
2626
rest = rest.truncatingRemainder(dividingBy: (365 * 24 * 60 * 60))
27-
if years > 0 {
27+
if years >= 1 {
2828
components.append(durationString(Int(years), .year))
2929
}
3030

3131
let days = rest / (24 * 60 * 60)
3232
rest = rest.truncatingRemainder(dividingBy: (24 * 60 * 60))
33-
if days > 0 {
33+
if days >= 1 {
3434
components.append(durationString(Int(days), .day))
3535
}
3636

3737
let hours = rest / (60 * 60)
3838
rest = rest.truncatingRemainder(dividingBy: (60 * 60))
39-
if hours > 0 {
39+
if hours >= 1 {
4040
components.append(durationString(Int(hours), .hour))
4141
}
4242

4343
let minutes = rest / 60
4444
rest = rest.truncatingRemainder(dividingBy: 60)
45-
if minutes > 0 {
45+
if minutes >= 1 {
4646
components.append(durationString(Int(minutes), .minute))
4747
}
4848

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
value = "true"
5858
isEnabled = "YES">
5959
</EnvironmentVariable>
60+
<EnvironmentVariable
61+
key = "PATH"
62+
value = "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
63+
isEnabled = "YES">
64+
</EnvironmentVariable>
6065
</EnvironmentVariables>
6166
</LaunchAction>
6267
<ProfileAction

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func takeScreenshot(ofURL url: String, outputPath: String, delay: Int? = nil, ca
1616
"--output=\"\(outputPath)\"",
1717
"--overwrite",
1818
"--full-page",
19+
"--timeout=30", // decrease timeout to prevent waiting for elements that are not there anymore
1920
"--user-agent=\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36\""
2021
]
2122
if let delay = delay, delay > 0 {
@@ -162,11 +163,14 @@ func handleScreenshotError(entry: URLEntry) throws {
162163
}
163164

164165
// If an error file already exists, get the attributes and check the creation date
166+
print("Error file already exists")
165167
if fileManager.fileExists(atPath: errorFile),
166-
let creationDate = try fileManager.attributesOfItem(atPath: errorFile)[.creationDate] as? Date,
167-
creationDate.distance(to: Date()) >= kErrorReportDuration {
168-
// Notify the user that an error persisted for the last `errorReportTime` seconds
169-
try notifyError(entry: entry)
168+
let creationDate = try fileManager.attributesOfItem(atPath: errorFile)[.creationDate] as? Date {
169+
print("Creation date: \(creationDate) (\(creationDate.distance(to: Date()) / 3600) hours ago")
170+
if creationDate.distance(to: Date()) >= kErrorReportDuration {
171+
// Notify the user that an error persisted for the last `errorReportTime` seconds
172+
try notifyError(entry: entry)
173+
}
170174
}
171175
}
172176

@@ -179,7 +183,8 @@ func notifyError(entry: URLEntry) throws {
179183
// TODO: Replace with DateComponentsFormatter when available on Linux
180184
let f = SharedUtils.DummyFormatter(fullUnits: true)
181185
let durationString = f.string(from: kErrorReportDuration) ?? "some time"
182-
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for at least \(durationString).",
186+
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for at least " +
187+
"\(durationString.isEmpty ? "some time" : durationString).",
183188
to: Int(entry.chatID))
184189
}
185190

urlwatcher/Sources/urlwatcher/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if let t = try? String(contentsOfFile: "\(mainDirectory)/BOT_TOKEN", encoding: .
2121
telegramBotToken = t
2222
} else {
2323
print("Unable to read bot token. Please place it into the file " +
24-
"\(mainDirectory.components(separatedBy: "/").dropLast().joined(separator: "/"))/BOT_TOKEN " +
24+
"\(mainDirectory)/BOT_TOKEN " +
2525
"or provide the environment variable TELEGRAM_BOT_TOKEN")
2626
exit(1)
2727
}

0 commit comments

Comments
 (0)