-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_day.py
executable file
·47 lines (37 loc) · 1.2 KB
/
start_day.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/local/bin/python3
import argparse
from io import BufferedWriter
import os
import time
import urllib.error
import urllib.request
def get_input(year: int, day: int) -> str:
with open('.env') as f:
contents = f.read()
url = f'https://adventofcode.com/{year}/day/{day}/input'
req = urllib.request.Request(url, headers={'Cookie': contents.strip()})
return urllib.request.urlopen(req).read().decode()
def dir_open(path) -> BufferedWriter:
os.makedirs(os.path.dirname(path), exist_ok=True)
return open(path, 'w')
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('year', type=int)
parser.add_argument('day', type=int)
args = parser.parse_args()
for _ in range(5):
try:
print("downloading...")
s = get_input(args.year, args.day)
except urllib.error.URLError as e:
print(f'Failed: not ready yet: {e}')
time.sleep(1)
else:
break
else:
raise SystemExit('Timed out after trying a few times')
with dir_open(f'{args.year}/day{args.day}/input.txt') as f:
f.write(s)
print("all done!")
if __name__ == "__main__":
raise SystemExit(main())