Uses one set of search logic for all results or just first

This commit is contained in:
Mouse Reeve 2021-05-10 09:57:53 -07:00
parent c005e458fa
commit f2d985e583

View file

@ -19,7 +19,7 @@ class ConnectorException(HTTPError):
"""when the connector can't do what was asked""" """when the connector can't do what was asked"""
def search(query, min_confidence=0.1): def search(query, min_confidence=0.1, return_first=False):
"""find books based on arbitary keywords""" """find books based on arbitary keywords"""
if not query: if not query:
return [] return []
@ -51,6 +51,9 @@ def search(query, min_confidence=0.1):
logger.exception(e) logger.exception(e)
continue continue
if return_first and result_set:
return result_set[0]
results.append( results.append(
{ {
"connector": connector, "connector": connector,
@ -58,6 +61,9 @@ def search(query, min_confidence=0.1):
} }
) )
if return_first:
return None
return results return results
@ -77,12 +83,7 @@ def isbn_local_search(query, raw=False):
def first_search_result(query, min_confidence=0.1): def first_search_result(query, min_confidence=0.1):
"""search until you find a result that fits""" """search until you find a result that fits"""
for connector in get_connectors(): return search(query, min_confidence=min_confidence, return_first=True)
result = connector.search(query, min_confidence=min_confidence)
if result:
return result[0]
return None
def get_connectors(): def get_connectors():
"""load all connectors""" """load all connectors"""