get_tasks.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import requests
  3. import bs4
  4. import markdownify as md
  5. from typing import Any, Callable
  6. def generate_readme(task_dir: str, day: int):
  7. readme_path = os.path.join(task_dir, "README.md")
  8. cookies_dict = {
  9. "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
  10. }
  11. if os.path.exists(readme_path):
  12. pass
  13. else:
  14. soup = bs4.BeautifulSoup(
  15. requests.get(
  16. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  17. ).content,
  18. features="html.parser",
  19. )
  20. with open(readme_path, "w") as readme:
  21. readme.write(md.markdownify(str(soup.find_all("article")[0])))
  22. if len(soup.find_all("article")) > 1:
  23. with open(readme_path, "a") as readme:
  24. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  25. def get_input(task_dir: str, day: int) -> tuple[list[str], list[str]]:
  26. input_path = os.path.join(task_dir, "input.txt")
  27. example_path = os.path.join(task_dir, "example.txt")
  28. cookies_dict = {
  29. "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
  30. }
  31. os.makedirs(task_dir, exist_ok=True)
  32. if os.path.exists(input_path):
  33. with open(input_path, "r") as f:
  34. input = f.read().splitlines()
  35. else:
  36. input = requests.get(
  37. f"https://adventofcode.com/2021/day/{day}/input", cookies=cookies_dict
  38. ).text
  39. with open(input_path, "w") as f:
  40. f.write(input)
  41. input = input.splitlines()
  42. if os.path.exists(example_path):
  43. with open(example_path, "r") as e:
  44. example = e.read().splitlines()
  45. else:
  46. example = bs4.BeautifulSoup(
  47. requests.get(
  48. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  49. ).content,
  50. features="html.parser",
  51. ).code.text
  52. with open(example_path, "w") as f:
  53. f.write(example)
  54. example = example.splitlines()
  55. return input, example
  56. def check_example(example: Any, part: Callable):
  57. print(f'\n{10*"-"}Example test here{10*"-"}\n')
  58. part(example)
  59. print(f'\n{10*"-"}End example test{10*"-"}\n')
  60. if __name__ == "__main__":
  61. # print(get_input("day_1_sonar_sweep", 3))
  62. generate_readme("../day3_binary_diagnostic", 3)