Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,108 +1,47 @@
|
|
1 |
-
from flask import Flask, render_template, request
|
2 |
-
|
3 |
-
from bs4 import BeautifulSoup
|
4 |
-
from requests import get
|
5 |
-
import urllib
|
6 |
import os
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
|
11 |
-
resp = get(
|
12 |
-
url="https://www.google.com/search",
|
13 |
-
headers={
|
14 |
-
"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"
|
15 |
-
},
|
16 |
-
params={
|
17 |
-
"q": term,
|
18 |
-
"num": results + 2, # Prevents multiple requests
|
19 |
-
"hl": lang,
|
20 |
-
"start": start,
|
21 |
-
"safe": safe,
|
22 |
-
},
|
23 |
-
proxies=proxies,
|
24 |
-
timeout=timeout,
|
25 |
-
verify=ssl_verify,
|
26 |
-
)
|
27 |
-
resp.raise_for_status()
|
28 |
-
return resp
|
29 |
-
|
30 |
-
|
31 |
-
class SearchResult:
|
32 |
-
def __init__(self, url, title, description):
|
33 |
-
self.url = url
|
34 |
-
self.title = title
|
35 |
-
self.description = description
|
36 |
-
|
37 |
-
def __repr__(self):
|
38 |
-
return f"SearchResult(url={self.url}, title={self.title}, description={self.description})"
|
39 |
-
|
40 |
-
def to_dict(self): # Add a method to convert to a dictionary
|
41 |
-
return {
|
42 |
-
"url": self.url,
|
43 |
-
"title": self.title,
|
44 |
-
"description": self.description,
|
45 |
-
}
|
46 |
-
|
47 |
-
|
48 |
-
def search(term, num_results=10, lang="en", proxy=None, advanced=False, sleep_interval=0, timeout=5, safe="active", ssl_verify=None):
|
49 |
-
"""Search the Google search engine"""
|
50 |
-
|
51 |
-
escaped_term = urllib.parse.quote_plus(term) # make 'site:xxx.xxx.xxx ' works.
|
52 |
-
|
53 |
-
# Proxy
|
54 |
-
proxies = None
|
55 |
-
if proxy:
|
56 |
-
if proxy.startswith("https"):
|
57 |
-
proxies = {"https": proxy}
|
58 |
-
else:
|
59 |
-
proxies = {"http": proxy}
|
60 |
-
|
61 |
-
# Fetch
|
62 |
-
start = 0
|
63 |
-
results = []
|
64 |
-
while start < num_results:
|
65 |
-
# Send request
|
66 |
-
resp = _req(escaped_term, num_results - start,
|
67 |
-
lang, start, proxies, timeout, safe, ssl_verify)
|
68 |
-
|
69 |
-
# Parse
|
70 |
-
soup = BeautifulSoup(resp.text, "html.parser")
|
71 |
-
result_block = soup.find_all("div", attrs={"class": "g"})
|
72 |
-
if len(result_block) == 0:
|
73 |
-
start += 1
|
74 |
-
for result in result_block:
|
75 |
-
# Find link, title, description
|
76 |
-
link = result.find("a", href=True)
|
77 |
-
title = result.find("h3")
|
78 |
-
description_box = result.find(
|
79 |
-
"div", {"style": "-webkit-line-clamp:2"})
|
80 |
-
if description_box:
|
81 |
-
description = description_box.text
|
82 |
-
if link and title and description:
|
83 |
-
start += 1
|
84 |
-
if advanced:
|
85 |
-
results.append(SearchResult(link["href"], title.text, description))
|
86 |
-
else:
|
87 |
-
results.append(link["href"])
|
88 |
-
sleep(sleep_interval)
|
89 |
-
|
90 |
-
return results
|
91 |
|
92 |
@app.route("/", methods=["GET", "POST"])
|
93 |
def index():
|
94 |
return render_template("index.html")
|
95 |
|
96 |
-
@app.route("/api/
|
97 |
-
def
|
98 |
search_query = request.args.get("q")
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
# Convert search results to a list of dictionaries
|
104 |
-
results_json = [result.to_dict() for result in search_results]
|
105 |
-
return jsonify(results_json)
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
port = int(os.environ.get('PORT', 7860))
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
import requests
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
from webscout import WEBS, transcriber
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
BASE_URL = "https://oevortex-webscout-api.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
@app.route("/", methods=["GET", "POST"])
|
11 |
def index():
|
12 |
return render_template("index.html")
|
13 |
|
14 |
+
@app.route("/api/suggestions", methods=["GET"])
|
15 |
+
def get_suggestions():
|
16 |
search_query = request.args.get("q")
|
17 |
+
api_endpoint = f"{BASE_URL}/api/suggestions?q={search_query}"
|
18 |
+
response = requests.get(api_endpoint)
|
19 |
+
return response.json()
|
20 |
+
|
21 |
+
|
22 |
+
@app.get("/api/search")
|
23 |
+
async def search(
|
24 |
+
q: str,
|
25 |
+
max_results: int = 10,
|
26 |
+
safesearch: str = "moderate",
|
27 |
+
region: str = "wt-wt",
|
28 |
+
backend: str = "api"
|
29 |
+
):
|
30 |
+
"""Perform a text search."""
|
31 |
+
try:
|
32 |
+
with WEBS() as webs:
|
33 |
+
results = webs.text(keywords=q, region=region, safesearch=safesearch, backend=backend, max_results=max_results)
|
34 |
+
return JSONResponse(content=jsonable_encoder(results))
|
35 |
+
except Exception as e:
|
36 |
+
raise HTTPException(status_code=500, detail=f"Error during search: {e}")
|
37 |
+
|
38 |
+
@app.route("/api/answers", methods=["GET"])
|
39 |
+
def get_people_also_search():
|
40 |
+
search_query = request.args.get("q")
|
41 |
+
api_endpoint = f"{BASE_URL}/api/answers?q={search_query}"
|
42 |
+
response = requests.get(api_endpoint)
|
43 |
+
return response.json()
|
44 |
|
|
|
|
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
port = int(os.environ.get('PORT', 7860))
|