From 21812a57564cc903b9dbb6d9baf7dbab148cc878 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 5 Dec 2022 14:36:05 +0100 Subject: [PATCH] added input caching to aoc --- .gitignore | 1 + src/aoc/aoc.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f295d3d..0559ea9 100644 --- a/.gitignore +++ b/.gitignore @@ -159,4 +159,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ +input/ diff --git a/src/aoc/aoc.py b/src/aoc/aoc.py index a239b88..2f80946 100644 --- a/src/aoc/aoc.py +++ b/src/aoc/aoc.py @@ -1,4 +1,5 @@ import os +import shutil import urllib.request from cpl_core.console import Console @@ -21,6 +22,18 @@ def get_input(year: int, day: int) -> str: """ original code from https://github.com/anthonywritescode/aoc2022/blob/main/support/support.py """ - url = f'https://adventofcode.com/{year}/day/{day}/input' - req = urllib.request.Request(url, headers=_get_cookie_headers()) - return urllib.request.urlopen(req).read().decode() + file = f'input/{year}/input_{day}.txt' + if not os.path.exists(file): + os.makedirs(os.path.dirname(file)) + url = f'https://adventofcode.com/{year}/day/{day}/input' + req = urllib.request.Request(url, headers=_get_cookie_headers()) + txt = urllib.request.urlopen(req).read().decode() + with open(file, 'w+') as f: + f.write(txt) + f.close() + + txt = '' + with open(file, 'r') as f: + txt = f.read() + f.close() + return txt