-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_day.py
More file actions
executable file
·85 lines (69 loc) · 2.76 KB
/
new_day.py
File metadata and controls
executable file
·85 lines (69 loc) · 2.76 KB
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
#! /usr/bin/env python
"""Script that creates a new problem folder starting
from the problem's URL"""
import argparse
import os
import shutil
import stat
import requests
import tomd
from bs4 import BeautifulSoup
from yaspin import yaspin
parser = argparse.ArgumentParser(description='Creates a new AoC folder.')
parser.add_argument('url', type=str, help='The URL of the problem')
parser.add_argument('session', type=str, help='The value of the "session" cookie')
args = parser.parse_args()
url = args.url
session = args.session
cookies = dict(session=session)
url_parts = url.split('/')
year = url_parts[-3]
day = url_parts[-1]
response = requests.get(
url,
cookies=cookies)
if response.status_code != 200:
print("Oops... Something does not work :( " \
"the request failed with status {}".format(response.status_code))
quit()
# Setup folder structure
problem_folder = '{}/{}'.format(year, day)
if not os.path.exists(problem_folder):
os.makedirs(problem_folder)
readme_file_path = '{}/README.md'.format(problem_folder)
input_file_path = '{}/input'.format(problem_folder)
boilerplate_folder = 'boilerplates/{}'.format(year)
# Start parsing the problem
html = response.text
soup = BeautifulSoup(html, 'html.parser')
# Write the readme file with the problem statement
with open(readme_file_path, "w") as readme:
for i, day in enumerate(soup.find_all('article', class_='day-desc')):
part = i+1
with yaspin(text=f"Adding problem text for part {part}") as spinner:
readme.write(tomd.Tomd(str(day)).markdown)
spinner.ok("✔ ")
next_sibling = day.next_sibling.next_sibling
if next_sibling.name == 'p' \
and next_sibling.text[:4] == 'Your':
print(" Found solution to part {}! Good work!".format(part))
readme.write("\n<details><summary>Spoiler alert!</summary>\n\n"
"<p>{}</p></details>\n".format(next_sibling.text))
# Download the problem input, if exists
with yaspin(text="Downloading the input file") as spinner:
file_url = '{}/input'.format(url)
file_response = requests.get(file_url, stream=True, cookies=cookies)
if file_response.status_code == 200:
with open(input_file_path, "w") as _input:
_input.write(file_response.text)
spinner.ok("✔ ")
# Clone the boilerplate
for boilerplate_file in os.listdir(boilerplate_folder):
source_file = os.path.join(boilerplate_folder, boilerplate_file)
dest_file = os.path.join(problem_folder, boilerplate_file)
if not os.path.exists(dest_file):
shutil.copyfile(source_file, dest_file)
_, file_extension = os.path.splitext(dest_file)
if file_extension == '.py':
st = os.stat(dest_file)
os.chmod(dest_file, st.st_mode | stat.S_IEXEC)