Convert downloadInput gradle task to python script
This commit is contained in:
parent
707cbca3ac
commit
41bceaaf5f
2 changed files with 63 additions and 49 deletions
63
download-input.py
Executable file
63
download-input.py
Executable file
|
@ -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<String>): Int {{
|
||||
return 0
|
||||
}}
|
||||
|
||||
fun part2(input: List<String>): 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))
|
|
@ -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<String>): Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
fun part2(input: List<String>): 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())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue