forked from binjie09/duckduckgo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (63 loc) · 2.44 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from itertools import islice
from duckduckgo_search import DDGS
from flask import Flask, request
app = Flask(__name__)
def run():
if request.method == 'POST':
keywords = request.form['q']
max_results = int(request.form.get('max_results', 10))
else:
keywords = request.args.get('q')
# 从请求参数中获取最大结果数,如果未指定,则默认为10
max_results = int(request.args.get('max_results', 10))
return keywords, max_results
@app.route('/search', methods=['GET', 'POST'])
async def search():
keywords, max_results = run()
results = []
with DDGS() as ddgs:
# 使用DuckDuckGo搜索关键词
ddgs_gen = ddgs.text(keywords, safesearch='Off', timelimit='y', backend="lite")
# 从搜索结果中获取最大结果数
for r in islice(ddgs_gen, max_results):
results.append(r)
# 返回一个json响应,包含搜索结果
return {'results': results}
@app.route('/searchAnswers', methods=['GET', 'POST'])
async def search_answers():
keywords, max_results = run()
results = []
with DDGS() as ddgs:
# 使用DuckDuckGo搜索关键词
ddgs_gen = ddgs.answers(keywords)
# 从搜索结果中获取最大结果数
for r in islice(ddgs_gen, max_results):
results.append(r)
# 返回一个json响应,包含搜索结果
return {'results': results}
@app.route('/searchImages', methods=['GET', 'POST'])
async def search_images():
keywords, max_results = run()
results = []
with DDGS() as ddgs:
# 使用DuckDuckGo搜索关键词
ddgs_gen = ddgs.images(keywords, safesearch='Off', timelimit=None)
# 从搜索结果中获取最大结果数
for r in islice(ddgs_gen, max_results):
results.append(r)
# 返回一个json响应,包含搜索结果
return {'results': results}
@app.route('/searchVideos', methods=['GET', 'POST'])
async def search_videos():
keywords, max_results = run()
results = []
with DDGS() as ddgs:
# 使用DuckDuckGo搜索关键词
ddgs_gen = ddgs.videos(keywords, safesearch='Off', timelimit=None, resolution="high")
# 从搜索结果中获取最大结果数
for r in islice(ddgs_gen, max_results):
results.append(r)
# 返回一个json响应,包含搜索结果
return {'results': results}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)