commit ca41e28b5fda2c089d2c62df919ce5bafba23767 parent 6fe4334119537fae8f7202cb1961cf4f27c76647 Author: Eamon Caddigan <eamon.caddigan@gmail.com> Date: Wed, 1 Dec 2021 04:13:51 -0500 Utility function to download puzzle data Diffstat:
A | utils.py | | | 22 | ++++++++++++++++++++++ |
1 file changed, 22 insertions(+), 0 deletions(-)
diff --git a/utils.py b/utils.py @@ -0,0 +1,22 @@ +"""A collection of utilities that should be useful for more than one puzzle.""" + +import os.path +import requests + +def get_puzzle_input(day_number): + """Downloads and returns the input data for the puzzle on a given day. + Note: requires a 'session' cookie string to be stored in + '~/.config/advent_of_code/session' and will raise an error if it's not + found.""" + with open(os.path.expanduser('~/.config/advent_of_code/session'), 'rt') \ + as f_in: + session = f_in.readline().strip() + + response = requests.get(f'https://adventofcode.com/2021/day/{day_number}/input', + cookies = {'session': session}, + headers = {'User-Aent': 'advent_of_code_2021/0.1'}) + + response.raise_for_status() + + return response.text +