1
0

aocutils.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import requests
  3. import bs4
  4. import markdownify as md
  5. def create_jl(task_dir: str, day: int):
  6. jl_path = os.path.join(task_dir, "puzzle.jl")
  7. if os.path.exists(jl_path):
  8. os.utime(jl_path, None)
  9. else:
  10. open(jl_path, "a").close()
  11. def generate_readme(task_dir: str, day: int):
  12. os.makedirs(task_dir, exist_ok=True)
  13. readme_path = os.path.join(task_dir, "README.md")
  14. cookies_dict = {
  15. "session": "53616c7465645f5fefb3f3f69b82ffe6cf03aeb1a491c72b218281b2f7a8fc768b12c1f70fe183ba512239efd882d68c28426443fd2b7e71c03833bfdbdd3562"
  16. }
  17. soup = bs4.BeautifulSoup(
  18. requests.get(
  19. f"https://adventofcode.com/2022/day/{day}", cookies=cookies_dict
  20. ).content,
  21. features="html.parser",
  22. )
  23. with open(readme_path, "w") as readme:
  24. readme.write(md.markdownify(str(soup.find_all("article")[0])))
  25. if len(soup.find_all("article")) > 1:
  26. with open(readme_path, "a") as readme:
  27. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  28. def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]] | None:
  29. input_path = os.path.join(task_dir, "input.txt")
  30. example_path = os.path.join(task_dir, "example.txt")
  31. readme_path = os.path.join(task_dir, "README.md")
  32. cookies_dict = {
  33. "session": "53616c7465645f5f479c5542473264aed531b6c7ec141c211d7b5b73dc7e982507a8452a3f54c7c91e666d48e23dedb590e8637e3003669d5c71bc35d7a3b6cc"
  34. }
  35. os.makedirs(task_dir, exist_ok=True)
  36. if os.path.exists(input_path):
  37. with open(input_path, "r") as f:
  38. input = f.read().splitlines()
  39. else:
  40. input = requests.get(
  41. f"https://adventofcode.com/2022/day/{day}/input", cookies=cookies_dict
  42. ).text
  43. with open(input_path, "w") as f:
  44. f.write(input.strip())
  45. input = input.splitlines()
  46. if os.path.exists(example_path):
  47. with open(example_path, "r") as e:
  48. example = e.read().splitlines()
  49. elif os.path.exists(readme_path):
  50. with open(example_path, "w") as e:
  51. with open(readme_path, "r") as r:
  52. example = r.read().split("\n\n```\n")[1]
  53. e.write(example)
  54. example = example.splitlines()
  55. else:
  56. print("call `generate_readme()` first!")
  57. return
  58. return input, example
  59. if __name__ == "__main__":
  60. day = 6
  61. generate_readme(f"day{day}", day)
  62. get_input(f"day{day}", day)
  63. create_jl(f"day{day}", day)