-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharvestmen.py
executable file
·342 lines (296 loc) · 11.5 KB
/
harvestmen.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
import requests
from argparse import ArgumentParser, Namespace
from bs4 import BeautifulSoup
from shared.ascii_format import (
RED, RESET, ERROR, FOUND, GREEN, INFO
)
from shared.config import SCRAPTYPE_STR, HEADER
from shared.scrape import Scraper
from shared.open_files import open_file_and_get_entries
class Harvestmen:
"""
This class implements a web scraper that recursively searches
for a specified string within the content of a given base URL
and all reachable links from that URL.
"""
def __init__(
self,
verbose: bool,
base_url: str,
search_string: str,
word_list: str,
recursive: bool,
case_insensitive: bool = False,
recurse_depth: int = 5,
ko_limit: int = 20,
sleep: bool = False,
max_sleep: int = 3
):
self.verbose: bool = verbose
self.base_url: str = base_url
self.search_string: str = search_string
self.recursive: bool = recursive
self.recurse_depth: int = 1 if not recursive else recurse_depth
self.case_insensitive: bool = case_insensitive
self.ko_limit: int = ko_limit
self.sleep: bool = sleep
self.max_sleep: int = max_sleep
self.loop_index: int = 0
if word_list:
try: # Get the word list if it is given
self.word_list: list[str] = \
open_file_and_get_entries(word_list)
except Exception as e:
raise ValueError(e)
else:
self.word_list = []
self.visited_urls: list[str] = []
self.found_count: list[int] = [0]
self.ko_count: int = 0
# A list containing results (dict) of each iteration
#
# Each dict contains:
# Key: the link
# Value: texts surrounding the search strings found inside the link
self.results: list[dict[str, list]] = []
def save_found_strings_with_contexts(self, url: str, text: str) -> int:
"""
Loop through the text looking for the search string.
Any time the string is found, the context (surrounding text)
is saved along with the link on the results dictionary.
"""
count = 0
start = 0
while True:
# Find the index of the search string
start = text.find(self.search_string, start)
if start == -1: # No more occurrences found
break
surrounding = self.get_text_surrounding_search_string(text, start)
# Move past the current occurrence
start += len(self.search_string)
# Create a new entry in the results dictionary
if url in self.results[self.loop_index]:
self.results[self.loop_index][url].append(surrounding)
else:
self.results[self.loop_index][url] = [surrounding]
self.found_count[self.loop_index] += 1 # Increment counter
count += 1
if self.verbose: # Print the found string with context
print("..." + surrounding + "...")
return count
def find_string(self, url: str) -> None:
"""Find the search string in the content of the given URL."""
try:
# Send a GET request to the URL
response = requests.get(url, headers=HEADER)
# Raise an error for bad responses
response.raise_for_status()
# Parse the HTML content
soup = BeautifulSoup(response.text, 'html.parser')
# Get the text from the soup object
text = soup.get_text()
# Check if the search string is in the text
if ((self.search_string and text and
((self.search_string.lower() in text.lower()
and self.case_insensitive)
or (self.search_string in text)))):
# If not already done, add the URL in the found list
if (url not in self.results[self.loop_index]):
count = self.save_found_strings_with_contexts(url, text)
if self.verbose:
print(
f"{FOUND} '{self.search_string}' "
f"found on the webpage {count} time(s).\033[0m"
)
except Exception as e:
print(f"{ERROR} {e}")
def get_text_surrounding_search_string(
self, text: str, begin: int, interval: int = 30) -> str:
# If str_pos - interval is < 0, we set start to pos 0
start = max(0, begin - interval)
# If str_pos + interval is > str_len, we set start to the last char
end = min(
begin + len(self.search_string) + interval,
len(text) - 1
)
# Remove leading and trailing whitespace characters from a string.
# This includes spaces, tabs, newlines (\n), and other whitespace.
stripped_text = text[start:end].strip()
# Replace newlines from within the text by spaces
stripped_text = stripped_text.replace('\n', ' ')
# Color in red the seaerch string inside the text
colored_search_string = RED + self.search_string + RESET
colored_text = stripped_text.replace(
self.search_string, colored_search_string)
return colored_text
def print_single_result(self, loop_index: int) -> None:
if self.verbose:
if self.word_list:
print(
f"\n{INFO} Results for "
f"'{RED}{self.word_list[loop_index]}{RESET}':"
)
else:
print("\nResults:")
print("\n============= Found search word in the following links:")
# Check if self.results and self.loop_index are valid
for link, texts in self.results[loop_index].items():
if self.verbose:
print("> ", end="")
print(f"{GREEN}{link}{RESET}")
if self.verbose:
for text in texts:
print(text)
if self.verbose:
print("============= Occurence:")
print(self.found_count[loop_index])
def run(self) -> None:
end = 1
words = []
ko_limit = self.ko_limit
count = 0
if self.word_list:
end = len(self.word_list)
words = self.word_list
# Init found count for each iteration
self.found_count = [0 for _ in words] if words else [0]
# Init dict for each iteration
self.results = [{} for _ in words] if words else [{}]
while self.loop_index < end:
self.ko_limit = ko_limit
self.visited_urls = []
try:
if words:
self.search_string = words[self.loop_index]
if self.verbose:
print(
"\n============= Searching "
f"'{RED}{self.search_string}{RESET}'...\n"
)
if self.recurse_depth == 1:
self.find_string(self.base_url)
# Recursively loop only if the depth is > 1
elif self.recurse_depth > 1:
scraper = Scraper(SCRAPTYPE_STR, self, self.base_url)
scraper.scrape()
except KeyboardInterrupt:
print("\nExiting...")
finally:
"""
If the string search mode is on, print the URLs of the
images containing the search string in its 'alt' value
"""
if not words:
self.print_single_result(self.loop_index)
count += self.found_count[self.loop_index]
self.loop_index += 1
if words:
for i in range(len(self.results)):
self.print_single_result(i)
if self.verbose:
print(f"\n{INFO} Total occurences:")
print(count)
def parse_args() -> Namespace:
"""Parse command-line arguments."""
# Create the parser
parser = ArgumentParser(description="""This program will
search the given string on the provided link and on every link that
can be reached from that link, recursively.
""")
# Add arguments
parser.add_argument(
'link', type=str, help='the name of the base URL to access'
)
parser.add_argument(
'-s', '--search-string', type=str, help='the string to search'
)
parser.add_argument(
'-i', '--case-insensitive', action='store_true',
help='Enable case-insensitive mode'
)
parser.add_argument(
'-r', '--recursive', action='store_true',
help='Enable recursive search mode'
)
parser.add_argument(
'-l', '--recurse-depth', type=int,
help='indicates the maximum depth level of the recursive download. \
If not indicated, it will be 5. \
(-r/--recursive has to be activated).'
)
parser.add_argument(
'-k', '--ko-limit', type=int,
help="Number of already visited/bad links that are \
allowed before we terminate the search. This is to ensure \
that we don't get stuck into a loop."
)
parser.add_argument(
'-v', '--verbose', action='store_true', help="Enable verbose mode.")
parser.add_argument(
'-S', '--sleep', action='store_true',
help="Enable sleep between HTTP requests to mimic a human-like \
behavior"
)
parser.add_argument(
'-t', '--max-sleep', type=int,
help='Maximum duration of the random sleeps between HTTP requests. \
If not indicated, it will be 3. \
(-s/--search-string has to be activated).'
)
parser.add_argument(
'-w', '--word-list', type=str,
help='Give the program a word list that will be used as search \
strings.'
)
args = parser.parse_args()
if not args.search_string and not args.word_list:
parser.error(
"Either s/--search-string or -w/--word-list has to be specified."
)
# Validate that -l is not used without -r
if args.recurse_depth and not args.recursive:
parser.error(
"The -l/--recurse-limit option can only be used "
"with -r/--recursive."
)
# Validate that -t is not used without -S
if args.max_sleep and not args.sleep:
parser.error(
"The -t/--max-sleep option can only be used "
"with -S/--sleep."
)
if args.search_string and args.word_list:
parser.error(
"The -s/--search-string option cannot be used "
"with -w/--word-list."
)
return args
def main():
# Parse command-line arguments
args = parse_args()
if not args.recurse_depth:
args.recurse_depth = 5
if not args.ko_limit:
args.ko_limit = 50
if not args.verbose:
args.verbose = False
if not args.max_sleep:
args.max_sleep = 3
# Create an instance of Harvestmen
scraper = Harvestmen(
args.verbose,
args.link,
args.search_string, args.word_list,
args.recursive, args.case_insensitive,
args.recurse_depth, args.ko_limit,
args.sleep, args.max_sleep
)
# Run the scraper
try:
scraper.run()
except Exception as e:
print(f"{ERROR} An error occurred: {e}")
if __name__ == "__main__":
main()