Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,107 @@
|
|
1 |
-
from flask import Flask, render_template, request
|
2 |
-
import
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
@app.route("/", methods=["GET", "POST"])
|
10 |
def index():
|
11 |
return render_template("index.html")
|
12 |
|
13 |
-
@app.route("/api/suggestions", methods=["GET"])
|
14 |
-
def get_suggestions():
|
15 |
-
search_query = request.args.get("q")
|
16 |
-
api_endpoint = f"{BASE_URL}/api/suggestions?q={search_query}"
|
17 |
-
response = requests.get(api_endpoint)
|
18 |
-
return response.json()
|
19 |
-
|
20 |
-
|
21 |
@app.route("/api/search", methods=["GET"])
|
22 |
def perform_search():
|
23 |
search_query = request.args.get("q")
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
api_endpoint = f"{BASE_URL}/api/search?q={search_query}&max_results={results}&timelimit={time_limit}&safesearch={safe_search}"
|
28 |
-
response = requests.get(api_endpoint)
|
29 |
-
return response.json()
|
30 |
-
|
31 |
-
@app.route("/api/answers", methods=["GET"])
|
32 |
-
def get_people_also_search():
|
33 |
-
search_query = request.args.get("q")
|
34 |
-
api_endpoint = f"{BASE_URL}/api/answers?q={search_query}"
|
35 |
-
response = requests.get(api_endpoint)
|
36 |
-
return response.json()
|
37 |
|
|
|
|
|
|
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
-
|
41 |
-
app.run(host='0.0.0.0', port=port)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from time import sleep
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from requests import get
|
5 |
+
import urllib
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
def _req(term, results, lang, start, proxies, timeout, safe, ssl_verify):
|
10 |
+
resp = get(
|
11 |
+
url="https://www.google.com/search",
|
12 |
+
headers={
|
13 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62"
|
14 |
+
},
|
15 |
+
params={
|
16 |
+
"q": term,
|
17 |
+
"num": results + 2, # Prevents multiple requests
|
18 |
+
"hl": lang,
|
19 |
+
"start": start,
|
20 |
+
"safe": safe,
|
21 |
+
},
|
22 |
+
proxies=proxies,
|
23 |
+
timeout=timeout,
|
24 |
+
verify=ssl_verify,
|
25 |
+
)
|
26 |
+
resp.raise_for_status()
|
27 |
+
return resp
|
28 |
+
|
29 |
+
|
30 |
+
class SearchResult:
|
31 |
+
def __init__(self, url, title, description):
|
32 |
+
self.url = url
|
33 |
+
self.title = title
|
34 |
+
self.description = description
|
35 |
+
|
36 |
+
def __repr__(self):
|
37 |
+
return f"SearchResult(url={self.url}, title={self.title}, description={self.description})"
|
38 |
+
|
39 |
+
def to_dict(self): # Add a method to convert to a dictionary
|
40 |
+
return {
|
41 |
+
"url": self.url,
|
42 |
+
"title": self.title,
|
43 |
+
"description": self.description,
|
44 |
+
}
|
45 |
+
|
46 |
+
|
47 |
+
def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active", ssl_verify=None):
|
48 |
+
"""Search the Google search engine"""
|
49 |
+
|
50 |
+
escaped_term = urllib.parse.quote_plus(term) # make 'site:xxx.xxx.xxx ' works.
|
51 |
+
|
52 |
+
# Proxy
|
53 |
+
proxies = None
|
54 |
+
if proxy:
|
55 |
+
if proxy.startswith("https"):
|
56 |
+
proxies = {"https": proxy}
|
57 |
+
else:
|
58 |
+
proxies = {"http": proxy}
|
59 |
+
|
60 |
+
# Fetch
|
61 |
+
start = 0
|
62 |
+
results = []
|
63 |
+
while start < num_results:
|
64 |
+
# Send request
|
65 |
+
resp = _req(escaped_term, num_results - start,
|
66 |
+
lang, start, proxies, timeout, safe, ssl_verify)
|
67 |
+
|
68 |
+
# Parse
|
69 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
70 |
+
result_block = soup.find_all("div", attrs={"class": "g"})
|
71 |
+
if len(result_block) == 0:
|
72 |
+
start += 1
|
73 |
+
for result in result_block:
|
74 |
+
# Find link, title, description
|
75 |
+
link = result.find("a", href=True)
|
76 |
+
title = result.find("h3")
|
77 |
+
description_box = result.find(
|
78 |
+
"div", {"style": "-webkit-line-clamp:2"})
|
79 |
+
if description_box:
|
80 |
+
description = description_box.text
|
81 |
+
if link and title and description:
|
82 |
+
start += 1
|
83 |
+
if advanced:
|
84 |
+
results.append(SearchResult(link["href"], title.text, description))
|
85 |
+
else:
|
86 |
+
results.append(link["href"])
|
87 |
+
sleep(sleep_interval)
|
88 |
+
|
89 |
+
return results
|
90 |
|
91 |
@app.route("/", methods=["GET", "POST"])
|
92 |
def index():
|
93 |
return render_template("index.html")
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
@app.route("/api/search", methods=["GET"])
|
96 |
def perform_search():
|
97 |
search_query = request.args.get("q")
|
98 |
+
max_results = int(request.args.get("max_results", 10)) # Default to 10 results
|
99 |
+
|
100 |
+
search_results = search(term=search_query, num_results=max_results, advanced=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
# Convert search results to a list of dictionaries
|
103 |
+
results_json = [result.to_dict() for result in search_results]
|
104 |
+
return jsonify(results_json)
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
+
app.run(debug=True)
|
|