Skip to content

Instantly share code, notes, and snippets.

@Juxsta
Last active February 11, 2023 05:55
Show Gist options
  • Save Juxsta/51c03e493313f0ca19c241d53b4ea1ed to your computer and use it in GitHub Desktop.
Save Juxsta/51c03e493313f0ca19c241d53b4ea1ed to your computer and use it in GitHub Desktop.
import {google} from 'googleapis'
const searchEngine = google.customsearch({
version: 'v1',
auth: process.env.GOOGLE_API_KEY,
});
const SITES_TO_SEARCH = ["youtube.com", "twitter.com", "facebook.com", "tiktok.com", "instagram.com", "medium.com"];
const search = async ( keyword: string, organization: string, site?:string,) => {
const results = await searchEngine.cse.list({
q: `${keyword} "${organization}"`,
cx: process.env.GOOGLE_SEARCH_ENGINE_ID,
num: 3,
siteSearch: site,
siteSearchFilter: 'i',
});
// return an array of links
return results.data.items?.map((item) => item.link).filter((link) => link) || [];
};
const getLinksForAllSites = async (keyword: string, organization: string) => {
let promises = SITES_TO_SEARCH.map((site) => search(site, keyword, organization))
// search using default google search
promises.push(search(keyword, organization))
const links = await Promise.all(promises);
return links.flat();
}
@jinocenc
Copy link

so clean; LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment