Last active
May 16, 2020 16:20
-
-
Save dezoito/42252dcfacafd20a13c8e0a31942a5cb 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
// Full article published here: | |
// http://dezoito.github.io/2020/05/16/node-asynchronous-list-searches-with-bing-api.html | |
// Bing Search Configs | |
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials; | |
const WebSearchAPIClient = require('azure-cognitiveservices-websearch'); | |
const BING_SEARCH_API_KEY = '<<your_subscription_key_here>>'; | |
// aux: generates pseudorandom numbers within a range | |
const randomIntFromInterval = (min, max) => { // min and max included | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
// aux: async sleep | |
const sleep = (milliseconds) => { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
} | |
// list of search terms/wish-list | |
const myList = ['Lagavulin Offerman Edition', | |
'Laphroaig Lore', | |
'Laphroaig PX CAsk', | |
'Hibiki Japanese Harmony', | |
'Jameson Caskmates', | |
'Caol Ila Distillers Edition', | |
'Aberlour A\'bunadh', | |
'Nikka Coffey Whisky', | |
'Glenfiddich Vintage Cask', | |
'Glenfiddich 18', | |
'Oban Little Bay']; | |
/* | |
* Defines logic for a single search against BING's API | |
*/ | |
const asyncSearchBing = async (entity) => { | |
console.log(`Searching BING for ${entity}`); | |
let credentials = new CognitiveServicesCredentials(BING_SEARCH_API_KEY); | |
let webSearchApiClient = new WebSearchAPIClient(credentials); | |
try { | |
await sleep(randomIntFromInterval(600, 800)); | |
const response = await webSearchApiClient.web.search(`${entity}`, { | |
market: "en-GB", | |
location: 'lat: 55.953251, long: -3.188267', | |
count: 5, | |
}) | |
var data = { | |
entity, | |
source: "Bing", | |
data: response["webPages"]// return only webPage results | |
}; | |
} catch (error) { | |
console.error(`Error seaarching entity ${entity}`) | |
console.error(error); | |
var data = { | |
entity, | |
source: "Bing", | |
data | |
}; | |
} | |
return data; | |
} | |
/* | |
* Runs async searches using the previously defined search functions | |
*/ | |
const runSearch = async (list) => { | |
const promises = [] | |
promises.push(...list.map(async (entity) => asyncSearchBing(entity))); | |
// promises.push(...list.map(async (entity) => asyncSearchAnotherAPI(entity))); | |
// executes all promisses asynchronously | |
const getData = async () => { | |
return await Promise.all( promises ) | |
} | |
return await getData(); | |
} | |
runSearch(myList) | |
.then(results => console.log(results)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment