Skip to content

Commit

Permalink
Merge pull request #17 from iTeam-S/webserver_limits
Browse files Browse the repository at this point in the history
Add Limit to search in webserver
  • Loading branch information
gaetan1903 committed Nov 19, 2021
2 parents 2722ea4 + ec2e003 commit c96efcb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ Exemple :
}
```

You can use the parameter `limit` to limit results
```
curl http://<host>:<port>/v1/images/one+piece?limit=4
[RESPONSE]
{
"0":"https://tse1.mm.bing.net/th?id=OIP.GlNk7idD3RCI_SYLiVzSBAHaE7",
"1":"https://tse2.mm.bing.net/th?id=OIP.uePUN5rwpB-7wicu1uxQcgHaFj",
"2":"https://tse2.mm.bing.net/th?id=OIP.dwWBU-A_6KPvvEYsL2nhVgHaFc",
"3":"https://tse1.mm.bing.net/th?id=OIP.5M8tKIhIWvbqGO1prhUGfAHaJ4"
}
```


</details>


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
python_requires='>=3.6',
py_modules=["websearch"], # Name of the python package
install_requires=[
"BeautifulSoup4", "requests", "gevent", "flask"], # Install other dependencies if any
"BeautifulSoup4", "requests", "gevent", "flask"], # depandance
include_package_data=True, # Include all data file with the package
package_data={'': ['*.json']}
)
25 changes: 21 additions & 4 deletions websearch/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

from http.client import error
from flask import Flask, redirect
from flask import Flask, redirect, request
from gevent.pywsgi import WSGIServer
from .script import WebSearch

Expand All @@ -21,30 +21,47 @@ def page_not_found(e):

@server.route('/v1/images/<string:query>')
def websearch_image(query):
limit = request.args.get('limit')
if limit and limit.isdigit():
limit = int(limit)
else:
limit = 100
res = None
try:
query = query.replace('+', ' ')
res = WebSearch(query).images
except error as e:
print(e)
return "Error 500, Something Wrong"
return {res.index(link): link for link in res} if res else redirect('/404')
return {res.index(link): link for link in res[:limit]} \
if res else redirect('/404')


@server.route('/v1/pages/<string:query>')
def websearch_page(query):
limit = request.args.get('limit')
if limit and limit.isdigit():
limit = int(limit)
else:
limit = 100
res = None
try:
query = query.replace('+', ' ')
res = WebSearch(query).pages
except error as e:
print(e)
return "Error 500, Something Wrong"
return {res.index(link): link for link in res} if res else redirect('/404')
return {res.index(link): link for link in res[:limit]} \
if res else redirect('/404')


@server.route('/v1/<string:ext>/<string:query>')
def websearch(ext, query):
limit = request.args.get('limit')
if limit and limit.isdigit():
limit = int(limit)
else:
limit = 100
try:
query = query.replace('+', ' ')
web = WebSearch(query)
Expand All @@ -53,7 +70,7 @@ def websearch(ext, query):
print(e)
return "Error 500, Something Wrong"

return {res.index(link): link for link in res} \
return {res.index(link): link for link in res[:limit]} \
if res and type(res) == list else redirect('/404')


Expand Down

0 comments on commit c96efcb

Please sign in to comment.