forked from merwin-asm/OpenCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencrawler
326 lines (240 loc) · 9.57 KB
/
opencrawler
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
#!/usr/bin/python3
"""
Open Crawler v 1.0.0 | CLI
"""
from rich.table import Table
from rich import print
from mongo_db import *
import robots_txt
import platform
import requests
import json
import sys
import os
def mongodb():
# Config File
config_file = "config.json"
# Load configs from config_file - > json
try:
config_file = open(config_file, "r")
configs = json.loads(config_file.read())
config_file.close()
except:
try:
os.system("python3 config.py") # Re-configures
except:
os.system("python config.py") # Re-configures
config_file = open(config_file, "r")
configs = json.loads(config_file.read())
config_file.close()
## Setting Up Configs
MONGODB_PWD = configs["MONGODB_PWD"]
MONGODB_URI = configs["MONGODB_URI"]
# Initializes MongoDB
connect_db(MONGODB_URI, MONGODB_PWD)
# Finding the location of the file
cur_path = __file__.split("/")
cur_path.remove(cur_path[-1])
cur_path_ = ""
for dir_ in cur_path:
cur_path_ += "/" + dir_
cur_path = cur_path_
table_commands = Table(title="Help - Open Crawler v 1.0.0")
table_commands.add_column("Command", style="cyan", no_wrap=True)
table_commands.add_column("Use", style="magenta")
table_commands.add_column("No", justify="right", style="green")
table_commands.add_row("help", "Get info about the commands", "1")
table_commands.add_row("v", "Get the version of open crawler", "2")
table_commands.add_row("crawl", "Starts up the normal crawler", "3")
table_commands.add_row("force_crawl <websites>", "Forcefully crawls a website", "4")
table_commands.add_row("crawled_status", "Shows the amount of data in DB , etc", "5")
table_commands.add_row("configure", "Write / reWrite The config file", "6")
table_commands.add_row("connection-tree <website> <no of layers>", "Makes a tree of websites connected to it, layers by default is 2", "7")
table_commands.add_row("check_html <website>", "Checks if a website respond with html content", "8")
table_commands.add_row("crawlable <website>", "Checks if a website is allowed to be crawled", "9")
table_commands.add_row("dissallowed <website>", "Lists the websites not allowed to be crawled", "10")
table_commands.add_row("re-install", "Re installs the Open Crawler", "11")
table_commands.add_row("update", "Updates the open crawler", "12")
table_commands.add_row("install-requirements", "Installs requirements for open crawler", "13")
table_commands.add_row("search <search>", "Search from the crawled data", "14")
table_commands.add_row("fix_db", "Tools to fix the DB", "15")
try:
main_arg = sys.argv[1]
except:
print(table_commands)
quit()
try:
if main_arg == "help":
print(table_commands)
elif main_arg == "v":
print("""
[medium_spring_green]
______ ______ __
/ \ / \ | \
| $$$$$$\ ______ ______ _______ | $$$$$$\ ______ ______ __ __ __ | $$
| $$ | $$ / \ / \ | \ | $$ \$$ / \ | \ | \ | \ | \| $$
| $$ | $$| $$$$$$\| $$$$$$\| $$$$$$$\ | $$ | $$$$$$\ \$$$$$$\| $$ | $$ | $$| $$
| $$ | $$| $$ | $$| $$ $$| $$ | $$ | $$ __ | $$ \$$/ $$| $$ | $$ | $$| $$
| $$__/ $$| $$__/ $$| $$$$$$$$| $$ | $$ | $$__/ \| $$ | $$$$$$$| $$_/ $$_/ $$| $$
\$$ $$| $$ $$ \$$ \| $$ | $$ \$$ $$| $$ \$$ $$ \$$ $$ $$| $$
\$$$$$$ | $$$$$$$ \$$$$$$$ \$$ \$$ \$$$$$$ \$$ \$$$$$$$ \$$$$$\$$$$ \$$
| $$
| $$
\$$ [bold]v 1.0.0[/bold] [/medium_spring_green]
""")
elif main_arg == "fix_db":
try:
os.system(f"python3 {cur_path}/fix_db.py")
except:
os.system(f"python {cur_path}/fix_db.py")
elif main_arg == "search":
pool = False
try:
test_arg = sys.argv[2:]
except:
print("[red] [-] No Search Text[/red]")
quit()
txt = ""
for e in test_arg:
txt += " " + e
try:
os.system(f"python3 {cur_path}/search.py {txt}")
except:
os.system(f"python {cur_path}/search.py {txt}")
elif main_arg == "configure":
try:
os.system(f"python3 {cur_path}/config.py")
except:
os.system(f"python {cur_path}/config.py")
elif main_arg == "crawl":
try:
os.system(f"python3 {cur_path}/crawler.py")
except:
os.system(f"python {cur_path}/crawler.py")
elif main_arg == "forced_crawl":
try:
web = sys.argv[2]
except:
print("[red] [-] Link Not Passed In [/red]")
quit()
try:
os.system(f"python3 {cur_path}/crawler.py {web}")
except:
os.system(f"python {cur_path}/crawler.py {web}")
elif main_arg == "crawled_status":
mongodb()
try:
res = get_info()
except:
print("[red] [-] Couldn't Get The Info[/red]")
quit()
print("[yellow3] [?] The Info Given Wouldn't Be Accurate[/yellow3]\n")
print(f"[dark_orange] \t : Crawled Sites - > {res[0]} [/dark_orange]")
print(f"[dark_orange] \t : Wait list - > {res[1]} [/dark_orange]")
print("")
elif main_arg == "connection-tree":
try:
web = sys.argv[2]
except:
print("[red] [-] Link Not Passed In [/red]")
quit()
num = 2
try:
num = sys.argv[3]
except:
pass
try:
os.system(f"python3 {cur_path}/connection_tree.py {web} {num}")
except:
os.system(f"python {cur_path}/connection_tree.py {web} {num}")
elif main_arg == "dissallowed":
try:
web = sys.argv[2]
except:
print("[red] [-] Link Not Passed In [/red]")
quit()
try:
restricted = robots_txt.disallowed(web, None)
except:
print("[red] [-] The site was down or some other error [/red]")
quit()
print("[green] [+] Dissallowed : [/green]")
for e in restricted:
print(f"[green] \t\t-------> {e} [/green]")
elif main_arg == "crawlable":
try:
web = sys.argv[2]
except:
print("[red] [-] Link Not Passed In [/red]")
quit()
try:
restricted = robots_txt.disallowed(web, None)
except:
print("[red] [-] The site was down or some other error [/red]")
quit()
site = web
site = site.replace("https://", "")
site = site.replace("http://", "")
web = site.split("/")
web.remove(web[0])
site = ""
for e in web:
site += e + "/"
web = site
A = True
for e in restricted:
if web.startswith(e):
A = False
break
if A:
print(f"[green] [+] Can Be Crawled [/green]")
else:
print(f"[red] [-] Can't Be Crawled [/red]")
elif main_arg == "check_html":
try:
web = sys.argv[2]
except:
print("[red] [-] Link Not Passed In [/red]")
quit()
try:
is_html = "html" in requests.get(web).headers["Content-Type"]
except:
print("[red] [-] Can't Be Checked Because the site is down or no content type provided in headers[/red]")
quit()
if is_html:
print(f"[green] [+] '{web}' Respond With HTML Content [/green]")
else:
print(f"[red] [-] '{web}' Doesn't Respond With HTML Content [/red]")
elif main_arg == "update":
if platform.system() != "windows":
try:
os.system("git clone https://github.com/merwin-asm/OpenCrawler.git")
except:
os.system("sudp rm -rf OpenCrawler")
os.system("git clone https://github.com/merwin-asm/OpenCrawler.git")
os.system("cd OpenCrawler")
os.system("chmod +x install.sh")
os.system("./install.sh")
else:
print("[yellow] This wont work on windows [/yellow]")
elif main_arg == "install_requirements":
try:
os.system(f"pip3 install -r {cur_path}/requirements.txt")
except:
os.system(f"pip install -r {cur_path}/requirements.txt")
elif main_arg == "re_install":
if platform.system() != "windows":
try:
os.system("git clone https://github.com/merwin-asm/OpenCrawler.git")
except:
os.system("sudp rm -rf OpenCrawler")
os.system("git clone https://github.com/merwin-asm/OpenCrawler.git")
os.system("cd OpenCrawler")
os.system("chmod +x install.sh")
os.system("./install.sh")
else:
print("[yellow] This wont work on windows [/yellow]")
else:
print(f"[red] [-] Command '{main_arg}' Not Found || Use 'opencrawler help' For Commands[/red]")
except:
print("[red] [-] Some Error Occurred[/red]")