resume/docker/lucky-ddg/app.py

31 lines
895 B
Python

import subprocess
from flask import Flask, request, redirect
app = Flask(__name__)
@app.route('/search')
def search():
query = request.args.get('q')
if not query:
return "Query parameter 'q' is missing.", 400
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)