Skip to content

Instantly share code, notes, and snippets.

@albertBuenaventura
Last active October 4, 2023 02:33
Show Gist options
  • Select an option

  • Save albertBuenaventura/384f45b6af087dacf0e93ab3fe05ce99 to your computer and use it in GitHub Desktop.

Select an option

Save albertBuenaventura/384f45b6af087dacf0e93ab3fe05ce99 to your computer and use it in GitHub Desktop.
greet-downloader
const fs = require('fs');
const https = require('https');
const { forEach } = require('lodash');
const download = (url, dest, cb) => {
const file = fs.createWriteStream(dest);
const request = https.get(url, (response) => {
if (response.statusCode !== 200) {
return cb('Response status was ' + response.statusCode);
}
response.pipe(file);
});
file.on('finish', () => file.close(cb));
request.on('error', (err) => {
fs.unlink(dest, () => cb(err.message)); // delete the (partial) file and then return the error
});
file.on('error', (err) => {
fs.unlink(dest, () => cb(err.message)); // delete the (partial) file and then return the error
});
};
const dir = './greet-video';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const downloadVideos = (data) => {
const greetData = JSON.parse(data).data;
greetData.forEach((greetResponse) => {
console.log(greetResponse.greet.video_url);
download(
greetResponse.greet.video_url,
`./greet-video/${greetResponse.profile.email}.mp4`,
(err) => console.log(err)
);
});
};
const options = {
hostname: 'www.bonjoro.com',
path: '/api/v2/greets/results/?date_filter=30d&sort=-last_modified&simple=1&limit=100&page=1',
headers: {
Authorization: `Bearer ${addTokenHere}`,
Accept: 'application/json',
},
};
https
.get(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
downloadVideos(data);
});
})
.on('error', (err) => {
console.log('Error: ' + err.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment