forked from colin/resume
Update docker/lucky-ddg/app.py
ci/woodpecker/push/woodpecker Pipeline failed
Details
ci/woodpecker/push/woodpecker Pipeline failed
Details
This commit is contained in:
parent
adc1726374
commit
525c8a11f9
|
@ -1,5 +1,5 @@
|
||||||
|
import subprocess
|
||||||
from flask import Flask, request, redirect
|
from flask import Flask, request, redirect
|
||||||
from duckduckgo_search import DDGS
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -9,11 +9,22 @@ def search():
|
||||||
if not query:
|
if not query:
|
||||||
return "Query parameter 'q' is missing.", 400
|
return "Query parameter 'q' is missing.", 400
|
||||||
|
|
||||||
with DDGS() as ddgs:
|
try:
|
||||||
results = ddgs.text(query, max_results=1)
|
# Execute the ddgs CLI command to perform the search
|
||||||
if results:
|
result = subprocess.run(
|
||||||
return redirect(results[0]['href'])
|
['ddgs', 'text', '-k', query, '-m', '1'],
|
||||||
return "No results found.", 404
|
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__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5000)
|
app.run(host='0.0.0.0', port=5000)
|
||||||
|
|
Loading…
Reference in New Issue