Files
serpbear/scrapers/services/serpapi.ts
towfiqi 3719f21d98 feat: Adds the ability for city level scraping for scapers that allow it.
- Only available for scrapers that allows custom location or city level scraping.
- When a city level keyword is added the city name is displayed in the keyword title.

closes #139, #151
2024-02-06 13:22:32 +06:00

44 lines
1.3 KiB
TypeScript

import countries from '../../utils/countries';
interface SerpApiResult {
title: string,
link: string,
position: number,
}
const serpapi:ScraperSettings = {
id: 'serpapi',
name: 'SerpApi.com',
website: 'serpapi.com',
allowsCity: true,
headers: (keyword, settings) => {
return {
'Content-Type': 'application/json',
'X-API-Key': settings.scaping_api,
};
},
scrapeURL: (keyword, settings) => {
const countryName = countries[keyword.country || 'US'][0];
const location = keyword.city && keyword.country ? `&location=${encodeURI(`${keyword.city},${countryName}`)}` : '';
return `https://serpapi.com/search?q=${encodeURI(keyword.keyword)}&num=100&gl=${keyword.country}&device=${keyword.device}${location}&api_key=${settings.scaping_api}`;
},
resultObjectKey: 'organic_results',
serpExtractor: (content) => {
const extractedResult = [];
const results: SerpApiResult[] = (typeof content === 'string') ? JSON.parse(content) : content as SerpApiResult[];
for (const { link, title, position } of results) {
if (title && link) {
extractedResult.push({
title,
url: link,
position,
});
}
}
return extractedResult;
},
};
export default serpapi;