-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
128 lines (122 loc) · 6.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Advent of code working directories creator
# IMPORTANT Remember to edit the USER_SESSION_ID & author values with yours
# uses requests module. If not present use pip install requests
# Author = Alexe Simon
# Date = 06/12/2018
# Imports
import os
import sys
try:
import requests
except ImportError:
sys.exit("You need requests module. Install it by running pip install requests.")
# USER SPECIFIC PARAMETERS
# Folders will be created here. If you want to make a parent folder, change this to ex "./adventofcode/"
base_pos = "./"
# Get your session by inspecting the session cookie content in your web browser while connected to
# adventofcode and paste it here as plain text in between the ". Leave at is to not download inputs.
USER_SESSION_ID = "53616c7465645f5fa6afda8d367357cdf84f830ea55e1670c8dd1af94a4abd23f063e3e6941f40a2e7d0e2be919b7e5b"
# Set to false to not download statements.
# Note that only part one is downloaded (since you need to complete it to access part two)
DOWNLOAD_STATEMENTS = False
# Set to false to not download inputs. Note that if the USER_SESSION_ID is wrong or left empty
# inputs will not be downloaded.
DOWNLOAD_INPUTS = True
# Set to false to not make code templates. Note that even if OVERWRITE is set to True, it will never overwrite codes.
MAKE_CODE_TEMPLATE = False
# Set to false to not create a direct url link in the folder.
MAKE_URL = False
# Name automatically put in the code templates.
author = "?"
# If you really need to download the whole thing again, set this to true.
# As the creator said, AoC is fragile; please be gentle. Statements and Inputs do not change.
# This will not overwrite codes.
OVERWRITE = False
# DATE SPECIFIC PARAMETERS
# Date automatically put in the code templates.
date = "December 2018"
# You can go as early as 2015.
starting_advent_of_code_year = 2018
# The setup will download all advent of code data up until that date included
last_advent_of_code_year = 2018
# If the year isn't finished, the setup will download days up until that day included for the last year
last_advent_of_code_day = 25
# Code
MAX_RECONNECT_ATTEMPT = 2
years = range(starting_advent_of_code_year, last_advent_of_code_year+1)
days = range(1, 26)
link = "https://adventofcode.com/" # ex use : https://adventofcode.com/2017/day/19/input
USER_AGENT = "adventofcode_working_directories_creator"
print("Setup will download data and create working directories and files for adventofcode.")
if not os.path.exists(base_pos):
os.mkdir(base_pos)
for y in years:
print("Year "+str(y))
year_pos = base_pos + 'solutions_' + str(y)
if not os.path.exists(year_pos):
os.mkdir(year_pos)
for d in (d for d in days if (y < last_advent_of_code_year or d <= last_advent_of_code_day)):
print(" Day "+str(d));
day_pos = year_pos + f"/day_{d:02d}"
if not os.path.exists(day_pos):
os.mkdir(day_pos)
if MAKE_CODE_TEMPLATE and not os.path.exists(day_pos+"/code.py"):
code = open(day_pos+"/code.py", "w+")
code.write("# Advent of code Year "+str(y)+" Day "+str(d)+" solution\n# Author = "+author+"\n# Date = "+date+"\n\nwith open((__file__.rstrip(\"code.py\")+\"input.txt\"), 'r') as input_file:\n input = input_file.read()\n\n\n\nprint(\"Part One : \"+ str(None))\n\n\n\nprint(\"Part Two : \"+ str(None))")
code.close()
if DOWNLOAD_INPUTS and (not os.path.exists(day_pos+"/input.txt") or OVERWRITE)and USER_SESSION_ID != "":
done = False
error_count = 0
while(not done):
try:
with requests.get(url=link+str(y)+"/day/"+str(d)+"/input", cookies={"session": USER_SESSION_ID}, headers={"User-Agent": USER_AGENT}) as response:
if response.ok:
data = response.text
input = open(day_pos+"/input.txt", "w+")
input.write(data.rstrip("\n"))
input.close()
else:
print(" Server response for input is not valid.")
done = True
except requests.exceptions.RequestException:
error_count += 1
if error_count > MAX_RECONNECT_ATTEMPT:
print(" Giving up.")
done = True
elif error_count == 0:
print(" Error while requesting input from server. Request probably timed out. Trying again.")
else:
print(" Trying again.")
except Exception as e:
print(" Non handled error while requesting input from server. " + str(e))
done = True
if DOWNLOAD_STATEMENTS and (not os.path.exists(day_pos+"/statement.html") or OVERWRITE):
done = False
error_count = 0
while(not done):
try:
with requests.get(url=link+str(y)+"/day/"+str(d), cookies={"session": USER_SESSION_ID}, headers={"User-Agent": USER_AGENT}) as response:
if response.ok:
html = response.text
start = html.find("<article")
end = html.rfind("</article>")+len("</article>")
end_success = html.rfind("</code>")+len("</code>")
statement = open(day_pos+"/statement.html", "w+")
statement.write(html[start:max(end, end_success)])
statement.close()
done = True
except requests.exceptions.RequestException:
error_count += 1
if error_count > MAX_RECONNECT_ATTEMPT:
print(" Error while requesting statement from server. Request probably timed out. Giving up.")
done = True
else:
print(" Error while requesting statement from server. Request probably timed out. Trying again.")
except Exception as e:
print(" Non handled error while requesting statement from server. " + str(e))
done = True
if MAKE_URL and (not os.path.exists(day_pos+"/link.url") or OVERWRITE):
url = open(day_pos+"/link.url", "w+")
url.write("[InternetShortcut]\nURL="+link+str(y)+"/day/"+str(d)+"\n")
url.close()
print("Setup complete : adventofcode working directories and files initialized with success.")