| import os |
| import fitz |
|
|
| def check_files_and_contents(directory, expected_titles, strings_to_check): |
| results = [] |
| |
| |
| if not os.path.exists(directory): |
| return [-1] * len(expected_titles) |
| |
| available_files = os.listdir(directory) |
| |
| for expected_title, string_to_check in zip(expected_titles, strings_to_check): |
| matching_file = None |
| |
| |
| for file in available_files: |
| if expected_title in file and file.endswith(".pdf"): |
| matching_file = file |
| break |
| |
| |
| if not matching_file: |
| results.append(-1) |
| continue |
| |
| file_path = os.path.join(directory, matching_file) |
| |
| try: |
| doc = fitz.open(file_path) |
| text = "" |
| for page in doc: |
| text += page.get_text() |
| |
| |
| if string_to_check in text: |
| results.append(1) |
| else: |
| results.append(0) |
| except Exception as e: |
| print(f"Error reading {matching_file}: {e}") |
| results.append(0) |
| finally: |
| if 'doc' in locals() and doc: |
| doc.close() |
| |
| return results |
|
|
| directory = "/home/user/Desktop/Blog" |
|
|
| |
| expected_titles = [ |
| "LLM Powered Autonomous Agents", |
| "Thinking about High-Quality Human Data" |
| ] |
| strings_to_check = [ |
| "LLM Powered Autonomous Agents", |
| "Thinking about High-Quality Human Data" |
| ] |
|
|
| results = check_files_and_contents(directory, expected_titles, strings_to_check) |
| print(results) |