mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Merge pull request #1180 from bookwyrm-social/search-timeout
Adds timeouts to get requests
This commit is contained in:
commit
1f071d3c18
2 changed files with 12 additions and 4 deletions
|
@ -37,7 +37,7 @@ class AbstractMinimalConnector(ABC):
|
||||||
for field in self_fields:
|
for field in self_fields:
|
||||||
setattr(self, field, getattr(info, field))
|
setattr(self, field, getattr(info, field))
|
||||||
|
|
||||||
def search(self, query, min_confidence=None):
|
def search(self, query, min_confidence=None, timeout=5):
|
||||||
"""free text search"""
|
"""free text search"""
|
||||||
params = {}
|
params = {}
|
||||||
if min_confidence:
|
if min_confidence:
|
||||||
|
@ -46,6 +46,7 @@ class AbstractMinimalConnector(ABC):
|
||||||
data = self.get_search_data(
|
data = self.get_search_data(
|
||||||
"%s%s" % (self.search_url, query),
|
"%s%s" % (self.search_url, query),
|
||||||
params=params,
|
params=params,
|
||||||
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
|
@ -218,7 +219,7 @@ def dict_from_mappings(data, mappings):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def get_data(url, params=None):
|
def get_data(url, params=None, timeout=10):
|
||||||
"""wrapper for request.get"""
|
"""wrapper for request.get"""
|
||||||
# check if the url is blocked
|
# check if the url is blocked
|
||||||
if models.FederatedServer.is_blocked(url):
|
if models.FederatedServer.is_blocked(url):
|
||||||
|
@ -234,6 +235,7 @@ def get_data(url, params=None):
|
||||||
"Accept": "application/json; charset=utf-8",
|
"Accept": "application/json; charset=utf-8",
|
||||||
"User-Agent": settings.USER_AGENT,
|
"User-Agent": settings.USER_AGENT,
|
||||||
},
|
},
|
||||||
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
except (RequestError, SSLError, ConnectionError) as e:
|
except (RequestError, SSLError, ConnectionError) as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
|
@ -250,7 +252,7 @@ def get_data(url, params=None):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def get_image(url):
|
def get_image(url, timeout=10):
|
||||||
"""wrapper for requesting an image"""
|
"""wrapper for requesting an image"""
|
||||||
try:
|
try:
|
||||||
resp = requests.get(
|
resp = requests.get(
|
||||||
|
@ -258,6 +260,7 @@ def get_image(url):
|
||||||
headers={
|
headers={
|
||||||
"User-Agent": settings.USER_AGENT,
|
"User-Agent": settings.USER_AGENT,
|
||||||
},
|
},
|
||||||
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
except (RequestError, SSLError) as e:
|
except (RequestError, SSLError) as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" interface with whatever connectors the app has """
|
""" interface with whatever connectors the app has """
|
||||||
|
from datetime import datetime
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
@ -29,9 +30,11 @@ def search(query, min_confidence=0.1, return_first=False):
|
||||||
isbn = re.sub(r"[\W_]", "", query)
|
isbn = re.sub(r"[\W_]", "", query)
|
||||||
maybe_isbn = len(isbn) in [10, 13] # ISBN10 or ISBN13
|
maybe_isbn = len(isbn) in [10, 13] # ISBN10 or ISBN13
|
||||||
|
|
||||||
|
timeout = 15
|
||||||
|
start_time = datetime.now()
|
||||||
for connector in get_connectors():
|
for connector in get_connectors():
|
||||||
result_set = None
|
result_set = None
|
||||||
if maybe_isbn and connector.isbn_search_url and connector.isbn_search_url == "":
|
if maybe_isbn and connector.isbn_search_url and connector.isbn_search_url != "":
|
||||||
# Search on ISBN
|
# Search on ISBN
|
||||||
try:
|
try:
|
||||||
result_set = connector.isbn_search(isbn)
|
result_set = connector.isbn_search(isbn)
|
||||||
|
@ -59,6 +62,8 @@ def search(query, min_confidence=0.1, return_first=False):
|
||||||
"results": result_set,
|
"results": result_set,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
if (datetime.now() - start_time).seconds >= timeout:
|
||||||
|
break
|
||||||
|
|
||||||
if return_first:
|
if return_first:
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in a new issue