2022-12-03 14:10:04 +01:00
|
|
|
#!/usr/bin/python3
|
2022-12-12 18:21:11 +01:00
|
|
|
"""
|
|
|
|
Interface en ligne de commande pour interagir avec https://adventofcode.com/
|
|
|
|
Prérequis: fichier ~/.aoc-cookie contenant le cookie de connexion au site
|
|
|
|
Suggéré: alias aoc="$CHEMIN_VERS_CE_REPO/utils/cli.py"
|
|
|
|
"""
|
2022-12-03 14:10:04 +01:00
|
|
|
from optparse import OptionParser
|
2022-12-12 18:21:11 +01:00
|
|
|
from pathlib import Path
|
2022-12-03 14:10:04 +01:00
|
|
|
import datetime
|
2023-12-01 10:47:58 +01:00
|
|
|
import colors
|
2022-12-12 18:21:11 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2022-12-03 14:10:04 +01:00
|
|
|
|
2022-12-12 18:21:11 +01:00
|
|
|
|
|
|
|
home = str(Path.home())
|
|
|
|
with open(os.path.join(home, ".aoc-cookie"), "r", encoding='utf8') as file:
|
2024-12-01 17:37:24 +01:00
|
|
|
cookie = file.read().replace("\n", " ").strip()
|
2022-12-03 14:10:04 +01:00
|
|
|
|
|
|
|
session = requests.Session()
|
|
|
|
session.cookies["session"] = cookie
|
|
|
|
|
2022-12-12 18:21:11 +01:00
|
|
|
|
2022-12-03 14:10:04 +01:00
|
|
|
def download_input(year, day):
|
2022-12-12 18:21:11 +01:00
|
|
|
"""Télécharge la donnée d'entrée correspondant à un certain jour"""
|
|
|
|
request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}/input")
|
|
|
|
with open(f"{year}/inputs/day{str(day).zfill(2)}.txt", "w", encoding='utf8') as file:
|
|
|
|
file.write(request.content.decode())
|
2022-12-03 14:10:04 +01:00
|
|
|
print("ok")
|
|
|
|
|
2022-12-12 18:21:11 +01:00
|
|
|
|
2023-12-01 10:47:58 +01:00
|
|
|
def show_puzzle(year, day, part=None, colored=False):
|
2022-12-12 18:21:11 +01:00
|
|
|
"""Affiche l'énoncé du jour demandé"""
|
|
|
|
request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}")
|
2023-12-01 10:47:58 +01:00
|
|
|
|
|
|
|
content = request.content.decode("utf-8")
|
|
|
|
if colored:
|
|
|
|
content = content.replace("<h2", f"{colors.bold}<h2")
|
|
|
|
content = content.replace('<em class="star">', f'<em class="star">{colors.bold}{colors.yellow}')
|
|
|
|
content = content.replace('<em>', f'<em>{colors.bold}')
|
|
|
|
content = content.replace('<a href', f'{colors.green}<a href')
|
|
|
|
content = content.replace("</h2>", f"</h2>{colors.reset}\n")
|
|
|
|
content = content.replace("</em>", f"</em>{colors.reset}")
|
|
|
|
content = content.replace("</a>", f"</a>{colors.reset}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
soup = BeautifulSoup(content, "html.parser")
|
2022-12-03 14:10:04 +01:00
|
|
|
day_desc = soup.find_all("article", {"class": "day-desc"})
|
|
|
|
if part is None:
|
|
|
|
for i in range(len(day_desc)):
|
|
|
|
print(day_desc[i].text)
|
|
|
|
return
|
|
|
|
if part > len(day_desc) or part <= 0:
|
|
|
|
print(f"Partie {part} non disponible")
|
|
|
|
return
|
2022-12-12 18:21:11 +01:00
|
|
|
print(day_desc[part - 1].text)
|
|
|
|
|
2022-12-03 14:10:04 +01:00
|
|
|
|
|
|
|
def submit(year, day, answer, level=1):
|
2022-12-12 18:21:11 +01:00
|
|
|
"""Soumet une réponse à validation"""
|
|
|
|
request = session.post(
|
|
|
|
f"https://adventofcode.com/{year}/day/{int(day)}/answer",
|
|
|
|
data={"answer": answer, "level": level},
|
|
|
|
)
|
2022-12-13 15:44:38 +01:00
|
|
|
soup = BeautifulSoup(request.content, "html.parser")
|
|
|
|
print(soup.find("main").find("article").find("p").text)
|
2022-12-03 14:10:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def __main__(options, args):
|
2022-12-12 18:21:11 +01:00
|
|
|
"""Fonction principale"""
|
2022-12-03 14:10:04 +01:00
|
|
|
if len(args) == 0:
|
|
|
|
print("Une action doit être spécifiée")
|
|
|
|
return
|
|
|
|
if args[0] == "download" or args[0] == "dl":
|
|
|
|
download_input(options.year, options.day)
|
|
|
|
elif args[0] == "show" or args[0] == "sh":
|
|
|
|
if options.level == "":
|
|
|
|
options.level = None
|
|
|
|
else:
|
|
|
|
options.level = int(options.level)
|
2023-12-01 10:47:58 +01:00
|
|
|
show_puzzle(options.year, options.day, options.level, colored=True)
|
2022-12-03 14:10:04 +01:00
|
|
|
elif args[0] == "submit" or args[0] == "sb":
|
|
|
|
if len(args) < 2:
|
|
|
|
answer = input("Answer ?\n>> ")
|
|
|
|
else:
|
|
|
|
answer = args[1]
|
|
|
|
if options.level == "":
|
|
|
|
options.level = 1
|
|
|
|
submit(options.year, options.day, answer, options.level)
|
|
|
|
else:
|
|
|
|
print(f"Invalid action {args[0]}")
|
|
|
|
|
|
|
|
|
|
|
|
today = datetime.datetime.now()
|
|
|
|
|
|
|
|
USAGE = "usage: %prog (download|dl|show|sh|submit|sb) [options]"
|
|
|
|
parser = OptionParser(usage=USAGE)
|
|
|
|
parser.add_option(
|
|
|
|
"-y",
|
|
|
|
"--year",
|
|
|
|
dest="year",
|
|
|
|
help="Année",
|
|
|
|
action="store",
|
|
|
|
default=today.year
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option(
|
|
|
|
"-d",
|
|
|
|
"--day",
|
|
|
|
dest="day",
|
|
|
|
help="Jour",
|
|
|
|
action="store",
|
|
|
|
default=today.day
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option(
|
|
|
|
"-l",
|
|
|
|
"--level",
|
|
|
|
dest="level",
|
|
|
|
help="Niveau de la difficulté",
|
|
|
|
action="store",
|
|
|
|
default=""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-12 18:21:11 +01:00
|
|
|
if __name__ == "__main__":
|
2022-12-03 14:10:04 +01:00
|
|
|
(OPTIONS, args) = parser.parse_args()
|
2022-12-12 18:21:11 +01:00
|
|
|
__main__(OPTIONS, args)
|