タイトルそのままですが、Google Readerの情報を簡単に取得できるようなライブラリを勉強をかねて作成してます。
出来栄えとしてはまだまだですが、こんな感じでGoogleReaderに登録してあるフィードの一覧がJSON形式で出力できる所まではわかってきたのですが、一番やりたいのはスターを付けたエントリの一覧を手軽に取得したいことなんですよね・・
//test_login.js var sys = require('sys'); var GoogleReaderClient = require(__dirname +'/../GoogleReaderClient').GoogleReaderClient; var googlereader = new GoogleReaderClient('xxxxx@gmail.com', 'YOURPASSWORD'); googlereader._login(function(result){ if(result){ console.log('login success!'); googlereader.subscriptionList(function(result) { console.log(result); }); }else{ console.log('login fail'); } });
とりあえず現時点でライブラリ(GoogleReaderClient.js)
//GoogleReaderClient.js var url = require('url'), querystring = require('querystring'), http = require('http'); var GoogleReaderClient = function(email, password){ this._authToken = ''; this.config = { email : email, password : password, host : "www.google.com", https_port : '443', number_of_items: "n", output : "json", login_param :{ accountType: 'HOSTED_OR_GOOGLE', Email : email, Passwd : password, 'Content-Type': "application/x-www-form-urlencoded", service : "reader" } }; }; GoogleReaderClient.prototype = { _parseChunks: function(chunks) { return chunks.join(''); }, _setHeaders:function(auth){ var param ={}; var _values = "GoogleLogin auth=" + auth; param["Authorization"]= _values; return param; }, _doRequest:function(request_param, callback){ var scope = this; // https or httpでcreateClientの最後のフラグのセットをする必要があるみたい if(request_param.port == '443'){ var _flag = true; } else { var _flag = false; } var client = http.createClient( request_param.port, this.config.host, _flag ); var request = client.request( request_param.method, request_param.pathname, request_param.httpheader ); request.on('response', function(response) { var data = []; response.on('data', function(chunk) { data.push(chunk); }); response.on('end', function() { console.log('STATUS: ' + response.statusCode); var urldata = scope._parseChunks(data); var result = JSON.parse(urldata); console.log(result); return callback(result); }); }); request.end(); }, _login:function(callback){ var self = this; var body = querystring.stringify(this.config.login_param); var client = http.createClient( this.config.https_port, this.config.host, true ); var req = client.request( 'POST', '/accounts/ClientLogin', { 'Content-Type': "application/x-www-form-urlencoded" } ); req.write(body); req.on('response', function(res){ var body = ''; res.on('data', function(chunk){ body += chunk; }); res.on('end', function(){ var obj = {}; var lines = body.split('\n'); for(var i in lines){ var l = lines[i]; var kv = l.split('='); if( kv.length == 2 ){ obj[kv[0]] = kv[1]; } } // obj.SID or obj.LSID or obj.Authで其々のプロパティ取得可能 if(res.statusCode == 200){ this._authToken = obj.Auth; self._authType = 'GoogleLogin'; self._setAuthKey(obj.Auth); } else { this._authToken = undefined; } callback && callback(res.statusCode == 200, obj); }); }); req.end(); }, _setAuthKey:function(value){ return this._authToken = value; }, _verifyAuthKey:function(){ return this._authToken; }, starred:function(callback){ var self = this; var query = { port: 80, method: 'GET', pathname:'/reader/atom/user/-/state/com.google/starred', httpheader:self._setHeaders(self._authToken) }; this._doRequest(query, callback); }, subscriptionList:function(callback){ var self = this; var query = { port: 80, method: 'GET', pathname:'/reader/api/0/subscription/list?output=json', httpheader:self._setHeaders(self._authToken) }; this._doRequest(query, callback); } }; exports.GoogleReaderClient = GoogleReaderClient;