1
0

get_tasks.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, day):
  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, day):
  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. if os.path.exists(input_path):
  32. with open(input_path, "r") as f:
  33. input = f.readlines()
  34. else:
  35. input = requests.get(
  36. f"https://adventofcode.com/2021/day/{day}/input", cookies=cookies_dict
  37. ).text
  38. with open(input_path, "w") as f:
  39. f.write(input)
  40. input = input.splitlines()
  41. if os.path.exists(example_path):
  42. with open(example_path, "r") as e:
  43. example = e.readlines()
  44. else:
  45. example = bs4.BeautifulSoup(
  46. requests.get(
  47. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  48. ).content,
  49. features="html.parser",
  50. ).code.text
  51. with open(example_path, "w") as f:
  52. f.write(example)
  53. example = example.splitlines()
  54. return input, example
  55. def check_example(example: Any, part: Callable):
  56. print(f'\n{10*"-"}Example test here{10*"-"}\n')
  57. part(example)
  58. print(f'\n{10*"-"}End example test{10*"-"}\n')
  59. if __name__ == "__main__":
  60. # print(get_input("day_1_sonar_sweep", 3))
  61. generate_readme("../day3_binary_diagnostic", 3)