generated from biobricks-ai/brick-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on some validation logic to catch bad PDFs
- Loading branch information
1 parent
33ca9db
commit f7e887f
Showing
3 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
requests | ||
requests | ||
pypdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
import requests | ||
from pypdf import PdfReader | ||
from pathlib import Path | ||
|
||
def is_readable_pdf(file_path): | ||
try: | ||
with open(file_path, 'rb') as file: | ||
reader = PdfReader(file) | ||
if len(reader.pages) > 0: | ||
# Try to extract text from the first page | ||
text = reader.pages[0].extract_text() | ||
return len(text.strip()) > 0 | ||
except Exception: | ||
return False | ||
return True | ||
|
||
good_pdfs = [] | ||
bad_pdfs = [] | ||
|
||
if __name__ == '__main__': | ||
pdfs = Path("pdfs") | ||
for file in pdfs.iterdir(): | ||
if is_readable_pdf(file): | ||
good_pdfs.append(file.name) | ||
else: | ||
bad_pdfs.append(file.name) | ||
print("PDFs captured:", len(good_pdfs)) | ||
print(f"Issues encountered with: {len(bad_pdfs)} files. Retrying...") | ||
# find a way to replace this with the actual URL... | ||
retry_urls = [{f: "url"} for f in bad_pdfs] |