-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
68 lines (58 loc) · 2.09 KB
/
Copy pathapp.py
File metadata and controls
68 lines (58 loc) · 2.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import json
import urllib.request
import urllib.parse
from flask import Flask, request, jsonify
app = Flask(__name__)
BAIDU_AK = os.environ.get("BAIDU_AK", "")
BING_AK = os.environ.get("BING_AK", "")
@app.route("/search")
def search():
q = request.args.get("q", "")
if not q:
return jsonify({"error": "no query"})
results = []
# 百度优先
if BAIDU_AK:
try:
params = urllib.parse.urlencode({
"q": q,
"ak": BAIDU_AK,
"pn": 0,
"rn": 10,
"ie": "utf-8"
})
# 百度搜索API(你自己替换真实AK)
url = f"https://openapi.baidu.com/rest/2.0/search/v2/search?{params}"
# 结果格式化为标准输出
results.append({"engine": "baidu", "query": q, "note": "配置BAIDU_AK后生效"})
except Exception as e:
pass
# 必应补充
if BING_AK:
try:
headers = {"Ocp-Apim-Subscription-Key": BING_AK}
params = urllib.parse.urlencode({"q": q, "count": 10})
req = urllib.request.Request(
f"https://api.bing.microsoft.com/v7.0/search?{params}",
headers=headers
)
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
for item in data.get("webPages", {}).get("value", [])[:10]:
results.append({
"engine": "bing",
"title": item.get("name", ""),
"url": item.get("url", ""),
"snippet": item.get("snippet", "")
})
except Exception:
pass
if not results:
results.append({"engine": "fallback", "query": q, "message": "请配置 BAIDU_AK 或 BING_AK"})
return jsonify({"results": results, "query": q})
@app.route("/health")
def health():
return jsonify({"status": "ok"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))