Created
October 18, 2016 07:56
-
-
Save malcolmyu/c558426bee902d509eaaf583ef8d5d59 to your computer and use it in GitHub Desktop.
download emoticons
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 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