-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
40 lines (32 loc) · 1.07 KB
/
main.py
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
from script import DetikNewsApi
from flask import Flask, jsonify, request
app = Flask(__name__)
# Initialize the DetikNewsApi object
DN_API = DetikNewsApi()
@app.route("/search", methods=["GET"])
def search():
# Retrieve query, detail, and limit parameters
qs = request.args.get("q", default="", type=str)
detail = request.args.get("detail", default="false", type=str).lower() in [
"true",
"1",
]
limit = request.args.get("limit", default=None, type=int)
if not qs:
return (
jsonify({"status": 400, "error": "Query parameter 'q' is required."}),
400,
)
try:
# Perform search with optional detail and limit parameters
search_result = DN_API.search(qs, detail=detail, limit=limit)
return (
jsonify(
{"status": 200, "data": search_result, "length": len(search_result)}
),
200,
)
except Exception as e:
return jsonify({"status": 500, "error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)