Skip to content

Instantly share code, notes, and snippets.

@YarivGilad
Created July 21, 2020 06:07
Show Gist options
  • Save YarivGilad/c844dffcdccf16ca1be9905b9704f72a to your computer and use it in GitHub Desktop.
Save YarivGilad/c844dffcdccf16ca1be9905b9704f72a to your computer and use it in GitHub Desktop.
// ---------------------------------
// write data to a file
// ---------------------------------
// import fs from 'fs';
// const heros = [
// { name: "Tyrion Lannister", minutes: 166, seconds: 15, episodes: 28, family: "Lannister", url: "http://gameofthrones.wikia.com/wiki/Tyrion_Lannister" },
// { name: "Daenerys Targaryen", minutes: 127, seconds: 35, episodes: 25, family: "Targaryen", url: "http://gameofthrones.wikia.com/wiki/Daenerys_Targaryen"},
// { name: "Jon Snow", minutes: 126, seconds: 41, episodes: 24, family: "Night's Watch", url: "http://gameofthrones.wikia.com/wiki/Jon_Snow" },
// { name: "Arya Stark", minutes: 100, seconds: 11, episodes: 27, family: "Stark", url: "http://gameofthrones.wikia.com/wiki/Arya_Stark"},
// { name: "Eddard Stark", minutes: 94, seconds: 35, episodes: 9, family: "Stark", url: "http://gameofthrones.wikia.com/wiki/Eddard_Stark" },
// { name: "Catelyn Stark", minutes: 91, seconds: 14, episodes: 25, family: "Stark", url: "http://gameofthrones.wikia.com/wiki/Catelyn_Stark"},
// { name: "Cersei Lannister", minutes: 90, seconds: 2, episodes: 27, family: "Lannister", url: "http://gameofthrones.wikia.com/wiki/Cersei_Lannister" },
// { name: "Robb Stark", minutes: 89, seconds: 16, episodes: 22, family: "Stark", url: "http://gameofthrones.wikia.com/wiki/Robb_Stark"},
// { name: "Sansa Stark", minutes: 85, seconds: 42, episodes: 24, family: "Stark", url: "http://gameofthrones.wikia.com/wiki/Sansa_Stark" },
// ];
// const wstream = fs.createWriteStream('./data/heros.json');
// wstream.write(JSON.stringify(heros,null,2));
// wstream.end();
//---------------------------------
// download an image
//---------------------------------
import fs from 'fs';
import fetch from 'node-fetch';
import log from '@ajar/marker'
// const url = 'https://www.rivernetwork.org/wp-content/uploads/2015/08/Our-Work-landing-page-big-image.jpg';
// const writeStream = fs.createWriteStream('./images/big-image.jpg');
// const url = 'https://joannachayes.files.wordpress.com/2015/06/nightsking.jpg';
// const writeStream = fs.createWriteStream('./images/nights-king.jpg')
const url = 'http://orig14.deviantart.net/dbcb/f/2016/152/b/c/white_walker_4k_by_blaufosc-da4n3yi.jpg';
const writeStream = fs.createWriteStream('./images/white-walker.jpg');
// (async ()=> {
// const response = await fetch(url);
// // fetch() returns a promise that
// // resolves once headers have been received
// log.obj(response.headers,'headers:');
// const total = Number(response.headers.get('content-length'));
// // response.body is a readable stream.
// const readStream = response.body;
// let loaded = 0;
// readStream.on('data', data => {
// loaded += data.length;
// const progress = ((loaded / total) * 100).toFixed(2);
// log.v(`${progress}%`);
// writeStream.write(data)
// });
// readStream.on('end', ()=> {
// log.info(`Finished fetching ${loaded} bytes`);
// writeStream.end()
// });
// readStream.on('error', err => {
// log.info(`Error: ${err.message}`);
// writeStream.close();
// });
// })().catch(log.error)
(async ()=> {
const response = await fetch(url);
const total = Number(response.headers.get('content-length'));
let loaded = 0;
for await(const data of response.body) {
loaded += data.length;
const progress = ((loaded / total) * 100).toFixed(2);
log.v(`${progress}%`);
writeStream.write(data)
}
log.info(`Finished fetching ${loaded} bytes`);
})().catch(log.error)
//--------------------------------------
// in the browser you could...
//--------------------------------------
// (async ()=> {
// const response = await fetch(url);
// const total = Number(response.headers.get('content-length'));
// let loaded = 0;
// for await(const {length} of response.body.getReader()) {
// loaded += length;
// const progress = ((loaded / total) * 100).toFixed(2);
// log.v(`${progress}%`); // or yourDiv.textContent = `${progress}%`;
// }
// log.info(`Finished fetching ${loaded} bytes`);
// })().catch(err=>log.error(err))
//--------------------------------------
// stream a response in express
//--------------------------------------
// router.get('/visual',async (req,res)=> {
// // response.body is a readable stream.
// // res is a writable stream
// const {url} = req.query;
// const response = await fetch(url);
// response.body.pipe(res);
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment