-
-
Save bumfo/8892810615f6f4d74b876beebf79ffec to your computer and use it in GitHub Desktop.
wechat emoticons dl
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
var https = require('https') | |
var http = require('http') | |
var fs = require('fs') | |
var path = require('path') | |
var list = require('./emoticons.json') | |
var threads = 5 | |
var chunck = Math.ceil(list.length / threads) | |
for (var i = 0; i < threads; ++i) { | |
download(list.slice(i * chunck, (i + 1) * chunck)) | |
} | |
function loopAsync(turns, work, callback) { | |
var currentTurn = 0 | |
var isDone = false | |
var hasNext = false | |
function done() { | |
isDone = true | |
callback.apply(this, arguments) | |
} | |
function next() { | |
if (isDone) return | |
hasNext = true | |
while (!isDone && currentTurn < turns && hasNext) { | |
hasNext = false | |
work.call(this, currentTurn++, next, done) | |
} | |
if (currentTurn >= turns && hasNext) { | |
isDone = true | |
callback() | |
} | |
} | |
next() | |
} | |
function download(emoticons) { | |
loopAsync(emoticons.length, function (index, next, done) { | |
var emoticon = emoticons[index] | |
http.get(emoticon.url, function(res) { | |
var name = path.join(__dirname, emoticon.md5 + '.gif') | |
res.pipe(fs.createWriteStream(name)) | |
res.on('end', next) | |
}) | |
}, function() { | |
console.log('finished') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment