-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
42 lines (37 loc) · 1.48 KB
/
init.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
# downloads the input of the current day (as input.txt) and making sure that ./<year>/<day>/ folder exists.
import datetime
import os.path
import sys
from urllib import request
from urllib.error import HTTPError
from bs4 import BeautifulSoup
if len(sys.argv) > 1:
now = datetime.datetime.strptime(f'{sys.argv[1]}-12-{sys.argv[2]}', '%Y-%m-%d')
else:
now = datetime.datetime.now()
year, day = now.year, now.day
with open('./cookie') as f:
cookie = f.readline()
if not os.path.exists(f'./{year}/day {day:02d}'):
os.makedirs(f'./{year}/day {day:02d}')
if os.path.exists(f'./{year}/day {day:02d}/input.txt'):
print(f'{year}/{day} Input exists. Skipping.')
else:
req = request.Request(f'https://adventofcode.com/{year}/day/{day}/input', headers={'cookie': cookie})
try:
with request.urlopen(req) as f:
with open(f'./{year}/day {day:02d}/input.txt', 'w') as inp:
inp.write(f.read().decode('utf-8'))
print('downloaded and saved.')
except HTTPError as e:
print(f'HTTP Error: {e.code} {e.reason}')
exit(1)
req = request.Request(f'https://adventofcode.com/{year}/day/{day}', headers={'cookie': cookie})
with request.urlopen(req) as f:
h = f.read().decode('utf-8')
soup = BeautifulSoup(h, 'html.parser')
for i, pre in enumerate(soup.find_all('pre')):
fname = f'./{year}/day {day:02d}/code{i:02d}.txt'
print(f'writing {fname}...')
with open(fname, 'a') as c:
c.write(pre.text)