-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_search-ai
executable file
·704 lines (561 loc) · 22.2 KB
/
deep_search-ai
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#!/usr/bin/python3
import requests
import sys
from bs4 import BeautifulSoup
import os
import re
from urllib.parse import urlparse
import json
import datetime
from requests.exceptions import HTTPError, ConnectionError, Timeout, RequestException
from datetime import datetime
import openai
from openai import OpenAI
from colorama import Fore, Style as ColoramaStyle
from colorama import Fore, Style as ColoramaStyle
import colorama
from serpapi import GoogleSearch
import urllib.parse
from colorama import init, Fore, Back, Style
import argparse
# Initialize the parser
parser = argparse.ArgumentParser(description="Search and process results with optional AI analysis")
# Add arguments
parser.add_argument("query", type=str, nargs="*", help="Search query") # Allow empty query
parser.add_argument("-a", "--ai", action="store_true", help="Continue with AI analysis")
parser.add_argument("-r", "--results", action="store_true", help="Return search results only")
parser.add_argument("-n", "--num-results", type=int, default=20, help="Number of websites to search. example: -n 5")
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
# Set flags for the arguments
arga = args.ai
argr = args.results
numr=args.num_results
noargs = not arga and not argr # If neither --ai nor --results is provided, set noargs to True
# Display messages based on the arguments provided
if argr:
print("choose to display results only\n")
if arga:
print("choose to display AI only\n")
if noargs:
print("choose to display results and AI \n")
print(f"search on {numr} websites")
colorama.init(autoreset=True)
def get_result_url(data):
knowledge_panel = {}
results = []
engine_value = data.get('search_parameters', {}).get('engine')
if engine_value == 'bing':
search_results = data
results = [
{
"position": result["position"],
"title": result["title"],
"url": result["link"],
"snippet": result.get("snippet", "")
}
for result in search_results.get("organic_results", [])
]
knowledge_graph = search_results.get("knowledge_graph", {})
knowledge_panel = {
"type": knowledge_graph.get("type", ""),
"title": knowledge_graph.get("title", ""),
"description": knowledge_graph.get("description", ""),
"quote": {
"title": knowledge_graph.get("quote", {}).get("title", ""),
"link": knowledge_graph.get("quote", {}).get("link", "")
},
"facts": [
{
"title": fact.get("title", ""),
"link": fact.get("link", ""),
"thumbnail": fact.get("thumbnail", "")
} for fact in knowledge_graph.get("facts", [])
],
"profiles": [
{
"title": profile.get("title", ""),
"link": profile.get("link", "")
} for profile in knowledge_graph.get("profiles", [])
],
"website": knowledge_graph.get("website", ""),
"timeline": [
{
"year": event.get("year", ""),
"text": event.get("text", ""),
"link": event.get("link", "")
} for event in knowledge_graph.get("timeline", [])
]
}
if 'knowledge_panel' in data:
kp = data['knowledge_panel']
if kp is not None:
knowledge_panel['name'] = kp.get('name', 'N/A')
knowledge_panel['label'] = kp.get('label', 'N/A')
knowledge_panel['description'] = {
'text': kp.get('description', {}).get('text', 'N/A'),
'url': kp.get('description', {}).get('url', 'N/A'),
'site': kp.get('description', {}).get('site', 'N/A')
}
knowledge_panel['image'] = {
'url': kp.get('image', {}).get('url', 'N/A'),
'width': kp.get('image', {}).get('width', 'N/A'),
'height': kp.get('image', {}).get('height', 'N/A'),
'page_url': kp.get('image', {}).get('page_url', 'N/A')
}
knowledge_panel['info'] = []
for item in kp.get('info', []):
knowledge_panel['info'].append({
'title': item.get('title', 'N/A'),
'labels': item.get('labels', [])
})
count=0
if isinstance(data, list):
for result in data:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('url', 'N/A'),
'description': result.get('description', 'N/A')
})
if 'results' in data:
for result in data['results']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('url', 'N/A'),
'description': result.get('description', 'N/A')
})
if 'items' in data:
for result in data['items']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('link', 'N/A'),
'description': result.get('snippet', 'N/A')
})
if 'objects' in data:
for result in data['objects']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('link', 'N/A'),
'description': result.get('snippet', 'N/A')
})
if 'data' in data:
for result in data['data']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('url', 'N/A'),
'description': result.get('snippet', 'N/A')
})
if 'organic_results' in data:
for result in data['organic_results']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('link', 'N/A'),
'description': result.get('snippet', 'N/A')
})
if engine_value != 'bing':
if 'position' in data:
for result in data['position']:
count += 1
results.append({
'position': str(count),
'title': result.get('title', 'N/A'),
'url': result.get('link', 'N/A'),
'description': result.get('snippet', 'N/A')
})
return {'knowledge_panel': knowledge_panel, 'results': results}
def keyword_in_result(result, keyword):
if not isinstance(keyword, str):
return False
if not isinstance(result, dict):
return False
fields_to_check = ['url', 'title', 'description']
for field in fields_to_check:
field_value = result.get(field, '')
if isinstance(field_value, str):
cleaned_field_value = field_value.replace('\xa0', ' ').strip()
if keyword.lower() in cleaned_field_value.lower():
return True
return False
def filtering_result(data, keyword):
keyword_words = keyword.lower().split()
filtered_items = []
unique_links = set()
count = 0
for item in data.get('results', []):
if all_words_in_result(item, keyword_words):
if item['url'] not in unique_links:
count += 1
item_copy = item.copy()
item_copy['position'] = count
filtered_items.append(item_copy)
unique_links.add(item['url'])
return filtered_items
def all_words_in_result(result, keyword_words):
result_text = f"{result.get('title', '')} {result.get('description', '')} {result.get('url', '')}".lower()
return all(word in result_text for word in keyword_words)
def get_links(results):
with open('links.txt', 'w') as file:
for result in results:
link = result.get('url', 'N/A')
if link != 'N/A':
file.write(f"{link}\n")
def read_links_to_array():
links = []
response=None
try:
with open('links.txt', 'r') as file:
for line in file:
links.append(line.strip())
except FileNotFoundError:
pass
except Exception as e:
pass
return links
def extract_text_from_url(url, output_dir,reads):
try:
print(f"\rreading web: {url}\r", end="")
response = requests.get(url, timeout=10)
response.raise_for_status()
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
for script in soup(["script", "style"]):
script.decompose()
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
title = soup.title.string if soup.title else "default_title"
title = re.sub(r'[\W_]+', '_', title)
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
output_dir = os.path.join(output_dir, os.getcwd())
os.makedirs(output_dir, exist_ok=True)
filename = "readableweb.txt"
full_path = os.path.join(output_dir, filename)
with open(full_path, 'a+') as f:
f.seek(0)
if f.read() == '':
f.truncate(0)
f.write("Extracted Web Content\n\n")
f.seek(0, 2)
f.write(f"Link URL:\n{url}\nInfo from website:\n{text}\n\n")
return full_path
except HTTPError as http_err:
pass
except ConnectionError:
pass
except Timeout:
pass
except RequestException as err:
pass
except Exception as e:
pass
return None
def read_file_to_clear_text():
try:
with open('readableweb.txt', 'r', encoding='utf-8') as file:
content = file.read()
clean_content = ''.join(char for char in content if char.isprintable() or char.isspace())
clean_content = ' '.join(clean_content.split())
return clean_content
except FileNotFoundError:
pass
return None
except Exception as e:
pass
return None
def clean():
try:
os.remove('readableweb.txt')
os.remove('links.txt')
except FileNotFoundError:
pass
except Exception as e:
pass
def get_bot_response(uinput,q):
summary_string = ""
chunk_size = 4000
chunks = [uinput[i:i+chunk_size] for i in range(0, len(uinput), chunk_size)]
count=1
try:
for chunk in chunks:
response = client.chat.completions.create(
model=modelSource,
messages=[
{
'role': 'system',
'content': f'''
user gives you data about a keyword to search the web, which is already scraped.
the keyword that user menation is {q}
You need to read and extract important information from it.
Focus only on the data provided by the user, forget anything else.
focuse only the keyword {q} you must provide info on that only.
if you not find any important points no need to mention that you not find.
find everything connected to this keyword you given
The data will be sent in chunks.
Previous data summary: {summary_string}.
important to share link of the result wherever you read it in content.
Do not repeat points that you already mentioned before.
if not connected directly to the keyword - dont mention it.
if this chunk not provide any info on the keyword - return nothing. empty. not msg at all.
'''
},
{
"role": "user",
"content": chunk,
},
],
stream=True,
temperature=0.7,
max_tokens=4000,
)
summary = ""
for chunk2 in response:
ch = chunk2.choices[0]
txt=ch.delta.content
if txt:
temptext = txt
if count != 1:
if txt.strip()!="":
summary_string += temptext
count+=1
print("")
if summary_string.strip():
cleaned_summary = "\n".join(line for line in summary_string.splitlines() if line.strip())
print(Fore.GREEN + cleaned_summary + ColoramaStyle.RESET_ALL)
return summary_string
except openai.APIError as e:
print(f"API Error: {str(e)}")
sys.exit(1)
except openai.APIConnectionError as e:
print(f"Connection Error: {str(e)}")
sys.exit(1)
except openai.RateLimitError as e:
print(f"Rate Limit Error: {str(e)}")
sys.exit(1)
except Exception as e:
print(f"Unexpected error: {e}")
sys.exit(1)
except KeyboardInterrupt:
print("\nExiting...")
sys.exit(1)
def bing(q,api_key,related_keywords,num,safe):
result_value = None
api_key = api_key
params = {
"api_key": api_key,
"engine": "bing",
"q": q,
"safeSearch":safe,
"count": str(num),
"form": "DEEPSH",
"related_keywords": related_keywords
}
search = GoogleSearch(params)
data = search.get_dict()
result_value = data.get('error')
if result_value==None:
return data
else:
return None
def serpi(q,api_key,related_keywords,num,safe):
result_value = None
params = {
"api_key": api_key,
"engine": "google",
"q": q,
"google_domain": "google.com",
"num": num ,
"safe": safe,
"related_keywords": related_keywords
}
search = GoogleSearch(params)
data = search.get_dict()
result_value = data.get('error')
if result_value==None:
return data
else:
return None
def rapidi(url,q,related_keywords, num, safe):
result_value = None
parsed_url = urlparse(url)
domain_url= parsed_url.netloc
headers = {
"x-rapidapi-key":os.environ["x_rapid_key"],
"x-rapidapi-host": domain_url
}
querystring = {"query":q,"related_keywords":related_keywords,"num":str(num),"safe":safe,"limit":str(num)}
response = requests.get(url, headers=headers, params=querystring)
if response.status_code == 429:
data=None
pass
elif response.status_code == 200:
data=response.json()
result_value = data.get('result')
if result_value =="success":
return data
else:
return None
def search_all(urls,q,related_keywords, num, safe):
data = {}
for url in urls:
result = rapidi(url,q,related_keywords, num, safe)
if result is not None:
data.update(result)
return data
def format_knowledge(data):
if isinstance(data, dict):
print(f"{Fore.CYAN}{Style.BRIGHT}Type: {data.get('type', '')}")
print(f"{Fore.YELLOW}{Style.BRIGHT}Title: {data.get('title', '')}")
print(f"{Fore.WHITE}{Style.BRIGHT}Description: {Fore.LIGHTWHITE_EX}{data.get('description', '')}\n")
if 'quote' in data:
qu=data['quote'].get('title', '')
if qu!="":
print(f"{Fore.GREEN}{Style.BRIGHT}Quote: {qu}")
li=data['quote'].get('link', '')
if li!="":
print(f"{Fore.GREEN}{Style.NORMAL}Link: {li}\n")
if 'facts' in data and data['facts']: # Check if 'facts' exists and is not empty
print(f"{Fore.MAGENTA}{Style.BRIGHT}Facts:")
for fact in data['facts']:
title = fact.get('title', '') # Access 'title' from each fact (which is a dictionary)
if title:
print(f" - {Fore.MAGENTA}{title}")
if fact.get('link'):
print(f" {Fore.MAGENTA}{Style.DIM}Link: {fact.get('link', '')}")
if fact.get('thumbnail'):
print(f" {Fore.MAGENTA}{Style.DIM}Thumbnail: {fact.get('thumbnail', '')}")
print()
if 'profiles' in data and data['profiles']: # Check if 'profiles' exists and is not empty
print(f"{Fore.BLUE}{Style.BRIGHT}Profiles:")
for profile in data['profiles']:
title = profile.get('title', '') # Access 'title' from each profile
link = profile.get('link', '') # Access 'link' from each profile
if title or link: # Only print if there's something to show
print(f" - {Fore.BLUE}{title}: {link}")
print() # Add a newline after profiles
if 'website' in data and data['website']: # Check if 'website' exists and is not empty
print(f"{Fore.CYAN}{Style.BRIGHT}Website: {Fore.CYAN}{data['website']}\n")
if 'timeline' in data and data['timeline']: # Check if 'timeline' exists and is not empty
print(f"{Fore.LIGHTRED_EX}{Style.BRIGHT}Timeline:")
for event in data['timeline']:
year = event.get('year', '') # Access 'year' from each timeline event
text = event.get('text', '') # Access 'text' from each timeline event
link = event.get('link', '') # Access 'link' from each timeline event
if year or text: # Only print if there's something to show
print(f" {Fore.LIGHTRED_EX}{year} - {text}")
if link:
print(f" {Fore.LIGHTRED_EX}{Style.DIM}Link: {link}")
print() # Add a newline after timeline
def format_results(results):
for result in results:
position = result.get('position', '')
title = result.get('title', '')
link = result.get('link', '')
if link =="":
link = result.get('url', '')
snippet = result.get('snippet', '')
if snippet =="":
snippet = result.get('description', '')
print(f"{Fore.CYAN}{Style.BRIGHT}Position: {Fore.LIGHTCYAN_EX}{position}")
print(f"{Fore.YELLOW}{Style.BRIGHT}Title: {Fore.LIGHTYELLOW_EX}{title}")
print(f"{Fore.BLUE}{Style.BRIGHT}Link: {Fore.LIGHTBLUE_EX}{link}")
print(f"{Fore.WHITE}{Style.BRIGHT}Snippet: {Fore.LIGHTWHITE_EX}{snippet}")
print("\n" + "-" * 50 + "\n")
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
openai.api_key = os.getenv("OPENAI_API_KEY")
api_key = os.environ.get('SERPAPI_API_KEY')
modelSource = "gpt-4-turbo"
q = ' '.join(args.query)
related_keywords = "true"
num = numr
safe = "off"
print(f"Searching for: {q}\n")
clean()
output=""
filtering_results=""
extracted_file=""
textall=""
ui=""
output = {}
data=""
print("searching...\n")
###SEARCH WITH SERPAPI
data=serpi(q,api_key,related_keywords,num,safe)
if data:
output.update(get_result_url(data))
###SEARCH WITH RAPIDAPI
urls = [
"https://google-search74.p.rapidapi.com",
"https://google-search72.p.rapidapi.com/search",
"https://google-search95.p.rapidapi.com/googlesearch.php",
"https://google-web-search1.p.rapidapi.com/"
]
data = search_all(urls,q,related_keywords, num, safe)
if data:
output.update(get_result_url(data))
###SEARCH WITH BING
data=bing(q,api_key,related_keywords,num,safe)
if data:
output.update(get_result_url(data))
if not output:
print("No results found. or you exceeded the limit.")
exit()
print("filtering results...\n")
filtering_results=filtering_result(output,q)
knowledge_panel = output.get('knowledge_panel', {}) or "N/A"
if argr or noargs:
# Ensure knowledge_panel is a dictionary before accessing its values.
if isinstance(knowledge_panel, dict) and all(
not value or (isinstance(value, dict) and all(not v for v in value.values()))
for value in knowledge_panel.values()
):
pass
else:
format_knowledge(knowledge_panel)
format_results(filtering_results)
print("\n")
# if argr or noargs:
# if all(not value or (isinstance(value, dict) and all(not v for v in value.values())) for value in knowledge_panel.values()):
# pass
# else:
# format_knowledge(knowledge_panel)
# format_results(filtering_results)
# print("\n")
if arga or noargs:
get_links(filtering_results)
links = read_links_to_array()
reads=0
for link in links:
reads+=1
extracted_file = extract_text_from_url(link,os.getcwd(),reads)
print("\n")
textall=read_file_to_clear_text()
final_string=""
if extracted_file==None or textall==None:
for index, result in enumerate(filtering_results):
if not result:
continue
title = result.get('title', '')
url = result.get('url', '')
description = result.get('description', '')
final_string+=f"title:{title} url:{url} description:{description}\n"
textall=final_string
print("sending to AI...\n")
if textall:
text=f"from links results: {textall}. from knowledge panel: {knowledge_panel}"
response = get_bot_response(text,q)