parse_class_pdf.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. import logging
  3. from argparse import ArgumentParser
  4. import pickle
  5. import json
  6. from parse import (
  7. extract_data_from_class_pdf,
  8. get_modules_for_class_json,
  9. get_modules_json,
  10. get_classes,
  11. deduplicate_modules,
  12. ClassPdfExtractionPageData,
  13. ClassJsonModule,
  14. )
  15. from config import CLASS_PDF_INPUT_FILE, CLASSES_JSON_OUTPUT_FILE
  16. def get_valid_lecturers(file_path: str) -> list[str]:
  17. """
  18. Reads the lecturers JSON file and extracts a list of valid lecturer shorthands.
  19. """
  20. valid_lecturers: list[str] = []
  21. try:
  22. print(f"READING: '{file_path}'")
  23. with open(file_path, "r", encoding="utf-8") as f:
  24. data = json.load(f)
  25. if isinstance(data, list):
  26. for entry in data:
  27. if isinstance(entry, dict) and "short" in entry:
  28. valid_lecturers.append(entry["short"])
  29. logging.info(
  30. "Loaded %d valid lecturers from %s", len(valid_lecturers), file_path
  31. )
  32. except Exception as e:
  33. logging.error("Failed to load valid lecturers from '%s': %s", file_path, e)
  34. return valid_lecturers
  35. def main() -> None:
  36. parser = ArgumentParser(description="Parse class PDF to JSON.")
  37. parser.add_argument(
  38. "-l", "--lecturers", help="Path to the lecturers.json file", default=None
  39. )
  40. parser.add_argument(
  41. "-i", "--input", help="Path to the input PDF file", default=CLASS_PDF_INPUT_FILE
  42. )
  43. parser.add_argument(
  44. "-o",
  45. "--output",
  46. help="Path to the output JSON file",
  47. default=CLASSES_JSON_OUTPUT_FILE,
  48. )
  49. parser.add_argument(
  50. "--save-intermediate",
  51. help="Path to save the intermediate extraction data (pickle format) and exit",
  52. default=None,
  53. )
  54. parser.add_argument(
  55. "--load-intermediate",
  56. help="Path to load the intermediate extraction data from (pickle format) and skip extraction",
  57. default=None,
  58. )
  59. args = parser.parse_args()
  60. lecturers_file = args.lecturers
  61. logging.basicConfig(level=logging.DEBUG)
  62. valid_lecturer_shorthands: list[str] | None = None
  63. if lecturers_file:
  64. valid_lecturer_shorthands = get_valid_lecturers(lecturers_file)
  65. extraction_data: list[ClassPdfExtractionPageData]
  66. if args.load_intermediate:
  67. logging.info("Loading intermediate data from %s", args.load_intermediate)
  68. with open(args.load_intermediate, "rb") as f:
  69. extraction_data = pickle.load(f)
  70. else:
  71. extraction_data = extract_data_from_class_pdf(args.input)
  72. if args.save_intermediate:
  73. logging.info("Saving intermediate data to %s", args.save_intermediate)
  74. with open(args.save_intermediate, "wb") as f:
  75. pickle.dump(extraction_data, f)
  76. return
  77. parsed_modules: list[ClassJsonModule] = [
  78. module
  79. for data in extraction_data
  80. for module in get_modules_for_class_json(
  81. data.raw_extracted_modules,
  82. data.page_metadata.class_name,
  83. data.page_metadata.degree_program,
  84. get_classes(extraction_data),
  85. valid_lecturer_shorthands,
  86. )
  87. ]
  88. parsed_modules = deduplicate_modules(parsed_modules)
  89. json_output: str = get_modules_json(parsed_modules)
  90. with open(args.output, "w", encoding="utf-8") as f:
  91. f.write(json_output)
  92. if __name__ == "__main__":
  93. main()