get_tasks.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. )
  19. with open(readme_path, "w") as readme:
  20. readme.write(md.markdownify(str(soup.find_all("article")[0])))
  21. if len(soup.find_all("article")) > 1:
  22. with open(readme_path, "a") as readme:
  23. readme.write(md.markdownify(str(soup.find_all("article")[1])))
  24. def get_input(task_dir, day):
  25. input_path = os.path.join(task_dir, "input.txt")
  26. example_path = os.path.join(task_dir, "example.txt")
  27. cookies_dict = {
  28. "session": "53616c7465645f5ffe3db8d154199da4d6e4e569142fda21d3350f5e550f2a4c509bd1b147264ffe0a0d2124909ec5d6"
  29. }
  30. if os.path.exists(input_path):
  31. with open(input_path, "r") as f:
  32. input = f.readlines()
  33. else:
  34. input = requests.get(
  35. f"https://adventofcode.com/2021/day/{day}/input", cookies=cookies_dict
  36. ).text
  37. with open(input_path, "w") as f:
  38. f.write(input)
  39. input = input.splitlines()
  40. if os.path.exists(example_path):
  41. with open(example_path, "r") as e:
  42. example = e.readlines()
  43. else:
  44. example = bs4.BeautifulSoup(
  45. requests.get(
  46. f"https://adventofcode.com/2021/day/{day}", cookies=cookies_dict
  47. ).content
  48. ).code.text
  49. with open(example_path, "w") as f:
  50. f.write(example)
  51. example = example.splitlines()
  52. return input, example
  53. def check_example(example: Any, part: Callable):
  54. print(f'\n{10*"-"}Example test here{10*"-"}\n')
  55. part(example)
  56. print(f'\n{10*"-"}End example test{10*"-"}\n')
  57. if __name__ == "__main__":
  58. # print(get_input("day_1_sonar_sweep", 3))
  59. generate_readme("../day3_binary_diagnostic", 3)