|
from flask import Flask, request, jsonify |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
SEARXNG_INSTANCE_URL = "https://oscarwang2-searxng.hf.space/search" |
|
|
|
@app.route('/search', methods=['GET']) |
|
def search(): |
|
|
|
search_term = request.args.get('q', '') |
|
|
|
if not search_term: |
|
return jsonify({'error': 'No search term provided'}), 400 |
|
|
|
|
|
params = { |
|
'q': search_term, |
|
'format': 'json', |
|
'categories': 'general' |
|
} |
|
|
|
try: |
|
|
|
response = requests.get(SEARXNG_INSTANCE_URL, params=params) |
|
|
|
|
|
if response.status_code == 200: |
|
data = response.json() |
|
|
|
results = data.get('results', [])[:30] |
|
snippets = [] |
|
|
|
|
|
for result in results: |
|
snippet = { |
|
'title': result.get('title', 'No title'), |
|
'snippet': result.get('content', 'No snippet available'), |
|
'url': result.get('url', 'No URL') |
|
} |
|
snippets.append(snippet) |
|
|
|
|
|
return jsonify(snippets) |
|
else: |
|
return jsonify({'error': f'SearXNG API error: {response.status_code}'}), response.status_code |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
if __name__ == '__main__': |
|
|
|
app.run(host='0.0.0.0', port=7860, debug=True) |