-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageSearch.js
More file actions
67 lines (57 loc) · 1.53 KB
/
ImageSearch.js
File metadata and controls
67 lines (57 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
require('dotenv').config()
let https = require('https');
let subscriptionKey = process.env.BING_SEARCH;
let host = 'api.cognitive.microsoft.com';
let path = '/bing/v7.0/images/search';
let totalImages = 10;
let bing_image_search = function (search, callback) {
let request_params = {
method : 'GET',
hostname : host,
path : path + '?q=' + encodeURIComponent(search) + "&count=" + totalImages,
headers : {
'Ocp-Apim-Subscription-Key' : subscriptionKey,
}
};
let req = https.request(request_params, function (response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
body = JSON.parse(body);
callback(body);
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
});
req.end();
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function searchLyrics(lyrics, callback) {
var count = 0;
lyrics.documents.forEach (line => {
line["urls"] = {};
line.keyPhrases.forEach (word => {
var random = getRandomInt(totalImages);
bing_image_search(word, (result) => {
let url = result.value[random].thumbnailUrl;
line.urls[word] = url;
});
});
count++;
});
setTimeout(() => {
callback(lyrics)
}, count*1000);
}
/*searchLyrics(ex, (finalLyrics) => {
console.log(JSON.stringify(finalLyrics, null, 2));
}); */
module.exports = {
getImages: searchlyrics
};