Fix search_patents route not respecting number of results per query
Browse files
app.py
CHANGED
@@ -73,15 +73,18 @@ async def query_google_patents(browser: Browser, q: str, n_results: int = 10):
|
|
73 |
PATENT_ID_REGEX = r"\b[A-Z]{2}\d{6,}(?:[A-Z]\d?)?\b"
|
74 |
|
75 |
items = await page.locator("search-result-item").all()
|
76 |
-
|
77 |
for item in items:
|
78 |
all_text = " ".join(await item.locator("span").all_inner_texts())
|
79 |
found = re.findall(PATENT_ID_REGEX, all_text)
|
80 |
if found:
|
81 |
-
|
82 |
|
83 |
await context.close()
|
84 |
-
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
async def query_brave_search(browser: Browser, q: str, n_results: int = 10):
|
@@ -102,6 +105,10 @@ async def query_brave_search(browser: Browser, q: str, n_results: int = 10):
|
|
102 |
|
103 |
results_cards = await page.locator('.snippet').all()
|
104 |
|
|
|
|
|
|
|
|
|
105 |
results = []
|
106 |
|
107 |
for result in results_cards:
|
@@ -141,11 +148,12 @@ async def search_patents(params: APISearchParams) -> APIPatentResults:
|
|
141 |
except Exception as e:
|
142 |
logging.error(
|
143 |
f"Failed to query Google Patents with query `{q}`: {e}")
|
144 |
-
return APIPatentResults(results=
|
145 |
|
146 |
|
147 |
@app.post("/search_brave")
|
148 |
async def search_brave(params: APISearchParams) -> APIBraveResults:
|
|
|
149 |
results = []
|
150 |
for q in params.queries:
|
151 |
logging.info(f"Searching Brave search with query `{q}`")
|
|
|
73 |
PATENT_ID_REGEX = r"\b[A-Z]{2}\d{6,}(?:[A-Z]\d?)?\b"
|
74 |
|
75 |
items = await page.locator("search-result-item").all()
|
76 |
+
id_matches = []
|
77 |
for item in items:
|
78 |
all_text = " ".join(await item.locator("span").all_inner_texts())
|
79 |
found = re.findall(PATENT_ID_REGEX, all_text)
|
80 |
if found:
|
81 |
+
id_matches.append(found[0])
|
82 |
|
83 |
await context.close()
|
84 |
+
|
85 |
+
patents = [{"href": f"https://patents.google.com/patent/{id}/en", "id": id}
|
86 |
+
for id in id_matches]
|
87 |
+
return patents[:n_results]
|
88 |
|
89 |
|
90 |
async def query_brave_search(browser: Browser, q: str, n_results: int = 10):
|
|
|
105 |
|
106 |
results_cards = await page.locator('.snippet').all()
|
107 |
|
108 |
+
if len(results_cards) == 0:
|
109 |
+
logging.warning(f"No results for query: {q}")
|
110 |
+
logging.warning(await page.content())
|
111 |
+
|
112 |
results = []
|
113 |
|
114 |
for result in results_cards:
|
|
|
148 |
except Exception as e:
|
149 |
logging.error(
|
150 |
f"Failed to query Google Patents with query `{q}`: {e}")
|
151 |
+
return APIPatentResults(results=results, error=None)
|
152 |
|
153 |
|
154 |
@app.post("/search_brave")
|
155 |
async def search_brave(params: APISearchParams) -> APIBraveResults:
|
156 |
+
"""Searches brave search for the specified queries and returns the found documents."""
|
157 |
results = []
|
158 |
for q in params.queries:
|
159 |
logging.info(f"Searching Brave search with query `{q}`")
|