Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GCLOUD_PROJECT_ID=
GCLOUD_KEY_FILENAME=
QUANTITY=3
RESOLUTION=1024/576
request.get('http://example.com', (err, res, body) => {
if (!error && res.statusCode == 200) {
const content = body.replace(/(<([^>]+)>)/ig, '')
fs.writeFile('contents.txt', content, (err) => {
console.log('contents are saved.')
})
}
})
request.get('http://example.com')
.then(stripTags)
.then(writeToFile)
.then(console.log)
.catch(console.error)
'use strict'
require('dotenv').config({ silent: true })
const Exec = require('child_process').exec
const Fs = require('fs')
const Request = require('request')
const Vision = require('@google-cloud/vision')
const visionClient = Vision({
projectId: process.env.GCLOUD_PROJECT_ID,
keyFilename: process.env.GCLOUD_KEY_FILENAME
})
const imageUrl = (id) => `https://unsplash.it/${process.env.RESOLUTION}?image=${id}`
const imageFilename = (id) => `img-${id}.jpg`
const sample = (collection, quantity = 1) => {
const samples = []
const max = quantity - 1
for (let i = 0; i <= max; i++) {
const index = Math.ceil(Math.random() * collection.length -1)
samples.push(collection[index])
}
return samples
}
const applyLabels = (img, cb) => {
const labels = img.labels.slice(0, 3)
const command = `
convert ${imageFilename(img.id)} -pointsize 30 -font 'DejaVu-Sans-Bold' -strokewidth 2 \
-draw "gravity north stroke black fill white text 0,20 '${labels.join(' | ')}' " \
-draw "gravity south stroke black fill white text 0,20 'by ${img.author}' " \
${imageFilename(img.id)}
`
Exec(command, cb)
}
// request full image list from unsplash.it
Request.get('https://unsplash.it/list', (err, res, body) => {
if (err) {
return console.error(err)
}
// get some samples
const json = JSON.parse(body)
const images = sample(json, process.env.QUANTITY)
// process each image...
images.forEach((img) => {
// download for local filesystem
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', () => {
// send image to Google Cloud Vision to get image labels
visionClient.detectLabels(imageUrl(img.id), (err, labels, apiRes) => {
if (err) {
return console.error(err)
}
const data = Object.assign(img, { labels })
// add text to images with image magic
applyLabels(data, (err) => {
if (err) {
return console.error(err)
}
})
})
})
})
})
const Promise = require('bluebird')
const Exec = Promise.promisify(require('child_process').exec)
//...
const applyLabels = (data) => {
//...
return Exec(command)
}
//...
// request full image list from unsplash.it
Request.get('https://unsplash.it/list')
.then((body) => {
// get some samples
const json = JSON.parse(body)
const images = sample(json, process.env.QUANTITY)
// process each image...
images.forEach((img) => {
// download for local filesystem
new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
//send image to Google Cloud Vision to get image labels
.then(() => visionClient.detectLabels(imageUrl(img.id)))
.then(([labels]) => {
const data = Object.assign(img, { labels })
// add text to images with image magic
return applyLabels(data)
})
.catch(console.error)
})
})
.catch(console.error)
'use strict'
require('dotenv').config({ silent: true })
const Fs = require('fs')
const Promise = require('bluebird')
const Request = require('request-promise')
const Vision = require('@google-cloud/vision')
const Exec = Promise.promisify(require('child_process').exec)
const visionClient = Vision({
projectId: process.env.GCLOUD_PROJECT_ID,
keyFilename: process.env.GCLOUD_KEY_FILENAME
})
const imageUrl = (id) => `https://unsplash.it/${process.env.RESOLUTION}?image=${id}`
const imageFilename = (id) => `img-${id}.jpg`
const sample = (collection, quantity = 1) => {
const samples = []
const max = quantity - 1
for (let i = 0; i <= max; i++) {
const index = Math.ceil(Math.random() * collection.length -1)
samples.push(collection[index])
}
return samples
}
const applyLabels = (img) => {
const labels = img.labels.slice(0, 3)
const command = `
convert ${imageFilename(img.id)} -pointsize 30 -font 'DejaVu-Sans-Bold' -strokewidth 2 \
-draw "gravity north stroke black fill white text 0,20 '${labels.join(' | ')}' " \
-draw "gravity south stroke black fill white text 0,20 'by ${img.author}' " \
${imageFilename(img.id)}
`
return Exec(command)
}
// request full image list from unsplash.it
Request.get('https://unsplash.it/list')
.then((body) => {
// get some samples
const json = JSON.parse(body)
const images = sample(json, process.env.QUANTITY)
// process each image...
images.forEach((img) => {
// download for local filesystem
new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
//send image to Google Cloud Vision to get image labels
.then(() => visionClient.detectLabels(imageUrl(img.id)))
.then(([labels]) => {
const data = Object.assign(img, { labels })
// add text to images with image magic
return applyLabels(data)
})
.catch(console.error)
})
})
.catch(console.error)
// request full image list from unsplash.it
Request.get('https://unsplash.it/list')
.then((body) => {
// get some samples
const json = JSON.parse(body)
const images = sample(json, process.env.QUANTITY)
// process each image...
return images.map((img) => {
// download for local filesystem
return new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
.then(() => img)
})
})
.then((promises) => Promise.all(promises))
//send images to Google Cloud Vision to get image labels
.then((images) => {
return images.map((img) => {
return visionClient.detectLabels(imageUrl(img.id))
.then(([labels]) => Object.assign(img, { labels }))
})
})
.then((promises) => Promise.all(promises))
//add text to images with image magic
.then((images) => {
return images.map((img) => applyLabels(img))
})
.catch(console.error)
'use strict'
require('dotenv').config({ silent: true })
const Fs = require('fs')
const Promise = require('bluebird')
const Request = require('request-promise')
const Vision = require('@google-cloud/vision')
const Exec = Promise.promisify(require('child_process').exec)
const visionClient = Vision({
projectId: process.env.GCLOUD_PROJECT_ID,
keyFilename: process.env.GCLOUD_KEY_FILENAME
})
const imageUrl = (id) => `https://unsplash.it/${process.env.RESOLUTION}?image=${id}`
const imageFilename = (id) => `img-${id}.jpg`
const sample = (collection, quantity = 1) => {
const samples = []
const max = quantity - 1
for (let i = 0; i <= max; i++) {
const index = Math.ceil(Math.random() * collection.length -1)
samples.push(collection[index])
}
return samples
}
const applyLabels = (img) => {
const labels = img.labels.slice(0, 3)
const command = `
convert ${imageFilename(img.id)} -pointsize 30 -font 'DejaVu-Sans-Bold' -strokewidth 2 \
-draw "gravity north stroke black fill white text 0,20 '${labels.join(' | ')}' " \
-draw "gravity south stroke black fill white text 0,20 'by ${img.author}' " \
${imageFilename(img.id)}
`
return Exec(command)
}
// request full image list from unsplash.it
Request.get('https://unsplash.it/list')
.then((body) => {
// get some samples
const json = JSON.parse(body)
const images = sample(json, process.env.QUANTITY)
// process each image...
return images.map((img) => {
// download for local filesystem
return new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
.then(() => img)
})
})
.then((promises) => Promise.all(promises))
//send images to Google Cloud Vision to get image labels
.then((images) => {
return images.map((img) => {
return visionClient.detectLabels(imageUrl(img.id))
.then(([labels]) => Object.assign(img, { labels }))
})
})
.then((promises) => Promise.all(promises))
//add text to images with image magic
.then((images) => {
return images.map((img) => applyLabels(img))
})
.catch(console.error)
//...
const requestImageList = (url) => Request.get(url)
//...
requestImageList('https://unsplash.it/list')
.then((body) => JSON.parse(body))
.then((json) => sample(json, process.env.QUANTITY))
.then((images) => images.map(downloadImage))
.then((promises) => Promise.all(promises))
.then((images) => images.map(detectLabels))
.then((promises) => Promise.all(promises))
.then((images) => images.map(applyLabels))
.catch(console.error)
'use strict'
require('dotenv').config({ silent: true })
const Fs = require('fs')
const Promise = require('bluebird')
const Request = require('request-promise')
const Vision = require('@google-cloud/vision')
const Exec = Promise.promisify(require('child_process').exec)
const visionClient = Vision({
projectId: process.env.GCLOUD_PROJECT_ID,
keyFilename: process.env.GCLOUD_KEY_FILENAME
})
const imageUrl = (id) => `https://unsplash.it/${process.env.RESOLUTION}?image=${id}`
const imageFilename = (id) => `img-${id}.jpg`
const sample = (collection, quantity = 1) => {
const samples = []
const max = quantity - 1
for (let i = 0; i <= max; i++) {
const index = Math.ceil(Math.random() * collection.length -1)
samples.push(collection[index])
}
return samples
}
const requestImageList = (url) => Request.get(url)
const downloadImage = (img) => {
return new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
.then(() => img)
}
const detectLabels = (img) => {
return visionClient.detectLabels(imageUrl(img.id))
.then(([labels]) => Object.assign(img, { labels }))
}
const applyLabels = (img) => {
const labels = img.labels.slice(0, 3)
const command = `
convert ${imageFilename(img.id)} -pointsize 30 -font 'DejaVu-Sans-Bold' -strokewidth 2 \
-draw "gravity north stroke black fill white text 0,20 '${labels.join(' | ')}' " \
-draw "gravity south stroke black fill white text 0,20 'by ${img.author}' " \
${imageFilename(img.id)}
`
return Exec(command)
}
requestImageList('https://unsplash.it/list')
.then((body) => JSON.parse(body))
.then((json) => sample(json, process.env.QUANTITY))
.then((images) => images.map(downloadImage))
.then((promises) => Promise.all(promises))
.then((images) => images.map(detectLabels))
.then((promises) => Promise.all(promises))
.then((images) => images.map(applyLabels))
.catch(console.error)
//...
const R = require('ramda')
//...
const sample = R.curry((quantity, collection) => {
//...
})
//...
requestImageList('https://unsplash.it/list')
.then(JSON.parse)
.then(sample(process.env.QUANTITY))
.then(R.map(downloadImage))
.then(Promise.all)
.then(R.map(detectLabels))
.then(Promise.all)
.then(R.map(applyLabels))
.catch(console.error)
'use strict'
require('dotenv').config({ silent: true })
const Fs = require('fs')
const Promise = require('bluebird')
const R = require('ramda')
const Request = require('request-promise')
const Vision = require('@google-cloud/vision')
const Exec = Promise.promisify(require('child_process').exec)
const visionClient = Vision({
projectId: process.env.GCLOUD_PROJECT_ID,
keyFilename: process.env.GCLOUD_KEY_FILENAME
})
const imageUrl = (id) => `https://unsplash.it/${process.env.RESOLUTION}?image=${id}`
const imageFilename = (id) => `img-${id}.jpg`
const sample = R.curry((quantity, collection) => {
const samples = []
const max = quantity - 1
for (let i = 0; i <= max; i++) {
const index = Math.ceil(Math.random() * collection.length -1)
samples.push(collection[index])
}
return samples
})
const requestImageList = (url) => Request.get(url)
const downloadImage = (img) => {
return new Promise((resolve, reject) => {
Request.get(imageUrl(img.id))
.pipe(Fs.createWriteStream(imageFilename(img.id)))
.on('close', resolve)
.on('error', reject)
})
.then(() => img)
}
const detectLabels = (img) => {
return visionClient.detectLabels(imageUrl(img.id))
.then(([labels]) => Object.assign(img, { labels }))
}
const applyLabels = (img) => {
const labels = img.labels.slice(0, 3)
const command = `
convert ${imageFilename(img.id)} -pointsize 30 -font 'DejaVu-Sans-Bold' -strokewidth 2 \
-draw "gravity north stroke black fill white text 0,20 '${labels.join(' | ')}' " \
-draw "gravity south stroke black fill white text 0,20 'by ${img.author}' " \
${imageFilename(img.id)}
`
return Exec(command)
}
requestImageList('https://unsplash.it/list')
.then(JSON.parse)
.then(sample(process.env.QUANTITY))
.then(R.map(downloadImage))
.then(Promise.all)
.then(R.map(detectLabels))
.then(Promise.all)
.then(R.map(applyLabels))
.catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment