-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nodejs: add a nodejs version (with: future, stream, ...).
- Loading branch information
Lionel Atty
committed
Jan 20, 2020
1 parent
e28bff9
commit 7af6fd4
Showing
5 changed files
with
213 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const request = require('request').defaults({ | ||
pool: {maxSockets: Infinity}, | ||
timeout: 10 * 1000, | ||
}); | ||
const bluebird = require('bluebird'); | ||
const fs = require('fs-extra'); | ||
const CONCURRENCY_LEVEL = 50; | ||
|
||
async function download(url, index) { | ||
console.log(index); | ||
if (!url) { | ||
return Promise.resolve(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const r = request(url); | ||
const f = fs.createWriteStream(`cats/${index}.jpg`); | ||
r.on('error', reject); | ||
r.pipe(f); | ||
f.on('finish', resolve); | ||
f.on('error', reject); | ||
}) | ||
.catch((err) => { | ||
console.log(`Skipping error ${err.message} for ${index}`) | ||
}) | ||
} | ||
|
||
async function main() { | ||
const catsBuffer = await fs.readFile('cats.txt'); | ||
const cats = catsBuffer.toString().split('\n'); | ||
await bluebird.map(cats, download, {concurrency: CONCURRENCY_LEVEL}) | ||
} | ||
|
||
main() | ||
.then(() => console.log('End.')) | ||
.catch((err) => console.log(`Error: ${err.toString()}\n${err.stack}`)) | ||
; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "image_downloader", | ||
"version": "1.0.0", | ||
"description": "Images Downloader", | ||
"main": "image_downloader.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"bluebird": "^3.7.2", | ||
"fs-extra": "^8.1.0", | ||
"request": "^2.88.0" | ||
} | ||
} |