Last active
February 11, 2023 05:55
-
-
Save Juxsta/51c03e493313f0ca19c241d53b4ea1ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so clean; LGTM 👍