-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1743 from intezer/add-pdf-miner-integration
Add pdfminer integration
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 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 |
---|---|---|
|
@@ -22,3 +22,4 @@ regex | |
ruff | ||
scp | ||
urlextract==1.5.0 | ||
pdfminer==20191125 |
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,64 @@ | ||
import logging | ||
from typing import Any | ||
from typing import Dict | ||
from typing import Iterable | ||
from typing import Set | ||
from typing import Union | ||
|
||
try: | ||
from pdfminer import pdfparser | ||
from pdfminer import pdfdocument | ||
from pdfminer import pdftypes | ||
|
||
HAVE_PDFMINER = True | ||
except ImportError: | ||
HAVE_PDFMINER = False | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _search_for_url(obj: Union[dict, list]) -> Iterable[str]: | ||
if obj is None: | ||
return | ||
|
||
if isinstance(obj, pdftypes.PDFStream): | ||
yield from _search_for_url(obj.attrs) | ||
elif isinstance(obj, list): | ||
for v in obj: | ||
yield from _search_for_url(v) | ||
elif isinstance(obj, dict): | ||
for key, value in obj.items(): | ||
if key == 'URI': | ||
yield value.decode() if isinstance(value, bytes) else value | ||
continue | ||
|
||
yield from _search_for_url(value) | ||
|
||
|
||
def _mine_for_urls(file_path: str) -> Set[str]: | ||
urls = set() | ||
try: | ||
with open(file_path, 'rb') as f: | ||
parser = pdfparser.PDFParser(f) | ||
doc = pdfdocument.PDFDocument(parser) | ||
|
||
for xref in doc.xrefs: | ||
for object_id in xref.get_objids(): | ||
try: | ||
obj = doc.getobj(object_id) | ||
urls.update(_search_for_url(obj)) | ||
except Exception as ex: | ||
log.error(ex, exc_info=True) | ||
except Exception as ex: | ||
log.error(ex, exc_info=True) | ||
|
||
return urls | ||
|
||
|
||
def pdfminer_parse(filepath: str, pdfresult: Dict[str, Any]) -> Dict[str, Any]: | ||
if not HAVE_PDFMINER: | ||
return pdfresult | ||
|
||
urls = _mine_for_urls(filepath) | ||
pdfresult["All_URLs"] = list(urls) | ||
return pdfresult |