-
Notifications
You must be signed in to change notification settings - Fork 638
/
image.js
84 lines (65 loc) · 2.47 KB
/
image.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const imageDownloader = require('image-downloader')
const google = require('googleapis').google
const customSearch = google.customsearch('v1')
const state = require('./state.js')
const googleSearchCredentials = require('../credentials/google-search.json')
async function robot() {
console.log('> [image-robot] Starting...')
const content = state.load()
await fetchImagesOfAllSentences(content)
await downloadAllImages(content)
state.save(content)
async function fetchImagesOfAllSentences(content) {
for (let sentenceIndex = 0; sentenceIndex < content.sentences.length; sentenceIndex++) {
let query
if (sentenceIndex === 0) {
query = `${content.searchTerm}`
} else {
query = `${content.searchTerm} ${content.sentences[sentenceIndex].keywords[0]}`
}
console.log(`> [image-robot] Querying Google Images with: "${query}"`)
content.sentences[sentenceIndex].images = await fetchGoogleAndReturnImagesLinks(query)
content.sentences[sentenceIndex].googleSearchQuery = query
}
}
async function fetchGoogleAndReturnImagesLinks(query) {
const response = await customSearch.cse.list({
auth: googleSearchCredentials.apiKey,
cx: googleSearchCredentials.searchEngineId,
q: query,
searchType: 'image',
num: 2
})
const imagesUrl = response.data.items.map((item) => {
return item.link
})
return imagesUrl
}
async function downloadAllImages(content) {
content.downloadedImages = []
for (let sentenceIndex = 0; sentenceIndex < content.sentences.length; sentenceIndex++) {
const images = content.sentences[sentenceIndex].images
for (let imageIndex = 0; imageIndex < images.length; imageIndex++) {
const imageUrl = images[imageIndex]
try {
if (content.downloadedImages.includes(imageUrl)) {
throw new Error('Image already downloaded')
}
await downloadAndSave(imageUrl, `${sentenceIndex}-original.png`)
content.downloadedImages.push(imageUrl)
console.log(`> [image-robot] [${sentenceIndex}][${imageIndex}] Image successfully downloaded: ${imageUrl}`)
break
} catch(error) {
console.log(`> [image-robot] [${sentenceIndex}][${imageIndex}] Error (${imageUrl}): ${error}`)
}
}
}
}
async function downloadAndSave(url, fileName) {
return imageDownloader.image({
url: url,
dest: `./content/${fileName}`
})
}
}
module.exports = robot