cookies.txt
Advanced tools
Comparing version 0.1.0 to 0.1.1
67
index.js
@@ -1,2 +0,3 @@ | ||
//This is a wget cookies.txt parser for nodejs | ||
"use strict"; | ||
//This is a wget cookies.txt parser for nodejs | ||
//Author:@mxfli | ||
@@ -6,4 +7,8 @@ //date::2011年 12月 13日 星期二 13:33:06 UTC | ||
var fs = require('fs'); | ||
var url = require('url'); | ||
var assert = require('assert'); | ||
var that = []; | ||
var cookieDefine = ['domain', 'httponly', 'path', 'secure', 'expires', 'name', 'value']; | ||
//Object contains parsed cookies from cookies.txt | ||
var COOKIES = []; | ||
@@ -13,8 +18,18 @@ /** | ||
*/ | ||
function parse(file, cb) { | ||
fs.readFile(file, function (err, buffer) { | ||
if (err) { throw err;} | ||
var parse = function (file, cb) { | ||
assert(fs.existsSync(file)); | ||
assert(typeof cb === 'function'); | ||
var str = buffer.toString('utf8'); | ||
fs.readFile(file, function read(err, buffer) { | ||
if (err) { | ||
throw err; | ||
} | ||
//change dos/mac files to unix format | ||
var toUnix = function (str) { | ||
assert(typeof str === 'string'); | ||
return str.replace(/\\r\\n/g, '\r').replace(/\\r/g, '\n'); | ||
}; | ||
var str = toUnix(buffer.toString('utf8')); | ||
//console.log('Cookies.txt content: \n', str); | ||
@@ -24,3 +39,2 @@ | ||
//console.log(cookies.length); | ||
var result = [], cookieDefine = ['domain', 'httponly', 'path', 'secure', 'expires', 'name', 'value']; | ||
@@ -30,38 +44,35 @@ cookies.forEach(function (line) { | ||
line = line.trim(); | ||
if (line.length > 0 && !/^#/.test(line)) { | ||
var cookie = {}; | ||
line.split(/\s/).forEach(function (c, index) { | ||
if (cookieDefine[index] === 'expires') {c = (new Date(parseInt(c, 10) * 1000))} | ||
if (cookieDefine[index] === 'expires') { | ||
c = (new Date(parseInt(c, 10) * 1000)); | ||
} | ||
cookie[cookieDefine[index]] = c; | ||
}); | ||
result.push(cookie); | ||
COOKIES.push(cookie); | ||
} | ||
}); | ||
console.log("node-Cookies.txt load:", result.length, 'cookies.'); | ||
that = result; | ||
cb && cb(result); | ||
}); | ||
} | ||
console.log("node-Cookies.txt load:", COOKIES.length, 'cookies.'); | ||
module.exports.parse = parse; | ||
module.exports.add = function (cookie) { | ||
that = that.filter(function (c) { | ||
// Avoid duplication (same path, same name) | ||
return !(c.name == cookie.name && c.path == cookie.path); | ||
cb(COOKIES); | ||
}); | ||
that.push(cookie); | ||
}; | ||
module.exports.get = function () {return that}; | ||
exports.parse = parse; | ||
module.exports.getCookieString = function () { | ||
var result = that.map( | ||
function (cookie) { | ||
return cookie['name'] + '=' + cookie['value']; | ||
}).join(';'); | ||
exports.getCookieString = function (urlStr) { | ||
var urlObj = url.parse(urlStr, false); | ||
console.log('Get cookies:', result); | ||
var result = COOKIES.reduce(function (pre, cookie) { | ||
if (urlObj.hostname === cookie.domain && urlObj.pathname.indexOf(cookie.path) === 0) { | ||
pre.push(cookie.name + '=' + cookie.value); | ||
} | ||
return pre; | ||
}, []).join(';'); | ||
console.log('Get "Cookie" :', result); | ||
return result; | ||
}; |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Inaction <Inaction.me@gmail.com> (http://inaction.me)", | ||
@@ -17,0 +17,0 @@ "repository": { |
@@ -1,8 +0,14 @@ | ||
[![Build Status](https://travis-ci.org/mxfli/node-cookies.txt.png)](https://travis-ci.org/mxfli/node-cookies.txt) | ||
cookies.txt | ||
=========== | ||
TestStatus:[![Test Status](https://travis-ci.org/mxfli/node-cookies.txt.png)](https://travis-ci.org/mxfli/node-cookies.txt) | ||
#README | ||
This is a _wget cookies.txt_ formart parser for nodejs. | ||
Convert wget cookies.txt format file into JSON boject. | ||
Convert wget cookies.txt format file into JSON boject. | ||
Working whith `http.requst` or `request module`. | ||
##Install | ||
@@ -19,3 +25,21 @@ ```npm install cookies.txt``` | ||
``` | ||
### Working with request module | ||
``` | ||
var request = require('request'); | ||
var cookie = requeire('cookies.txt'); | ||
cookie.parse('your cookies.txt file path here.', function(jsonCookieObj){ | ||
//your codes here | ||
request.get({ | ||
url:'the http url', | ||
jar:true, | ||
encoding:null, | ||
headers:{ | ||
Cookie:cookie.getCookieString('the url')}}) | ||
.pipe(AWritebleStreamInstance); | ||
//... | ||
}); | ||
``` | ||
##License | ||
MIT |
3919
4
59
45