Update app.py
Browse files
app.py
CHANGED
|
@@ -13,34 +13,31 @@ def log(message: str):
|
|
| 13 |
print(f"[SEARCH-PROXY] {message}", file=sys.stdout, flush=True)
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
-
def home():
|
| 17 |
-
return {
|
| 18 |
-
"status": "healthy",
|
| 19 |
-
"engine": "DuckDuckGo-SearXNG-Proxy",
|
| 20 |
-
"tip": "Point Open WebUI to this space URL directly."
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
@app.get("/search")
|
| 24 |
@app.get("/proxy")
|
| 25 |
-
@app.get("/v1")
|
| 26 |
def searxng_compat_endpoint(
|
| 27 |
-
q: str = Query(
|
| 28 |
format: str = Query("json")
|
| 29 |
):
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
Maps parameters to DuckDuckGo and formats the response back cleanly.
|
| 33 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
log(f"Received search query request: '{q}'")
|
| 35 |
|
| 36 |
-
# Standard desktop browser user-agent to avoid automated scraping blocks
|
| 37 |
headers = {
|
| 38 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 39 |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
| 40 |
"Accept-Language": "en-US,en;q=0.5"
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# We use the clean HTML-only version of DuckDuckGo for fast parsing
|
| 44 |
url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(q)}"
|
| 45 |
|
| 46 |
try:
|
|
@@ -52,15 +49,13 @@ def searxng_compat_endpoint(
|
|
| 52 |
soup = BeautifulSoup(response.text, "html.parser")
|
| 53 |
results = []
|
| 54 |
|
| 55 |
-
|
| 56 |
-
for row in soup.find_all('div', class_='result')[:8]: # Limit to top 8 dense results
|
| 57 |
title_node = row.find('a', class_='result__url')
|
| 58 |
snippet_node = row.find('a', class_='result__snippet')
|
| 59 |
|
| 60 |
if title_node and snippet_node:
|
| 61 |
raw_url = title_node.get('href', '')
|
| 62 |
|
| 63 |
-
# Clean potential internal proxy redirect paths out of DDG links
|
| 64 |
clean_url = raw_url
|
| 65 |
if "//duckduckgo.com/l/?" in raw_url:
|
| 66 |
match = re.search(r'uddg=([^&]+)', raw_url)
|
|
@@ -74,8 +69,6 @@ def searxng_compat_endpoint(
|
|
| 74 |
})
|
| 75 |
|
| 76 |
log(f"Successfully collected and returned {len(results)} results.")
|
| 77 |
-
|
| 78 |
-
# This wrapper directly mimics the SearXNG JSON structural layout Open WebUI parses
|
| 79 |
return {"results": results}
|
| 80 |
|
| 81 |
except Exception as e:
|
|
|
|
| 13 |
print(f"[SEARCH-PROXY] {message}", file=sys.stdout, flush=True)
|
| 14 |
|
| 15 |
@app.get("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
@app.get("/search")
|
| 17 |
@app.get("/proxy")
|
|
|
|
| 18 |
def searxng_compat_endpoint(
|
| 19 |
+
q: str = Query(None), # Make optional so a plain browser visit doesn't crash
|
| 20 |
format: str = Query("json")
|
| 21 |
):
|
| 22 |
"""
|
| 23 |
+
Catches queries hitting the root URL path or sub-paths seamlessly.
|
|
|
|
| 24 |
"""
|
| 25 |
+
# If a user just opens the page in a web browser without a query parameter
|
| 26 |
+
if not q:
|
| 27 |
+
return {
|
| 28 |
+
"status": "healthy",
|
| 29 |
+
"engine": "DuckDuckGo-SearXNG-Proxy",
|
| 30 |
+
"tip": "Configure this URL inside your Open WebUI dashboard settings."
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
log(f"Received search query request: '{q}'")
|
| 34 |
|
|
|
|
| 35 |
headers = {
|
| 36 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 37 |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
| 38 |
"Accept-Language": "en-US,en;q=0.5"
|
| 39 |
}
|
| 40 |
|
|
|
|
| 41 |
url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(q)}"
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 49 |
soup = BeautifulSoup(response.text, "html.parser")
|
| 50 |
results = []
|
| 51 |
|
| 52 |
+
for row in soup.find_all('div', class_='result')[:8]:
|
|
|
|
| 53 |
title_node = row.find('a', class_='result__url')
|
| 54 |
snippet_node = row.find('a', class_='result__snippet')
|
| 55 |
|
| 56 |
if title_node and snippet_node:
|
| 57 |
raw_url = title_node.get('href', '')
|
| 58 |
|
|
|
|
| 59 |
clean_url = raw_url
|
| 60 |
if "//duckduckgo.com/l/?" in raw_url:
|
| 61 |
match = re.search(r'uddg=([^&]+)', raw_url)
|
|
|
|
| 69 |
})
|
| 70 |
|
| 71 |
log(f"Successfully collected and returned {len(results)} results.")
|
|
|
|
|
|
|
| 72 |
return {"results": results}
|
| 73 |
|
| 74 |
except Exception as e:
|