-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_links_from_pdf.py
205 lines (156 loc) · 5.21 KB
/
get_links_from_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Get the words from a PDF
python3 get_links_from_pdf.py /source/to/pdf
----------------------------
Example:
python3 get_links_from_pdf.py /source/to/pdf --page_start 232 --page_end 254
----------------------------
Parameters:
--page_start [(0,Infinity)]
Starting page, defaults to 0
--page_end [(0,Infinity)]
Ending page, defaults to end of document
--paragraph_start [(0,Infinity)]
Starting paragraph, defaults to 0, the first paragraph
--paragraph_end [(0,Infinity)]
Ending paragraph, defaults to end of page
----------------------------
Optionals:
--unique:
[FLAG] Determines wether to return all links only once
"""
import argparse
import logging
import os
import re
from ast import Dict
from concurrent.futures import ThreadPoolExecutor
from sys import maxsize
from typing import Callable, List
from pypdf import PageObject, PdfReader
PARAGRAPH_DELIMITER = "\n\n"
INFINITY = maxsize
def get_cli_args():
"""Returns the CLI args"""
parser = argparse.ArgumentParser(
prog='get_words_from_pdf',
description='Gets the words from a PDF and applies some operations to them',
epilog='Developed by Pepe Fabra Valverde'
)
# path
parser.add_argument('filename',
help="Absolute path to the PDF")
# parameters
parser.add_argument('--page_start', type=int,
help="Start reading since this page", default=0)
parser.add_argument('--page_end', type=int,
help="Stop reading since at this page", default=INFINITY)
parser.add_argument('--paragraph_start', type=int,
help="Start reading since this paragraph", default=0)
parser.add_argument('--paragraph_end', type=int,
help="Stop reading since at this paragraph", default=INFINITY)
# optional "flags"
parser.add_argument('-u', '--unique', action='store_true',
help="Determines wether to return all links only once",
default=False, required=False)
args = parser.parse_args()
return {
"filename": args.filename,
"page_start": args.page_start,
"page_end": args.page_end,
"paragraph_start": args.paragraph_start,
"paragraph_end": args.paragraph_end,
"unique": args.unique
}
def get_pdf(filename: str) -> PdfReader:
"""Returns the PdfReader instance, or raises an error"""
if not os.path.exists(os.path.realpath(filename)):
raise FileNotFoundError("File not found in the system")
return PdfReader(filename)
def get_pages(pdf_instance: PdfReader, page_start: int, page_end: int) -> List[PageObject]:
"""Gets the pages by range"""
if len(pdf_instance.pages) <= 0:
raise ValueError("Empty content, no pages were found in the PDF")
return [
page
for index, page in enumerate(pdf_instance.pages)
if page_start <= index <= page_end
]
def subtract_text(
pages: List[PageObject],
paragraph_start: int,
paragraph_end: int,
on_page_read: Callable[[str], None],
) -> None:
"""Gets the text from the pages, might use a concurrency/parallelism technique"""
# evaluate first paragraph clause
on_page_read(
PARAGRAPH_DELIMITER.join(
pages[0]
.extract_text()
.split(PARAGRAPH_DELIMITER)[paragraph_start:]
)
)
# evaluate the rest
with ThreadPoolExecutor() as executor:
executor.map(
lambda page: on_page_read(page.extract_text()),
pages[1:-1],
)
# evaluate last paragraph clause
on_page_read(
PARAGRAPH_DELIMITER.join(
pages[-1]
.extract_text()
.split(PARAGRAPH_DELIMITER)[:paragraph_end]
)
)
def get_hyperlinks_from_text(text: str) -> List[str]:
"""Retrieves all the hyperlinks from a text"""
return re.findall(r"(https?://\S+)", text)
def get_hyperlinks(
pages: List[PageObject],
paragraph_start: int = 0,
paragraph_end: int = INFINITY,
unique: bool = False
) -> List[str]:
"""Returns all the hyperlinks"""
hyperlinks: List[str] = []
subtract_text(
pages,
paragraph_start,
paragraph_end,
on_page_read=lambda x: hyperlinks.extend(
get_hyperlinks_from_text(x)
)
)
if unique:
hyperlinks = list(set(hyperlinks))
return hyperlinks
def prepare(cli_args: Dict) -> None:
"""Prepares the system"""
logger = logging.getLogger("pypdf")
logger.setLevel(logging.ERROR)
assert cli_args["filename"].endswith(".pdf")
assert cli_args["page_start"] <= cli_args["page_end"]
if cli_args["page_start"] == cli_args["page_end"]:
assert cli_args["paragraph_start"] <= cli_args["paragraph_end"]
def entrypoint() -> None:
"""Entrypoint"""
cli_args = get_cli_args()
prepare(cli_args)
pdf_instance = get_pdf(cli_args["filename"])
pages = get_pages(
pdf_instance,
cli_args["page_start"],
cli_args["page_end"]
)
hyperlinks = get_hyperlinks(
pages,
cli_args["paragraph_start"],
cli_args["paragraph_end"],
unique=cli_args["unique"]
)
print(hyperlinks)
if __name__ == "__main__":
entrypoint()