Skip to content

Commit 8bec20e

Browse files
committed
add geo location parameter to search engine
1 parent 7bb6bf1 commit 8bec20e

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

server.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,22 @@ addTool({
189189
cursor: z.string()
190190
.optional()
191191
.describe('Pagination cursor for next page'),
192+
geo_location: z.string()
193+
.length(2)
194+
.optional()
195+
.describe('2-letter country code for geo-targeted results '
196+
+'(e.g., "us", "uk")'),
192197
}),
193-
execute: tool_fn('search_engine', async({query, engine, cursor}, ctx)=>{
198+
execute: tool_fn('search_engine', async({query, engine, cursor,
199+
geo_location}, ctx)=>
200+
{
194201
const is_google = engine=='google';
195-
const url = search_url(engine, query, cursor);
202+
const url = search_url(engine, query, cursor, geo_location);
196203
let response = await axios({
197204
url: 'https://api.brightdata.com/request',
198205
method: 'POST',
199206
data: {
200-
url: url,
207+
url: is_google ? `${url}&brd_json=1` : url,
201208
zone: unlocker_zone,
202209
format: 'raw',
203210
data_format: is_google ? 'parsed_light' : 'markdown',
@@ -269,18 +276,25 @@ addTool({
269276
.default('google'),
270277
cursor: z.string()
271278
.optional(),
279+
geo_location: z.string()
280+
.length(2)
281+
.optional()
282+
.describe('2-letter country code for geo-targeted results '
283+
+'(e.g., "us", "uk")'),
272284
})).min(1).max(10),
273285
}),
274-
execute: tool_fn('search_engine_batch', async ({queries}, ctx)=>{
275-
const search_promises = queries.map(({query, engine, cursor})=>{
286+
execute: tool_fn('search_engine_batch', async({queries}, ctx)=>{
287+
const search_promises = queries.map(({query, engine, cursor,
288+
geo_location})=>{
276289
const is_google = (engine || 'google') === 'google';
277-
const url = search_url(engine || 'google', query, cursor);
290+
const url = search_url(engine || 'google', query, cursor,
291+
geo_location);
278292

279293
return axios({
280294
url: 'https://api.brightdata.com/request',
281295
method: 'POST',
282296
data: {
283-
url,
297+
url: is_google ? `${url}&brd_json=1` : url,
284298
zone: unlocker_zone,
285299
format: 'raw',
286300
data_format: is_google ? 'parsed_light' : 'markdown',
@@ -1040,13 +1054,14 @@ function clean_google_search_payload(raw_data){
10401054
return {organic: organic_clean};
10411055
}
10421056

1043-
function search_url(engine, query, cursor){
1057+
function search_url(engine, query, cursor, geo_location){
10441058
let q = encodeURIComponent(query);
10451059
let page = cursor ? parseInt(cursor) : 0;
10461060
let start = page * 10;
10471061
if (engine=='yandex')
10481062
return `https://yandex.com/search/?text=${q}&p=${page}`;
10491063
if (engine=='bing')
10501064
return `https://www.bing.com/search?q=${q}&first=${start + 1}`;
1051-
return `https://www.google.com/search?q=${q}&start=${start}`;
1065+
let gl = geo_location ? `&gl=${geo_location}` : '';
1066+
return `https://www.google.com/search?q=${q}&start=${start}${gl}`;
10521067
}

0 commit comments

Comments
 (0)