djekl
1/21/2012 - 7:55 PM

Xbox Live Scraper For PhantomJS

Xbox Live Scraper For PhantomJS

/* 
Copyright (c) 2012 Jabbslad

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. 
*/

if (phantom.args.length !== 2) {
    console.log('Usage xbox.js <username> <password>');
    phantom.exit();
}

var page = new WebPage({
    'page.settings.loadImages': false
});

/*
* Callback to process page when finshed loading
*/
page.onLoadFinished = function(status) {
    if (status !== "success") {
        console.log("Unable to access network");
    } else {
        var url = getPageUrl();
        switch (true) {
        case /login.srf/.test(url):
            // Step 1 - Login
            page.evaluate(fillFormFunctionAsString());
            break;
        case /post.srf/.test(url):
            // Step 2 - Process Cookies
            break;
        case /Friends$/.test(url):
            // We did it!
            parseFriends();
            phantom.exit();
            break;
        default:
            // uh of we hit an unexpected url
            phantom.exit(1);
        }
    }
}

function getPageUrl() {
    return page.evaluate(function() {
        return location.href
    });
}

/*
* TODO: Parse HTML script
*/
function parseFriends() {
    console.log(page.content);
}

/*
* Hack due to phantomjs page.evaluate limitations:- 
* http://code.google.com/p/phantomjs/issues/detail?id=132
*/
function fillFormFunctionAsString() {
    return "function() {" 
         + "var form = document.querySelector(\"form[name='f1']\"); " 
         + "var login = form.querySelector(\"input[name='login']\"); " 
         + "login.value = '" + phantom.args[0] + "'; " 
         + "var passwd = form.querySelector(\"input[name='passwd']\"); " 
         + "passwd.value = '" + phantom.args[1] + "'; " 
         + "var kmsi = form.querySelector(\"input[name='KMSI']\"); " 
         + "kmsi.value = '2'; " 
         + "form.querySelector('#idSIButton9').click();}";
};

page.open(encodeURI('https://live.xbox.com/en-US/Friends'));