-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate.py
executable file
·80 lines (54 loc) · 1.75 KB
/
populate.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
#!/usr/bin/env python3
import os
import re
import requests
import sys
import time
from dotenv import load_dotenv
#------------------------------------------------------------------------------#
def find_all(pattern, path):
pattern = re.compile(pattern)
out = []
for root, dirs, files in os.walk(path):
files = [file for file in files if pattern.match(file)]
if files:
out.append(root)
return out
#------------------------------------------------------------------------------#
load_dotenv()
session = os.getenv("SESSION")
if not session:
print("Key \"SESSION\" not found in \'./.env\', aborting.")
exit()
url = "https://adventofcode.com/{year}/day/{day}/input"
paths = find_all(r"solve[.](py|hs|rs)", ".")
home = os.getcwd()
paths = sorted(paths)
p_year_day = re.compile(r"[.]/(?P<year>\d{4})/day(?P<day>\d{2})$")
wait = False
for path in paths:
if wait:
msg = "\tWaiting 5s to not spam requests..."
print(msg, end="")
sys.stdout.flush()
time.sleep(5)
print("\r", " "*(len(msg) + 6), end = "")
print("\r", end="")
wait = False
print(path, end = "\t")
m = p_year_day.match(path)
day = int(m["day"])
year = int(m["year"])
if os.path.exists(os.path.join(path, "input")):
print("Skipped")
else:
print("Acquiring...", end = "\t")
req = requests.get(url.format(year = year, day = day), cookies = {"session":session})
if req.status_code == 200:
with open(os.path.join(path,"input"), "w+") as file:
file.write(req.content.decode("utf-8"))
wait = True
print("Done")
else:
print("Failed w/:", req.status_code)
exit()