Skip to content

Instantly share code, notes, and snippets.

@malcolmyu
Created October 18, 2016 07:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcolmyu/c558426bee902d509eaaf583ef8d5d59 to your computer and use it in GitHub Desktop.
Save malcolmyu/c558426bee902d509eaaf583ef8d5d59 to your computer and use it in GitHub Desktop.
download emoticons
var https = require('https')
var http = require('http')
var fs = require('fs')
var path = require('path')
var url = 'https://raw.githubusercontent.com/spacelan/wechat-emoticon/master/emoticons.json'
https.get(url, function(res) {
var data = ''
res.setEncoding('utf8')
res.on('data', function(chunk) {
data += chunk
})
res.on('end', function() {
download(JSON.parse(data))
})
})
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('下载图片完成')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment