From 525c8a11f970059520bed31b5ac0aa3c854792db Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Feb 2025 14:49:51 -0500 Subject: [PATCH] Update docker/lucky-ddg/app.py --- docker/lucky-ddg/app.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docker/lucky-ddg/app.py b/docker/lucky-ddg/app.py index d56ce0d..0954355 100644 --- a/docker/lucky-ddg/app.py +++ b/docker/lucky-ddg/app.py @@ -1,5 +1,5 @@ +import subprocess from flask import Flask, request, redirect -from duckduckgo_search import DDGS app = Flask(__name__) @@ -9,11 +9,22 @@ def search(): if not query: return "Query parameter 'q' is missing.", 400 - with DDGS() as ddgs: - results = ddgs.text(query, max_results=1) - if results: - return redirect(results[0]['href']) - return "No results found.", 404 + try: + # Execute the ddgs CLI command to perform the search + result = subprocess.run( + ['ddgs', 'text', '-k', query, '-m', '1'], + capture_output=True, + text=True, + check=True + ) + # Parse the output to extract the first result URL + output_lines = result.stdout.splitlines() + for line in output_lines: + if line.startswith('http'): + return redirect(line) + return "No results found.", 404 + except subprocess.CalledProcessError as e: + return f"An error occurred: {e}", 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)