Skip to content

Commit 5cd1bd4

Browse files
committed
Added location header to Bing search.
1 parent 1a6e0c0 commit 5cd1bd4

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/aprs_assistant/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
if request == "quit" or request == "exit":
4040
break
4141
response = generate_reply(fromcall, request)
42-
print(f"\n{BOT_CALLSIGN}: {response}\n")
42+
print(f"\n{BOT_CALLSIGN()}: {response}\n")

src/aprs_assistant/_bing.py

+9-18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from urllib.parse import quote, quote_plus, unquote, urlparse, urlunparse
1414

1515

16-
def bing_search(query, interleave_results=True):
17-
results = _bing_api_call(query)
16+
def bing_search(query, lat=None, lon=None, interleave_results=True):
17+
results = _bing_api_call(query, lat, lon)
1818
snippets = {}
1919

2020
def _processFacts(elm):
@@ -154,12 +154,18 @@ def _processFacts(elm):
154154
return f"## A Bing search for '{query}' found {idx} results:\n\n" + content.strip()
155155

156156

157-
def _bing_api_call(query: str):
157+
def _bing_api_call(query, lat=None, lon=None):
158158
# Prepare the request parameters
159159
request_kwargs = {}
160160
request_kwargs["headers"] = {}
161161
request_kwargs["headers"]["Ocp-Apim-Subscription-Key"] = os.environ["BING_API_KEY"]
162162

163+
# Specify the user's location
164+
if lat is not None or lon is not None:
165+
if lat is None or lon is None:
166+
raise ValueError("If lat is specified, lon must also be specified.")
167+
request_kwargs["headers"]["X-Search-Location"] = f"lat:{lat};long:{lon};re:22m"
168+
163169
request_kwargs["params"] = {}
164170
request_kwargs["params"]["q"] = query
165171
request_kwargs["params"]["textDecorations"] = False
@@ -184,18 +190,3 @@ def _markdown_link(anchor, href):
184190
return f"[{anchor}]({href})"
185191
except ValueError: # It's not clear if this ever gets thrown
186192
return f"[{anchor}]({href})"
187-
188-
189-
def _bing_news_call():
190-
request_kwargs = {}
191-
request_kwargs["headers"] = {}
192-
request_kwargs["headers"]["Ocp-Apim-Subscription-Key"] = os.environ["BING_API_KEY"]
193-
request_kwargs["stream"] = False
194-
195-
# Make the request
196-
response = requests.get(
197-
"https://api.bing.microsoft.com/v7.0/news/search?q=&mkt=en-us", **request_kwargs
198-
)
199-
response.raise_for_status()
200-
results = response.json()
201-
return results

src/aprs_assistant/_bot.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@ def _generate_reply(fromcall, messages):
216216

217217
# Step 3: Call the function and retrieve results. Append the results to the messages list.
218218
if function_name == TOOL_WEB_SEARCH["function"]["name"]:
219-
results = bing_search(args["query"])
219+
if position is not None:
220+
results = bing_search(
221+
args["query"],
222+
lat=position["latitude"],
223+
lon=position["longitude"],
224+
)
225+
else:
226+
results = bing_search(args["query"])
220227

221228
elif function_name == TOOL_CALLSIGN_SEARCH["function"]["name"]:
222229
results = get_callsign_info(args["callsign"])

0 commit comments

Comments
 (0)