forked from colin/resume
2
0
Fork 0

Update docker/lucky-ddg/app.py
ci/woodpecker/push/woodpecker Pipeline failed Details

This commit is contained in:
colin 2025-02-09 14:49:51 -05:00
parent adc1726374
commit 525c8a11f9
1 changed files with 17 additions and 6 deletions

View File

@ -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)