-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
66 lines (54 loc) · 2.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Roughly inspired by https://github.com/AlexeSimon/adventofcode/blob/master/init.py
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader
import os
import json
import requests
USER_AGENT = "Python AdventOfCode Generator (Dev)"
env = Environment(
loader = FileSystemLoader(os.path.abspath('templates')),
autoescape = select_autoescape(['py']),
)
with open(os.path.join(os.path.dirname(__file__), "config.json"), 'r') as config_file:
config = json.load(config_file)
for year in range(config['years']['start'], config['years']['end']+1):
year_dir = str(year)
if not os.path.exists(os.path.join(year_dir)):
os.mkdir(os.path.join(year_dir))
for day in range(1,26):
if year > config['latest']['year']:
continue
if year == config['latest']['year'] and day > config['latest']['day']:
continue
day_dir = "{:02d}".format(day)
print("{}/{}".format(year_dir, day_dir))
# Skip if folder already exists
# if os.path.exists(os.path.join(year_dir, day_dir)):
# continue
# Get Inputdata
if not os.path.exists(os.path.join(year_dir, day_dir, "input.txt")):
print(" Downloading Inputdata")
req_input = requests.get(
"https://adventofcode.com/{}/day/{}/input".format(year, day),
cookies={"session": config['user_session_id']},
# headers={"User-Agent": USER_AGENT},
)
if req_input.ok:
# print(req_input.content.decode('utf-8'))
os.mkdir(os.path.join(year_dir, day_dir))
with open(os.path.join(year_dir, day_dir, "input.txt"), 'w') as outfile:
outfile.write(req_input.content.decode('utf-8'))
print(" Success")
else:
print(" DAY {}/{}: Not available yet (Errorcode: {})".format(year_dir, day_dir, req_input.status_code))
continue
# Create code.py file
if not os.path.exists(os.path.join(year_dir, day_dir, "code.py")):
print(" Creating Template")
template = env.get_template("code.py")
with open(os.path.join(year_dir, day_dir, "code.py"), 'w') as outfile:
outfile.write(template.render(
YEAR=year_dir,
DAY=day_dir,
AUTHOR=config['author'],
))
print(" Success")