forked from lilingfengdev/PluginSearchEngine
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
52 lines (42 loc) · 1.59 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, request, send_from_directory
from os.path import dirname, abspath, join
from json import loads, dumps
import os
from core import search
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 # 缓存时间为1小时
app.config['STATIC_FOLDER'] = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'static')
@app.route('/<path:filename>')
def static_files(filename):
return send_from_directory(app.config['STATIC_FOLDER'], filename)
@app.route('/')
def home():
return send_from_directory(app.config['STATIC_FOLDER'], 'index.html')
@app.route('/s', methods=['GET'])
def about():
try:
content = []
request_data, s = request.args.get('q'), request.args.get('s')
if s in ["谷歌", "必应"]:
if s == "谷歌":
s = "google"
elif s == "必应":
s = "bing"
result = search.search(request_data, s)
for obj in result:
try:
content.append([obj.title, obj.summary, obj.url])
except Exception as e:
print(e)
print(obj.title, obj.summary, obj.url)
except Exception as e:
return "Something's wrong. " + str(e), 500, {"Content-Type": "application/json"}
# print(e)
# return content, 200, {"Content-Type": "application/json"}
return content, 200, {"Content-Type": "application/json"}
if __name__ == '__main__':
app.run(host='127.0.0.1', port=2333)