-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep
executable file
·79 lines (58 loc) · 2.03 KB
/
prep
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
#!/usr/bin/env python3
import os
import requests
import shutil
import time
import yaml
from datetime import datetime
def get(url, cookie, ua):
cookies = {'session': cookie}
headers = {'user-agent': ua}
for i in range(3):
try:
r = requests.get(url, headers=headers, cookies=cookies)
r.raise_for_status()
print('ok', url)
return r.text
except Exception as e:
print(e)
time.sleep(1)
def prep(day, year=None):
if year is None:
year = datetime.now().year
day = int(day)
mydir = os.path.dirname(os.path.realpath(__file__))
codes = os.path.join(mydir, 'code')
boildir = os.path.join(codes, 'boilerplate')
daydir = os.path.join(codes, '{:02}'.format(day))
dayurl = 'http://adventofcode.com/{}/day/{}/input'.format(year, day)
with open(os.path.join(mydir, '.prep'), 'r') as f:
env = yaml.load(f)
session_cookie = env['session_cookie']
user_agent = env.get('user_agent', None)
if os.path.isdir(daydir):
print(daydir)
yn = input('Directory already exists. Overwrite? [N/y] ')
overwrite = yn.strip().lower() in ('y', 'yes')
if not overwrite:
return
shutil.rmtree(daydir)
shutil.copytree(boildir, daydir)
print(daydir)
that_moment = datetime(year, 12, day, 5)
waiting = datetime.utcnow() < that_moment
while datetime.utcnow() < that_moment:
print('.', end='', flush=True)
time.sleep(1)
if waiting:
print()
problem = get(dayurl, cookie=session_cookie, ua=user_agent)
with open(os.path.join(daydir, 'input.txt'), 'w') as f:
f.write(problem)
if __name__ == '__main__':
import argparse
cli = argparse.ArgumentParser(description='Setup directory for day\'s challenge')
cli.add_argument('day', type=int, help='Challenge day number')
cli.add_argument('--year', type=int, default=None, help='Challenge year')
args = cli.parse_args()
prep(year=args.year, day=args.day)