Spaces:
Sleeping
Sleeping
Github Change
#2
by
That1BrainCell
- opened
app.py
CHANGED
@@ -1,172 +1,327 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import concurrent.futures
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from googlesearch import search
|
5 |
+
from duckduckgo_search import DDGS
|
6 |
+
import concurrent.futures
|
7 |
+
import re
|
8 |
+
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
API_KEY_DEFAULT = '12345'
|
13 |
+
|
14 |
+
# Function to search DuckDuckGo
|
15 |
+
def duckduckgo_search(query):
|
16 |
+
try:
|
17 |
+
results = DDGS().text(f"{query} manual filetype:pdf", max_results=5)
|
18 |
+
return [res['href'] for res in results]
|
19 |
+
except:
|
20 |
+
return []
|
21 |
+
|
22 |
+
# Function to search Google
|
23 |
+
def google_search(query):
|
24 |
+
|
25 |
+
links = []
|
26 |
+
try:
|
27 |
+
api_key = 'AIzaSyDV_uJwrgNtawqtl6GDfeUj6NqO-H1tA4c'
|
28 |
+
search_engine_id = 'c4ca951b9fc6949cb'
|
29 |
+
|
30 |
+
url = f"https://www.googleapis.com/customsearch/v1"
|
31 |
+
params = {
|
32 |
+
"key": api_key,
|
33 |
+
"cx": search_engine_id,
|
34 |
+
"q": query + " manual filetype:pdf"
|
35 |
+
}
|
36 |
+
|
37 |
+
response = requests.get(url, params=params)
|
38 |
+
results = response.json()
|
39 |
+
|
40 |
+
for item in results.get('items', []):
|
41 |
+
links.append(item['link'])
|
42 |
+
except:
|
43 |
+
pass
|
44 |
+
|
45 |
+
try:
|
46 |
+
extension = "ext:pdf"
|
47 |
+
for result in search(query + " manual " + extension, num_results=5):
|
48 |
+
if result.endswith('.pdf'):
|
49 |
+
links.append(result)
|
50 |
+
except:
|
51 |
+
pass
|
52 |
+
|
53 |
+
|
54 |
+
return links
|
55 |
+
|
56 |
+
# Function to search Internet Archive
|
57 |
+
def archive_search(query):
|
58 |
+
|
59 |
+
try:
|
60 |
+
url = "https://archive.org/advancedsearch.php"
|
61 |
+
params = {
|
62 |
+
'q': f'{query} manual',
|
63 |
+
'fl[]': ['identifier', 'title', 'format'],
|
64 |
+
'rows': 50,
|
65 |
+
'page': 1,
|
66 |
+
'output': 'json'
|
67 |
+
}
|
68 |
+
|
69 |
+
# Make the request
|
70 |
+
response = requests.get(url, params=params)
|
71 |
+
data = response.json()
|
72 |
+
|
73 |
+
# Function to extract hyperlinks from a webpage
|
74 |
+
def extract_hyperlinks(url):
|
75 |
+
# Send a GET request to the URL
|
76 |
+
response = requests.get(url)
|
77 |
+
|
78 |
+
# Check if the request was successful
|
79 |
+
if response.status_code == 200:
|
80 |
+
# Parse the HTML content of the page
|
81 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
82 |
+
|
83 |
+
# Find all <a> tags (hyperlinks)
|
84 |
+
for link in soup.find_all('a', href=True):
|
85 |
+
href = link['href']
|
86 |
+
if href.endswith('.pdf'):
|
87 |
+
pdf_files.append(url+'/'+href)
|
88 |
+
if href.endswith('.iso'):
|
89 |
+
# If the link ends with .iso, follow the link and extract .pdf hyperlinks
|
90 |
+
extract_pdf_from_iso(url+'/'+href+'/')
|
91 |
+
|
92 |
+
# Function to extract .pdf hyperlinks from an .iso file
|
93 |
+
def extract_pdf_from_iso(iso_url):
|
94 |
+
# Send a GET request to the ISO URL
|
95 |
+
iso_response = requests.get(iso_url)
|
96 |
+
|
97 |
+
# Check if the request was successful
|
98 |
+
if iso_response.status_code == 200:
|
99 |
+
# Parse the HTML content of the ISO page
|
100 |
+
iso_soup = BeautifulSoup(iso_response.text, 'html.parser')
|
101 |
+
|
102 |
+
# Find all <a> tags (hyperlinks) in the ISO page
|
103 |
+
for link in iso_soup.find_all('a', href=True):
|
104 |
+
href = link['href']
|
105 |
+
if href.endswith('.pdf'):
|
106 |
+
pdf_files.append('https:'+href)
|
107 |
+
|
108 |
+
pdf_files = []
|
109 |
+
|
110 |
+
def process_doc(doc):
|
111 |
+
identifier = doc.get('identifier', 'N/A')
|
112 |
+
# title = doc.get('title', 'N/A')
|
113 |
+
# format = doc.get('format', 'N/A')
|
114 |
+
pdf_link = f"https://archive.org/download/{identifier}"
|
115 |
+
extract_hyperlinks(pdf_link)
|
116 |
+
|
117 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
118 |
+
futures = [executor.submit(process_doc, doc) for doc in data['response']['docs']]
|
119 |
+
|
120 |
+
# Optionally, wait for all futures to complete and handle any exceptions
|
121 |
+
for future in concurrent.futures.as_completed(futures):
|
122 |
+
try:
|
123 |
+
future.result() # This will raise an exception if the function call raised
|
124 |
+
except Exception as exc:
|
125 |
+
print(f'Generated an exception: {exc}')
|
126 |
+
|
127 |
+
|
128 |
+
return pdf_files
|
129 |
+
|
130 |
+
except:
|
131 |
+
return []
|
132 |
+
|
133 |
+
def github_search(query):
|
134 |
+
|
135 |
+
try:
|
136 |
+
# GitHub Search API endpoint
|
137 |
+
url = f"https://api.github.com/search/code?q={query}+extension:md"
|
138 |
+
|
139 |
+
headers = {
|
140 |
+
'Authorization': 'Token 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'
|
141 |
+
}
|
142 |
+
|
143 |
+
# Make the request
|
144 |
+
response = requests.get(url,headers=headers)
|
145 |
+
data = response.json()
|
146 |
+
links = [item['html_url'].replace('/blob','').replace('//github','//raw.github') for item in data['items']]
|
147 |
+
|
148 |
+
return links
|
149 |
+
|
150 |
+
except:
|
151 |
+
return []
|
152 |
+
|
153 |
+
|
154 |
+
#Similarity Check
|
155 |
+
|
156 |
+
def extract_similar_products(query):
|
157 |
+
results = DDGS().chat(f'{query} Similar Products')
|
158 |
+
|
159 |
+
pattern = r'^\d+\.\s(.+)$'
|
160 |
+
matches = re.findall(pattern, results, re.MULTILINE)
|
161 |
+
matches = [item.split(': ')[0] for item in matches]
|
162 |
+
print(matches)
|
163 |
+
|
164 |
+
return matches[:5] if matches else []
|
165 |
+
|
166 |
+
|
167 |
+
# Define API routes -------------------------------------------------------
|
168 |
+
|
169 |
+
@app.route('/')
|
170 |
+
def home():
|
171 |
+
return render_template('index.html')
|
172 |
+
|
173 |
+
|
174 |
+
@app.route('/search/google', methods=['GET','POST'])
|
175 |
+
def search_google():
|
176 |
+
|
177 |
+
if request.method == 'POST':
|
178 |
+
data = request.get_json()
|
179 |
+
api_key = data.get('API_KEY')
|
180 |
+
product = data.get('product')
|
181 |
+
else:
|
182 |
+
product = request.args.get('product')
|
183 |
+
api_key = request.args.get('API_KEY')
|
184 |
+
|
185 |
+
similar_products = extract_similar_products(product)
|
186 |
+
if api_key == API_KEY_DEFAULT:
|
187 |
+
results = {product: google_search(product)}
|
188 |
+
for p in similar_products:
|
189 |
+
results[p] = google_search(p)
|
190 |
+
return jsonify(results)
|
191 |
+
|
192 |
+
else:
|
193 |
+
return jsonify({'error': 'Invalid API key'}), 401
|
194 |
+
|
195 |
+
@app.route('/search/duckduckgo', methods=['GET','POST'])
|
196 |
+
def search_duckduckgo():
|
197 |
+
|
198 |
+
if request.method == 'POST':
|
199 |
+
data = request.get_json()
|
200 |
+
api_key = data.get('API_KEY')
|
201 |
+
product = data.get('product')
|
202 |
+
else:
|
203 |
+
product = request.args.get('product')
|
204 |
+
api_key = request.args.get('API_KEY')
|
205 |
+
|
206 |
+
similar_products = extract_similar_products(product)
|
207 |
+
|
208 |
+
|
209 |
+
if api_key == API_KEY_DEFAULT:
|
210 |
+
results = {product: duckduckgo_search(product)}
|
211 |
+
for p in similar_products:
|
212 |
+
results[p] = duckduckgo_search(p)
|
213 |
+
return jsonify(results)
|
214 |
+
else:
|
215 |
+
return jsonify({'error': 'Invalid API key'}), 401
|
216 |
+
|
217 |
+
|
218 |
+
@app.route('/search/archive', methods=['GET','POST'])
|
219 |
+
def search_archive():
|
220 |
+
|
221 |
+
if request.method == 'POST':
|
222 |
+
data = request.get_json()
|
223 |
+
api_key = data.get('API_KEY')
|
224 |
+
product = data.get('product')
|
225 |
+
else:
|
226 |
+
product = request.args.get('product')
|
227 |
+
api_key = request.args.get('API_KEY')
|
228 |
+
|
229 |
+
# Retrieve custom headers if any
|
230 |
+
|
231 |
+
similar_products = extract_similar_products(product)
|
232 |
+
|
233 |
+
if api_key == API_KEY_DEFAULT:
|
234 |
+
results = {product: archive_search(product)}
|
235 |
+
|
236 |
+
def process_product(product):
|
237 |
+
return product, archive_search(product)
|
238 |
+
|
239 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
240 |
+
# Map the process_product function to similar_products
|
241 |
+
future_to_product = {executor.submit(process_product, p): p for p in similar_products}
|
242 |
+
|
243 |
+
# Collect results as they complete
|
244 |
+
for future in concurrent.futures.as_completed(future_to_product):
|
245 |
+
product, result = future.result()
|
246 |
+
results[product] = result
|
247 |
+
|
248 |
+
return jsonify(results)
|
249 |
+
|
250 |
+
else:
|
251 |
+
return jsonify({'error': 'Invalid API key'}), 401
|
252 |
+
|
253 |
+
|
254 |
+
@app.route('/search/github', methods=['GET','POST'])
|
255 |
+
def search_github():
|
256 |
+
|
257 |
+
if request.method == 'POST':
|
258 |
+
data = request.get_json()
|
259 |
+
api_key = data.get('API_KEY')
|
260 |
+
product = data.get('product')
|
261 |
+
else:
|
262 |
+
product = request.args.get('product')
|
263 |
+
api_key = request.args.get('API_KEY')
|
264 |
+
|
265 |
+
similar_products = extract_similar_products(product)
|
266 |
+
|
267 |
+
if api_key == API_KEY_DEFAULT:
|
268 |
+
results = {product: github_search(product)}
|
269 |
+
for p in similar_products:
|
270 |
+
results[p] = github_search(p)
|
271 |
+
return jsonify(results)
|
272 |
+
|
273 |
+
else:
|
274 |
+
return jsonify({'error': 'Invalid API key'}), 401
|
275 |
+
|
276 |
+
|
277 |
+
@app.route('/search/all', methods=['GET','POST'])
|
278 |
+
def search_all():
|
279 |
+
|
280 |
+
if request.method == 'POST':
|
281 |
+
data = request.get_json()
|
282 |
+
api_key = data.get('API_KEY')
|
283 |
+
product = data.get('product')
|
284 |
+
else:
|
285 |
+
product = request.args.get('product')
|
286 |
+
api_key = request.args.get('API_KEY')
|
287 |
+
|
288 |
+
similar_products = extract_similar_products(product)
|
289 |
+
|
290 |
+
|
291 |
+
if api_key == API_KEY_DEFAULT:
|
292 |
+
|
293 |
+
results = {
|
294 |
+
product : [{'duckduckgo': duckduckgo_search(product)},{'google': google_search(product)},{'github': github_search(product)},{'archive': archive_search(product)}]
|
295 |
+
}
|
296 |
+
|
297 |
+
def search_product(p):
|
298 |
+
return {
|
299 |
+
'product': p,
|
300 |
+
'duckduckgo': duckduckgo_search(p),
|
301 |
+
'google': google_search(p),
|
302 |
+
'github': github_search(p),
|
303 |
+
'archive': archive_search(p)
|
304 |
+
}
|
305 |
+
|
306 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
307 |
+
future_to_product = {executor.submit(search_product, p): p for p in similar_products}
|
308 |
+
|
309 |
+
for future in concurrent.futures.as_completed(future_to_product):
|
310 |
+
result = future.result()
|
311 |
+
product = result['product']
|
312 |
+
results[product] = [
|
313 |
+
{'duckduckgo': result['duckduckgo']},
|
314 |
+
{'google': result['google']},
|
315 |
+
{'github': result['github']},
|
316 |
+
{'archive': result['archive']}
|
317 |
+
]
|
318 |
+
|
319 |
+
return jsonify(results)
|
320 |
+
|
321 |
+
else:
|
322 |
+
return jsonify({'error': 'Invalid API key'}), 401
|
323 |
+
|
324 |
+
# Run the Flask app
|
325 |
+
if __name__ == '__main__':
|
326 |
+
app.run(debug=True)
|
327 |
+
|