Skip to content

Commit

Permalink
Merge pull request binjie09#13 from Zyx-A/main
Browse files Browse the repository at this point in the history
提供POST支持,提供DDG的answers、images、videos搜索,提供异步支持
  • Loading branch information
binjie09 authored Dec 5, 2023
2 parents b639936 + 68f4f8a commit a64c3f2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
27 changes: 0 additions & 27 deletions api/index.py

This file was deleted.

76 changes: 74 additions & 2 deletions app.py
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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ requests>=2.28.2
urllib3==1.26.15
Werkzeug==2.2.3
zipp==3.15.0
flask[async]

0 comments on commit a64c3f2

Please sign in to comment.