-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
155 lines (123 loc) · 4.19 KB
/
main.py
File metadata and controls
155 lines (123 loc) · 4.19 KB
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
from __future__ import annotations
import os
from typing import Optional
import requests
import yarl
from colorama import Fore, init
from tabulate import tabulate
from dotenv import load_dotenv
init(autoreset=True)
load_dotenv()
if os.name == "nt":
from colorama import just_fix_windows_console
just_fix_windows_console()
class GoogleSearchAPI:
API_URL = yarl.URL("https://www.googleapis.com/customsearch/v1")
API_KEY = os.environ["GOOGLE_API"]
CX = os.environ["GOOGLE_CX"]
BASE_HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json",
}
@staticmethod
def search(
query: str,
nsfw_filter_enabled: bool = False,
num_results: int = 10,
page: int = 1,
exact_terms: Optional[str] = None,
exclude_terms: Optional[str] = None,
file_type: Optional[str] = None,
or_terms: Optional[str] = None,
site_search: Optional[str] = None,
) -> dict:
if not GoogleSearchAPI.API_KEY:
raise ValueError("GOOGLE_API_KEY is not set")
if not GoogleSearchAPI.CX:
raise ValueError("GOOGLE_CX is not set")
params = {
"key": GoogleSearchAPI.API_KEY,
"cx": GoogleSearchAPI.CX,
"q": query,
"num": num_results,
"start": page,
}
if nsfw_filter_enabled:
params["safe"] = "off"
if exact_terms:
params["exactTerms"] = exact_terms
if exclude_terms:
params["excludeTerms"] = exclude_terms
if file_type:
params["fileType"] = file_type
if or_terms:
params["orTerms"] = or_terms
if site_search:
params["siteSearch"] = site_search
response = requests.get(
GoogleSearchAPI.API_URL, headers=GoogleSearchAPI.BASE_HEADERS, params=params
)
response.raise_for_status()
return response.json()
class ConsolePrinter:
@staticmethod
def print_logo() -> str:
with open("logo.txt") as f:
logo = f.read()
logo = logo.replace("{0}", "\t\t\t" + Fore.BLUE)
logo = logo.replace("{1}", Fore.RED)
logo = logo.replace("{2}", Fore.YELLOW)
logo = logo.replace("{3}", Fore.BLUE)
logo = logo.replace("{4}", Fore.GREEN)
logo = logo.replace("{5}", Fore.RED)
return logo
@staticmethod
def print_search_results(results: dict):
r = []
for _, item in enumerate(results.get("items", []), start=1):
temp = [
[f"{Fore.YELLOW}{item['title']}{Fore.WHITE}"],
[f"{Fore.CYAN}{item['link']}{Fore.WHITE}"],
[f"{Fore.WHITE}{item['snippet']}{Fore.WHITE}"],
]
temp_table = str(
tabulate(
temp,
tablefmt="simple",
showindex=False,
numalign="center",
maxcolwidths=95,
)
)
r.append([temp_table])
table = tabulate(
r, tablefmt="fancy_grid", showindex=range(1, len(r) + 1), numalign="center"
)
print(table)
class SearchApp:
def __init__(self):
self.logo = ConsolePrinter.print_logo()
def run(self):
print(self.logo)
query = input(
f"\t\t\t{Fore.WHITE}[*] {Fore.GREEN}Enter search query: {Fore.WHITE}"
)
num_results = input(
f"\t\t\t{Fore.WHITE}[*] {Fore.GREEN}Enter number of results: {Fore.WHITE}"
)
if not num_results.isdigit():
print(
f"\t\t\t{Fore.WHITE}[*] {Fore.RED}Invalid number of results. Defaulting to 5...{Fore.WHITE}"
)
num_results = 5
else:
num_results = int(num_results)
print(
f"\t\t\t{Fore.WHITE}[*] {Fore.GREEN}Searching for {Fore.YELLOW}{num_results}{Fore.GREEN} results for {Fore.YELLOW}`{query}`{Fore.GREEN}...{Fore.WHITE}"
)
print()
results = GoogleSearchAPI.search(query, num_results=num_results)
ConsolePrinter.print_search_results(results)
if __name__ == "__main__":
app = SearchApp()
app.run()