forked from binjie09/duckduckgo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request binjie09#13 from Zyx-A/main
提供POST支持,提供DDG的answers、images、videos搜索,提供异步支持
- Loading branch information
Showing
3 changed files
with
75 additions
and
29 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,81 @@ | ||
from itertools import islice | ||
|
||
from duckduckgo_search import DDGS | ||
from flask import Flask, request | ||
from api.index import search | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
app.route('/search')(search) | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ requests>=2.28.2 | |
urllib3==1.26.15 | ||
Werkzeug==2.2.3 | ||
zipp==3.15.0 | ||
flask[async] |