-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (39 loc) · 1.25 KB
/
Copy pathserver.py
File metadata and controls
49 lines (39 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request, jsonify
from flask_swagger_ui import get_swaggerui_blueprint
from googlesearch import search
from page_parser import process_links
app = Flask(__name__)
# Swagger configuration
SWAGGER_URL = '/docs'
API_URL = '/static/swagger.json'
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Search API"
}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
@app.route('/search', methods=['GET'])
def perform_search():
query = request.args.get('q')
if not query:
return jsonify({"error": "No query provided"}), 400
results = [{
"link": result.url,
"title": result.title,
"snippet": result.description,
} for result in search(query, num_results=20, advanced=True)]
return jsonify(results)
@app.route('/parse', methods=['GET'])
async def parse_urls():
urls = request.args.get('urls')
if not urls:
return jsonify({"error": "No URLs provided"}), 400
urls = urls.split(',')
if not urls:
return jsonify({"error": "No URLs provided"}), 400
results = await process_links(urls)
return jsonify(results)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=4000, debug=True)