<commit_before>var request = require('request'),
    assert = require('assert'),
    async = require('async'),
    Feed = require('../models/class.feed.js'),
    Entry = require('../models/class.entry.js');

var CLIENT_LOGIN = "https://www.google.com/accounts/ClientLogin",
    TOKEN_URL = 'https://www.google.com/reader/api/0/token',
    SERVICE = "reader",
    SOURCE = "greader4kindle";

function GReader() {

}

GReader.authenticate = function(username, password, cb) {
    request.post({
        uri: CLIENT_LOGIN,
        form: {
            Email: username,
            Passwd: password,
            service: SERVICE,
            source: SOURCE,
        }
    }, function(err, res, body) {
        assert.equal(err, null);
        var tokens = getAuthenticationTokens(body);
        cb(tokens);
    });
}

function getAuthenticationTokens(body) {
    var result = {
        SID: "",
        LSID: "",
        Auth: "",
    }       

    var tokens = body.split("\n");
    var i = 0;
    for (var token in result) {
        var parts = tokens[i++].split("=");
        if (parts.length) {
            result[token] = parts[1];
        }
    }
    return result;
}

GReader.getSessionToken = function(auth, cb) {
    request({
        url: TOKEN_URL,
        headers: { 'Authorization': 'GoogleLogin auth=' + auth }
    }, function(err, res, body) {
        assert.equal(err, null);
        cb(body);
    });
}

GReader.setSessionToken = function(token) {
    this.sessionToken = token;
}

GReader.setAuthToken = function(token) {
    this.authToken = token;
}

GReader.getFeeds = function(cb) {
    getAllUnreadSubscriptionFeeds(cb);
}

function getAllUnreadSubscriptionFeeds(cb) {
    var allFeeds, unreadFeeds;

    async.series([
            function(callback) {
                var url = 'https://www.google.com/reader/api/0/subscription/list?output=json&token=' + GReader.sessionToken;
                request({
                    url: url,
                    headers: { 'Authorization': 'GoogleLogin auth=' + GReader.authToken }
                }, function(err, res, body) {
                    assert.equal(err, null);
                    allFeeds = hashAllFeeds(body);
                    callback();
                });
            },
            function(callback) {
                request({
                    url: 'https://www.google.com/reader/api/0/unread-count?output=json&all=true&token=' + GReader.sessionToken,
                    headers: { 'Authorization': 'GoogleLogin auth=' + GReader.authToken }
                }, function(err, res, body) {
                    assert.equal(err, null);
                    unreadFeeds = getUnreadFeeds(body);
                    callback();
                });
            }
    ], function (err, result) {
        for (var i = 0; i < unreadFeeds.length; i += 1) {
            var unreadFeed = unreadFeeds[i];
            if (unreadFeed === undefined) {
                continue;
            }
            var feed = allFeeds[unreadFeed.id()];
            if (feed === undefined) {
                continue;
            }
            unreadFeed.title(feed.title);
            unreadFeed.url(feed.url);

            unreadFeeds[i] = unreadFeed;
        }
        cb(sortFeedsByTitle(unreadFeeds));
    });

}

function hashAllFeeds(body) {
    var result = [];

    if (body.length == 0) return result;
    body = JSON.parse(body);
    for (var i = 0; i < body.subscriptions.length; i++) {
        var feed = body.subscriptions[i];
        result[feed.id] = {
            id: feed.id,
            title: feed.title,
            url: feed.htmlUrl
        }
    }
    return result;
}

function getUnreadFeeds(body) {
    var result = [];

    if (body.length == 0) return result;
    body = JSON.parse(body);
    for (var i = 0; i < body.unreadcounts.length; i += 1) {
        var feed = body.unreadcounts[i];
        var newFeed = Feed.create(feed.id)
        newFeed.count(feed.count);
        result.push(newFeed);
    }
    return result;
}
<commit_msg>Fails on heroku after authentication: print out some messages for debugging<commit_after>var request = require('request'),
    assert = require('assert'),
    async = require('async'),
    Feed = require('../models/class.feed.js'),
    Entry = require('../models/class.entry.js');

var CLIENT_LOGIN = "https://www.google.com/accounts/ClientLogin",
    TOKEN_URL = 'https://www.google.com/reader/api/0/token',
    SERVICE = "reader",
    SOURCE = "greader4kindle";

function GReader() {

}

GReader.authenticate = function(username, password, cb) {
    request.post({
        uri: CLIENT_LOGIN,
        form: {
            Email: username,
            Passwd: password,
            service: SERVICE,
            source: SOURCE,
        }
    }, function(err, res, body) {
        assert.equal(err, null);
        var tokens = getAuthenticationTokens(body);
        cb(tokens);
    });
}

function getAuthenticationTokens(body) {
    var result = {
        SID: "",
        LSID: "",
        Auth: "",
    }       

    var tokens = body.split("\n");
    var i = 0;
    for (var token in result) {
        var parts = tokens[i++].split("=");
        if (parts.length) {
            result[token] = parts[1];
        }
    }
    return result;
}

GReader.getSessionToken = function(auth, cb) {
    request({
        url: TOKEN_URL,
        headers: { 'Authorization': 'GoogleLogin auth=' + auth }
    }, function(err, res, body) {
        assert.equal(err, null);
        cb(body);
    });
}

GReader.setSessionToken = function(token) {
    this.sessionToken = token;
}

GReader.setAuthToken = function(token) {
    this.authToken = token;
}

GReader.getFeeds = function(cb) {
    getAllUnreadSubscriptionFeeds(cb);
}

function getAllUnreadSubscriptionFeeds(cb) {
    var allFeeds, unreadFeeds;

    async.series([
            function(callback) {
                console.log("hashAllFeeds");
                var url = 'https://www.google.com/reader/api/0/subscription/list?output=json&token=' + GReader.sessionToken;
                console.log("url: " + url);
                console.log("auth=" + GReader.authToken);
                request({
                    url: url,
                    headers: { 'Authorization': 'GoogleLogin auth=' + GReader.authToken }
                }, function(err, res, body) {
                    assert.equal(err, null);
                    allFeeds = hashAllFeeds(body);
                    callback();
                });
            },
            function(callback) {
                console.log("getUnreadFeed");
                var url = 'https://www.google.com/reader/api/0/unread-count?output=json&all=true&token=' + GReader.sessionToken;
                console.log("url: " + url);
                console.log("auth=" + GReader.authToken);
                request({
                    url: url,
                    headers: { 'Authorization': 'GoogleLogin auth=' + GReader.authToken }
                }, function(err, res, body) {
                    assert.equal(err, null);
                    unreadFeeds = getUnreadFeeds(body);
                    callback();
                });
            }
    ], function (err, result) {
        for (var i = 0; i < unreadFeeds.length; i += 1) {
            var unreadFeed = unreadFeeds[i];
            if (unreadFeed === undefined) {
                continue;
            }
            var feed = allFeeds[unreadFeed.id()];
            if (feed === undefined) {
                continue;
            }
            unreadFeed.title(feed.title);
            unreadFeed.url(feed.url);

            unreadFeeds[i] = unreadFeed;
        }
        cb(sortFeedsByTitle(unreadFeeds));
    });

}

function hashAllFeeds(body) {
    var result = [];

    if (body.length == 0) return result;
    body = JSON.parse(body);
    for (var i = 0; i < body.subscriptions.length; i++) {
        var feed = body.subscriptions[i];
        result[feed.id] = {
            id: feed.id,
            title: feed.title,
            url: feed.htmlUrl
        }
    }
    return result;
}

function getUnreadFeeds(body) {
    var result = [];

    if (body.length == 0) return result;
    console.log(body);
    body = JSON.parse(body);
    for (var i = 0; i < body.unreadcounts.length; i += 1) {
        var feed = body.unreadcounts[i];
        var newFeed = Feed.create(feed.id)
        newFeed.count(feed.count);
        result.push(newFeed);
    }
    return result;
}

{'fraction_non_alphanumeric': 0.1364012776529043, 'fraction_numerical': 0.0030758310658937655, 'mean_word_length': 1.794710743801653, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069286', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2502, 'n_words': 496}