From 41bceaaf5fb2d8987165a07699b84c4da9a1bf2b Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 4 Dec 2022 21:30:08 -0700 Subject: [PATCH] Convert downloadInput gradle task to python script --- download-input.py | 63 +++++++++++++++++++++++++++++++++++++++++ kotlin/build.gradle.kts | 49 -------------------------------- 2 files changed, 63 insertions(+), 49 deletions(-) create mode 100755 download-input.py diff --git a/download-input.py b/download-input.py new file mode 100755 index 0000000..e81c74e --- /dev/null +++ b/download-input.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 + +import argparse +import os +from os.path import exists +import shutil +from urllib.request import Request, urlopen +import webbrowser + + +def write_kotlin(day): + with open(src_file, 'w') as src: + src.write("""fun main() {{ + fun part1(input: List): Int {{ + return 0 + }} + + fun part2(input: List): Int {{ + return 0 + }} + + val testInput = readInput("Day{day}_test") + check(part1(testInput) == 0) + check(part2(testInput) == 0) + + val input = readInput("Day{day}") + println(part1(input)) + println(part2(input)) +}}""".format(day=day)) + + +session_cookie = os.getenv('AOC_SESSION') +if not session_cookie: + print('AOC_SESSION environment variable not set, aborting') + exit(1) + +if not session_cookie.startswith('session='): + session_cookie = 'session=' + session_cookie + +parser = argparse.ArgumentParser() +parser.add_argument('-d', '--day', type=int) +args = parser.parse_args() + +challenge_url = "https://adventofcode.com/2022/day/{}".format(args.day) +input_url = "{}/input".format(challenge_url) + +webbrowser.open(challenge_url) + +day_file_base = 'Day{}'.format(str(args.day).zfill(2)) +day_file_test = 'kotlin/src/{}_test.txt'.format(day_file_base) + +with open(day_file_test, 'w'): + pass + +request = Request(input_url, headers={'Cookie': session_cookie}) +with urlopen(request) as response: + with open('kotlin/src/{}.txt'.format(day_file_base), 'wb') as input_file: + shutil.copyfileobj(response, input_file) + +src_file = 'kotlin/src/{}.kt'.format(day_file_base) +if exists(src_file): + exit(0) +write_kotlin(str(args.day).zfill(2)) diff --git a/kotlin/build.gradle.kts b/kotlin/build.gradle.kts index 94a039c..c664ade 100644 --- a/kotlin/build.gradle.kts +++ b/kotlin/build.gradle.kts @@ -20,52 +20,3 @@ tasks { gradleVersion = "7.6" } } - -tasks.register("downloadInput") { - doLast { - val day: String = findProperty("day") as? String ?: error("day is required") - val challengeUrl = "https://adventofcode.com/2022/day/$day" - val inputUrl = "$challengeUrl/input" - exec { - commandLine("xdg-open", challengeUrl) - } - var sessionCookie = System.getenv("AOC_SESSION") - if (sessionCookie.isNullOrBlank()) { - error("AOC_SESSION environment variable missing or empty") - } - if (!sessionCookie.startsWith("session=")) { - sessionCookie = "session=$sessionCookie" - } - val url = URL(inputUrl) - val connection = url.openConnection() as HttpURLConnection - connection.requestMethod = "GET" - connection.setRequestProperty("Cookie", sessionCookie) - val content = connection.inputStream.bufferedReader().readText() - val dayFileName = "Day${day.padStart(2, '0')}" - File("src", "${dayFileName}_test.txt").createNewFile() - File("src", "$dayFileName.txt").writeText(content) - val sourceFile = File("src", "$dayFileName.kt") - if (sourceFile.exists()) { - return@doLast - } - sourceFile.writeText(""" - fun main() { - fun part1(input: List): Int { - return 0 - } - - fun part2(input: List): Int { - return 0 - } - - val testInput = readInput("${dayFileName}_test") - check(part1(testInput) == 0) - check(part2(testInput) == 0) - - val input = readInput("$dayFileName") - println(part1(input)) - println(part2(input)) - } - """.trimIndent()) - } -} \ No newline at end of file