-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-inputs
executable file
·100 lines (91 loc) · 3.58 KB
/
get-inputs
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
#!/usr/bin/env python3
import argparse
from datetime import datetime, timedelta, timezone
from pathlib import Path
import shutil
import sys
import time
from urllib.error import HTTPError
from urllib.request import urlopen, Request
tz = timezone(timedelta(hours=-5))
def main():
terminal_output = sys.stdout if sys.stdout.isatty(
) else sys.stderr if sys.stderr.isatty() else None
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-n", "--dry-run", action='store_true')
group.add_argument("-o", "--overwrite", action='store_true', dest='force')
group = parser.add_mutually_exclusive_group()
group.add_argument("-s", "--session")
group.add_argument("-S",
"--session-file",
metavar='FILE',
type=argparse.FileType())
parser.add_argument("-y", "--year", type=int, default=2022)
parser.add_argument("days", metavar='DAY', type=int, nargs='*')
args = parser.parse_args()
dry_run, force, session = args.dry_run, args.force, args.session
if not session:
session_file = args.session_file
if not session_file:
session_file = open(Path.home() / '.aocrc')
with session_file:
session = session_file.read().strip()
year, days = args.year, args.days
base = datetime(year, 12, 1, 0, 0, 0, 0, tz)
if days:
days = sorted(set(days))
else:
days = (datetime.now(tz) - base).days + 1
days = range(1, max(0, min(25, days)) + 1)
for day in days:
file = f"day{day}.txt"
if not force and Path(file).exists():
print(f"{file} already exists")
continue
if not dry_run:
target = base + timedelta(days=day - 1)
while True:
now = datetime.now(tz)
if now >= target:
break
delta = target - now
message = f"{file} available in {delta}"
if terminal_output:
print(message, end='', file=terminal_output, flush=True)
if delta > timedelta(hours=2):
delta = delta % timedelta(hours=1)
elif delta > timedelta(minutes=2):
delta = delta % timedelta(minutes=1)
elif delta > timedelta(seconds=2):
delta = delta % timedelta(seconds=1)
elif delta > timedelta(milliseconds=20):
delta = delta % timedelta(milliseconds=10)
time.sleep(delta.total_seconds())
print('\033[2K\r',
end='',
file=terminal_output,
flush=True)
else:
print(message)
time.sleep(delta.total_seconds())
url = f"https://www.adventofcode.com/{year}/day/{day}/input"
print(f"{file} = {url}")
if dry_run:
continue
request = Request(
url,
headers={
"User-Agent":
"https://github.com/ephemient/aoc2022/blob/main/get-inputs",
"Cookie": f"session={session}"
})
response = urlopen(request)
if response.status != 200:
raise HTTPError(response.url, response.code, response.msg,
response.headers, None)
with response:
with open(file, 'wb') as f:
shutil.copyfileobj(response, f)
if __name__ == "__main__":
main()