/* Google Reader in NodeJS by WHK */
var http = require("http");
var https = require("https");
/* Configuración */
var sesion = {
SID : '',
LSID : '',
Auth : '',
token : ''
};
var bufferIO = '';
var obNoticias = {};
var loginData = {
mail : 'correo@gmail.com',
pass : '**********'
};
console.log(' - Conectando al servidor de Google Reader ...');
https.get('https://www.google.com/accounts/ClientLogin?service=reader&source=reader&Email=' + escape(loginData.mail) + '&Passwd=' + escape(loginData.pass),
function(res){
/* esperando un HTTP 200 */
if(res.statusCode == 200){
console.log(' - Obteniendo llaves de acceso ...');
/* Procesa el contenido */
res.on('data', function(chunk){
bufferIO += chunk.toString() + '';
}).on('end', function(){
bufferIO = bufferIO.trim(); /* Limpia el buffer de cualquier basura extra */
/* Autentificado? */
if(bufferIO == 'Error=BadAuthentication'){
console.log(' - Clave de acceso erronea.');
return false;
}
var partes = bufferIO.split('\n');
if(!partes.length){
console.log(' - Imposible ingresar, google no ha respondido correctamente.');
return false;
}
bufferIO = ''; /* Limpia la memoria */
var linea;
for(_id in partes){
linea = partes[_id].trim().split('=');
if((linea[0] == 'SID') || (linea[0] == 'LSID') || (linea[0] == 'Auth')){
sesion[linea[0]] = linea[1];
}
}
console.log(' - Obteniendo token de seguridad ...');
http.get({
host : 'www.google.com',
port : 80,
path : '/reader/api/0/token',
headers: {
'Authorization': 'GoogleLogin auth=' + sesion['Auth']
}
}, function(res2){
/* esperando un HTTP 200 */
if(res2.statusCode == 200){
/* Procesa el contenido */
res2.on('data', function(chunk2){
bufferIO += chunk2.toString() + '';
}).on('end', function(){
sesion['token'] = bufferIO;
console.log(' - Acceso concedido!');
bufferIO = ''; /* Limpia la memoria */
console.log(' - Obteniendo temas ...');
/* ############################ Obtiene las noticias ############################ */
http.get({
host : 'www.google.com',
port : 80,
path : '/reader/api/0/stream/contents/user/-/state/com.google/reading-list?ck=' + Math.floor(new Date().getTime() / 1000),
headers: {
'Authorization': 'GoogleLogin auth=' + sesion['Auth']
}
}, function(res3){
/* esperando un HTTP 200 */
if(res3.statusCode == 200){
bufferIO = '';
/* Procesa el contenido */
res3.on('data', function(chunk3){
bufferIO += chunk3.toString() + '';
}).on('end', function(){
try{
eval('obNoticias = ' + bufferIO + ';'); /* Procesa el Json entregado por Google */
for(_id in obNoticias.items){
console.log(' [' + parseInt((parseInt(_id) + 1)) + '] ' + obNoticias.items[_id].title.trim().substr(0, 67) + ' ...');
}
console.log(' - ' + parseInt((parseInt(_id) + 1)) + ' Items obtenidos.');
return true;
}catch(e){
console.log(' - Error en el servidor de Google Reader : Json corrupto.');
return false;
}
});
}else{
console.log(' - Error en el servidor de Google Reader : ' + res3.statusCode);
return false;
}
}).on('error', function(e){
console.log(' - Imposible conectar al servidor de Google Reader.');
return false;
});
});
}else{
console.log(' - Error en el servidor de Google Reader : ' + res2.statusCode);
return false;
}
}).on('error', function(e){
console.log(' - Imposible conectar al servidor de Google Reader.');
return false;
});
});
}else{
console.log(' - Error en el servidor de cuentas de Google : ' + res.statusCode);
return false;
}
}
).on('error', function(e){
console.log(' - Imposible conectar al servidor de cuentas de Google.');
return false;
});
return true;