cheerio-httpcli
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -0,1 +1,8 @@ | ||
# 0.3.4 (2015-10-29) | ||
* 依存ライブラリを最新バージョンに更新 | ||
* POST後のリダイレクト先の指定がおかしかったのを修正(#4) | ||
* `submit()`時のパラメータ指定で数字の0を指定した場合に空扱いされてしまうのを修正 | ||
* READMEにBasic認証についてのTips追加 | ||
# 0.3.3 (2015-08-30) | ||
@@ -2,0 +9,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0, no-invalid-this:0*/ | ||
'use strict'; | ||
@@ -25,7 +25,10 @@ | ||
var $h3 = $(this).find('h3'); | ||
results.push({ | ||
title: $h3.text(), | ||
url: $h3.find('a').attr('href'), | ||
description: $(this).find('.st').text() | ||
}); | ||
var url = $h3.find('a').attr('href'); | ||
if (url) { | ||
results.push({ | ||
title: $h3.text(), | ||
url: url, | ||
description: $(this).find('.st').text() | ||
}); | ||
} | ||
}); | ||
@@ -32,0 +35,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0, no-invalid-this:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0, no-invalid-this:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
#!/usr/bin/env node | ||
/*jshint node:true */ | ||
/*eslint no-console:0*/ | ||
'use strict'; | ||
@@ -4,0 +4,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*jshint node:true */ | ||
/*eslint no-invalid-this:0*/ | ||
'use strict'; | ||
@@ -27,3 +27,3 @@ | ||
// 文字列でない場合(cheerioオブジェクトなど)はそのまま返す | ||
if (typeof(str) !== 'string') { | ||
if (typeof str !== 'string') { | ||
return str; | ||
@@ -52,3 +52,3 @@ } | ||
var $ = cheerio; | ||
var $link; | ||
var $link = null; | ||
@@ -87,3 +87,3 @@ // a要素でなければエラー | ||
var $ = cheerio; | ||
var $form; | ||
var $form = null; | ||
@@ -125,3 +125,3 @@ // form要素でなければエラー | ||
Object.keys(param).forEach(function (p) { | ||
if (! param[p]) { | ||
if (typeof param[p] !== 'number' && ! param[p]) { | ||
param[p] = ''; | ||
@@ -128,0 +128,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*jshint node:true */ | ||
/*eslint key-spacing:0, no-undefined:0*/ | ||
'use strict'; | ||
@@ -52,3 +52,3 @@ | ||
Object.keys(extra).forEach(function (ex) { | ||
if (typeof(extra[ex]) !== 'undefined') { | ||
if (typeof extra[ex] !== 'undefined') { | ||
error[ex] = extra[ex]; | ||
@@ -63,3 +63,3 @@ } | ||
Object.keys(result).forEach(function (r) { | ||
if (typeof(result[r]) !== 'undefined') { | ||
if (typeof result[r] !== 'undefined') { | ||
error[r] = result[r]; | ||
@@ -125,3 +125,3 @@ } | ||
} | ||
process.stderr.write(prettyjson.render(debugParams) + "\n\n"); | ||
process.stderr.write(prettyjson.render(debugParams) + '\n\n'); | ||
} | ||
@@ -143,3 +143,3 @@ | ||
// パラメータの調整 | ||
param.url = res.headers.location; | ||
param.uri = res.headers.location; | ||
param.method = 'GET'; | ||
@@ -166,3 +166,3 @@ delete(param.form); | ||
} else { | ||
callback(undefined, res, body); | ||
return callback(undefined, res, body); | ||
} | ||
@@ -169,0 +169,0 @@ }).bind(this)); |
@@ -1,2 +0,2 @@ | ||
/*jshint node:true */ | ||
/*eslint key-spacing:0*/ | ||
'use strict'; | ||
@@ -3,0 +3,0 @@ |
@@ -1,3 +0,3 @@ | ||
/*jshint node:true,expr:true */ | ||
/*global EscapeSJIS,EscapeEUCJP,EscapeJIS7 */ | ||
/*eslint key-spacing:0, no-unused-expressions:0*/ | ||
/*global EscapeSJIS, EscapeEUCJP, EscapeJIS7 */ | ||
'use strict'; | ||
@@ -103,3 +103,3 @@ | ||
escape: function (enc, str) { | ||
var encodeFunc; | ||
var encodeFunc = null; | ||
// とりあえず大部分を占めそうなものだけ対応 | ||
@@ -138,3 +138,3 @@ switch (enc) { | ||
} | ||
return undefined; | ||
return null; | ||
}, | ||
@@ -156,3 +156,3 @@ | ||
} | ||
return undefined; | ||
return null; | ||
}, | ||
@@ -159,0 +159,0 @@ |
{ | ||
"name": "cheerio-httpcli", | ||
"version": "0.3.3", | ||
"description": "html client module with cheerio & iconv(-lite) & promise", | ||
"version": "0.3.4", | ||
"description": "http client module with cheerio & iconv(-lite) & promise", | ||
"main": "index.js", | ||
@@ -12,17 +12,17 @@ "engines": { | ||
"ent": "^2.2.0", | ||
"iconv-lite": "^0.4.11", | ||
"iconv-lite": "^0.4.13", | ||
"jschardet": "^1.3.0", | ||
"prettyjson": "^1.1.3", | ||
"request": "^2.61.0", | ||
"request": "^2.65.0", | ||
"rsvp": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"blanket": "*", | ||
"espower-loader": "*", | ||
"intelli-espower-loader": "*", | ||
"mocha": "*", | ||
"mocha-clean": "*", | ||
"node-static": "*", | ||
"power-assert": "*", | ||
"type-of": "*" | ||
"espower-loader": "^1.0.0", | ||
"intelli-espower-loader": "^1.0.0", | ||
"istanbul": "^0.4.0", | ||
"mocha": "^2.3.3", | ||
"mocha-clean": "^0.4.0", | ||
"node-static": "^0.7.7", | ||
"power-assert": "^1.1.0", | ||
"type-of": "^2.0.1" | ||
}, | ||
@@ -32,17 +32,5 @@ "directories": { | ||
}, | ||
"config": { | ||
"blanket": { | ||
"pattern": [ | ||
"lib" | ||
], | ||
"data-cover-never": [ | ||
"node_modules", | ||
"test", | ||
"vendor" | ||
] | ||
} | ||
}, | ||
"scripts": { | ||
"test": "mocha --opts test/mocha-cov.opts > coverage.html && true || true", | ||
"hint": "jshint test lib example && true || true", | ||
"test": "istanbul cover ./node_modules/mocha/bin/_mocha -x lib/vendor/** -x index.js && true || true", | ||
"lint": "eslint test/*.js lib/*.js example/*.js && true || true", | ||
"cs": "jscs test lib example && true || true" | ||
@@ -49,0 +37,0 @@ }, |
@@ -378,2 +378,37 @@ # cheerio-httpcli - Node.js用WEBスクレイピングモジュール | ||
## Tips | ||
### Basic認証 | ||
Basic認証が必要なページには以下の二通りの方法でアクセスできます。 | ||
#### リクエストヘッダに認証情報をセット | ||
```js | ||
var client = require('cheerio-httpcli'); | ||
var user = 'hoge'; | ||
var password = 'foobarbaz'; | ||
client.headers['Authorization'] = 'Basic ' + new Buffer(user + ':' + password).toString('base64'); | ||
client.fetch('http://securet.example.com', function (err, $, res, body) { | ||
. | ||
. | ||
. | ||
// 不要になったら消去 | ||
delete(client.headers['Authorization']); | ||
}); | ||
``` | ||
#### URLに認証情報をセット | ||
```js | ||
var client = require('cheerio-httpcli'); | ||
var user = 'hoge'; | ||
var password = 'foobarbaz'; | ||
client.fetch('http://' + user + ':' + password + '@securet.example.com', function (err, $, res, body) { | ||
``` | ||
詳細は[こちら](http://qiita.com/ktty1220/items/e9e42247ede476d04ce2#comment-02b5b12c8be4f193834b) | ||
## その他 | ||
@@ -380,0 +415,0 @@ |
@@ -1,6 +0,5 @@ | ||
/*jshint node:true,strict:false */ | ||
var static = require('node-static'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var qs = require('querystring'); | ||
var nstatic = require('node-static'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var qs = require('querystring'); | ||
@@ -59,6 +58,7 @@ /** | ||
var _this = this; | ||
var file = new static.Server(this.root, { | ||
var _traceRoute = []; | ||
var file = new nstatic.Server(this.root, { | ||
gzip: true | ||
}); | ||
return http.createServer(function (req, res) { | ||
var server = http.createServer(function (req, res) { | ||
var pdata = ''; | ||
@@ -69,2 +69,3 @@ req.on('data', function (data) { | ||
req.on('end', function () { | ||
_traceRoute.push(req.url); | ||
if (/~info/.test(req.url)) { | ||
@@ -112,2 +113,9 @@ // アクセス元のヘッダ情報などをレスポンスヘッダにセットして返す | ||
}).listen(this.port); | ||
server.resetTraceRoute = function () { | ||
_traceRoute = []; | ||
}; | ||
server.getTraceRoute = function () { | ||
return _traceRoute; | ||
}; | ||
return server; | ||
}, | ||
@@ -114,0 +122,0 @@ |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0, quote-props:0, no-undefined:0*/ | ||
var assert = require('power-assert'); | ||
@@ -233,2 +234,34 @@ var type = require('type-of'); | ||
}); | ||
it('submit()時に指定するパラメータのvalueがnull/undefined/emptyの場合は"name="という形でURLに追加される', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
$('form[name=post]').submit({ | ||
foo: null, bar: undefined, baz: '' | ||
}, function (err, $, res, body) { | ||
assert($.documentInfo().url === helper.url('~info')); | ||
var h = res.headers; | ||
assert(h['request-url'] === '/~info'); | ||
assert(h['request-method'] === 'POST'); | ||
assert(h['post-data'] === 'hoge=fuga&foo=&bar=&baz='); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('submit()時に指定するパラメータが数字の0の場合は"name=0"という形でURLに追加される', function (done) { | ||
cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
$('form[name=post]').submit({ hoge: 0 }, function (err, $, res, body) { | ||
assert($.documentInfo().url === helper.url('~info')); | ||
var h = res.headers; | ||
assert(h['request-url'] === '/~info'); | ||
assert(h['request-method'] === 'POST'); | ||
assert(h['post-data'] === 'hoge=0'); | ||
assert(type($) === 'function'); | ||
assert(type(body) === 'string'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -235,0 +268,0 @@ |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0, no-undefined:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint max-len:[1, 150, 2], no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -15,5 +16,5 @@ var helper = require('./_helper'); | ||
var expected = { | ||
text: '夏目漱石「私の個人主義」', | ||
text: '夏目漱石「私の個人主義」', | ||
html: '夏目漱石「<strong>私の個人主義</strong>」', | ||
sign: '<"私の個人主義"&' + "'吾輩は猫である'>" | ||
sign: '<"私の個人主義"&\'吾輩は猫である\'>' | ||
}; | ||
@@ -64,2 +65,3 @@ | ||
// jscs:disable maximumLineLength | ||
it('16進数エンティティが文字列に変換されない', function (done) { | ||
@@ -88,2 +90,3 @@ cli.fetch(helper.url('entities', 'hex'), function (err, $, res, body) { | ||
}); | ||
// jscs:enable maximumLineLength | ||
@@ -90,0 +93,0 @@ it('文字参照エンティティが文字列に変換されない', function (done) { |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var type = require('type-of'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var type = require('type-of'); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -24,2 +25,4 @@ var type = require('type-of'); | ||
it('POST送信後にクッキーがセットされリダイレクト先に飛ぶ', function (done) { | ||
var server = this.server; | ||
server.resetTraceRoute(); | ||
var url = helper.url('manual', 'euc-jp'); | ||
@@ -31,2 +34,7 @@ cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) { | ||
assert($.documentInfo().url === url); | ||
assert.deepEqual(server.getTraceRoute(), [ | ||
'/form/utf-8.html', | ||
'/~redirect', | ||
'/manual/euc-jp.html' | ||
]); | ||
done(); | ||
@@ -33,0 +41,0 @@ }); |
@@ -1,2 +0,3 @@ | ||
/*jshint node:true,mocha:true,strict:false */ | ||
/*eslint-env mocha*/ | ||
/*eslint no-invalid-this:0*/ | ||
var assert = require('power-assert'); | ||
@@ -3,0 +4,0 @@ var helper = require('./_helper'); |
564718
84
2787
422
Updatediconv-lite@^0.4.13
Updatedrequest@^2.65.0