Modified Downloader to wait until the time is right before attempting to download today's puzzle.

This commit is contained in:
Chris Hodges 2025-12-02 23:01:42 +01:00
parent f399fcdea7
commit ab5fd24260

View File

@ -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 = "<insert your session cookie here or store in gradle.properties>"
@ -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"
)
)