Skip to content

Instantly share code, notes, and snippets.

@bumfo

bumfo/dl.js Secret

Created October 21, 2016 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bumfo/8892810615f6f4d74b876beebf79ffec to your computer and use it in GitHub Desktop.
Save bumfo/8892810615f6f4d74b876beebf79ffec to your computer and use it in GitHub Desktop.
wechat emoticons dl
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