Created
December 23, 2022 15:45
-
-
Save kurtroberts/54a9286d63394d12c99fde03fe858cef to your computer and use it in GitHub Desktop.
Create QR codes from a google sheet of URLS and save them locally
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
//sheet should be formatted in 2 columns: name, url | |
require('dotenv').config(); | |
let { GoogleSpreadsheet } = require('google-spreadsheet'), | |
doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID), | |
Bottleneck = require('bottleneck'), | |
limiter = new Bottleneck({ | |
minTime: 500, | |
maxConcurrent: 1 | |
}), | |
axios = require('axios'), | |
fs = require('fs'), | |
saveImage = function (url, name) { | |
console.log(`https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${url}`); | |
return axios.request({ | |
url: `https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${url}`, | |
responseType: 'arraybuffer', | |
method: 'get' | |
}) | |
.then(resp => { | |
name = name.replace(/\s/g, ''); | |
fs.writeFileSync(`${name}.png`, resp.data); | |
}); | |
}; | |
doc.useServiceAccountAuth({ | |
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, | |
private_key: process.env.GOOGLE_PRIVATE_KEY_ONELINE.replace(/\\n/g, "\n") | |
}) | |
.then(() => doc.loadInfo()) | |
.then(() => { | |
return doc.sheetsByIndex[0].getRows().then(rows => { | |
let tasks = []; | |
console.log(`Got ${rows.length} rows.`); | |
rows.forEach(function (x) { | |
let data = x._rawData, | |
name = data.shift(), | |
url = data.shift(); | |
console.log(name); | |
tasks.push(limiter.schedule(() => saveImage(url, name))); | |
}); | |
return Promise.all(tasks); | |
}); | |
}) | |
.then(res => console.log(res)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment