From ab5fd24260b3097a5c6906fd2afd9c7155626c3a Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Tue, 2 Dec 2025 23:01:42 +0100 Subject: [PATCH] Modified Downloader to wait until the time is right before attempting to download today's puzzle. --- src/downloader/Downloader.kt | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/downloader/Downloader.kt b/src/downloader/Downloader.kt index adf46ea..527b1fb 100644 --- a/src/downloader/Downloader.kt +++ b/src/downloader/Downloader.kt @@ -4,15 +4,19 @@ import com.mohamedrejeb.ksoup.html.parser.KsoupHtmlHandler import com.mohamedrejeb.ksoup.html.parser.KsoupHtmlParser import fuel.Fuel import fuel.method +import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import java.io.FileNotFoundException import java.nio.charset.Charset import java.nio.file.Files import java.nio.file.Paths import java.time.LocalDate +import java.time.LocalTime import java.time.Month import java.time.ZoneId import java.util.* +import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds fun main() { var cookie = "" @@ -42,7 +46,7 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri if (!Files.exists(targetDir)) { Files.createDirectories(targetDir) } - val now = LocalDate.now(ZoneId.of("UTC-1")) + val now = LocalDate.now(ZoneId.of("UTC+1")) val maxPuzzles = if (year < 2025) 25 else 12 val lastDay = if (now.isBefore(LocalDate.of(year, Month.DECEMBER, maxPuzzles))) { if (now.isAfter(LocalDate.of(year, Month.NOVEMBER, 30))) { @@ -60,6 +64,20 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri val descriptionFile = targetDir.resolve(DESC_FILENAME.format(day)) val genClassFile = targetDir.resolve(GENCLASS_FILENAME.format(day)) if (!Files.exists(inputFile)) { + if (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour < 5) { + println("Puzzle will not be available within the next hour. Skipping.") + break + } + while (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour == 5) { + println("Waiting until puzzle is available...") + runBlocking { + if (LocalTime.now(ZoneId.of("UTC+1")).minute < 59) { + delay(1.minutes) + } else { + delay(1.seconds) + } + } + } println("Attempting to download input for day $day") val (code, data) = downloadInput(day) if (code != 200) { @@ -157,7 +175,7 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri url = "https://adventofcode.com/$year/day/$day$suffix", method = "GET", headers = mapOf( - "User-Agent" to "git.platon42.de/chrisly42/advent-of-code", + "User-Agent" to "git.platon42.de/chrisly42/advent-of-code (chrisly@platon42.de)", "Cookie" to "session=$sessionCookie" ) )