Created
September 30, 2011 15:23
-
-
Save ishiduca/1254094 to your computer and use it in GitHub Desktop.
pixivへのログインから画像ダウンロード #Node.js #HttpClient
This file contains hidden or 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 HttpClient = require('../lib/httpClient.js').HttpClient; | |
var client = new HttpClient (); | |
var body = require('querystring').stringify({ | |
mode : 'login', pixiv_id : 'your pixiv id', pass : 'your pixiv pass' | |
}); | |
var pixivHome = 'http://www.pixiv.net'; | |
var loginPhp = [ pixivHome, '/login.php' ].join(''); | |
var illust_top = [ pixivHome, '/member_illust.php?mode=medium&illust_id=22089162' ].join(''); | |
client.request('POST', loginPhp, { 'connection' : 'keep-alive' }, body, function (res, req) { | |
client.request('GET', illust_top, function (res, req) { | |
res.setEncoding('utf-8'); | |
var data = ''; | |
res.on('data', function (chunk) { data += chunk; }); | |
res.on('end', function () { | |
data = data.replace(/\n/g, ''); | |
var pattern = /<a href="(member_illust\.php\?mode=[^"]+?)"[^>]+?><img src="([^"]+?)".+?title="([^"]+?)"/; | |
var result = pattern.exec(data); | |
//var contentsURL = unescapeHTML([ pixivHome, result[1] ].join('/')); | |
var imgSrc = unescapeHTML(result[2]); | |
//var title = result[3]; | |
client.request('GET', imgSrc, function (res, req) { | |
var target = require('path').basename(imgSrc); | |
var writeStream = require('fs').createWriteStream(target); | |
writeStream.on('error', function (exception) { | |
throw exception; | |
}); | |
writeStream.on('close', function () { | |
console.log('writeStream say "close"'); | |
}); | |
res.on('data', function (chunk) { | |
writeStream.write(chunk); | |
}); | |
res.on('end', function () { | |
writeStream.end(); | |
console.log('response say "end"'); | |
}); | |
}); | |
}); | |
}); | |
}); | |
function unescapeHTML (str) { | |
var chas = [ | |
['&', '&'], | |
['>', '>'], | |
['<', '<'], | |
['"', '"'] | |
]; | |
chas.forEach(function (cha) { | |
var c = cha[0]; | |
str = str.replace(new RegExp(cha[0], "g"), cha[1]); | |
}); | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment