PhantomJS Xbox Achievements

I originally tracked my xbox achievements by using macros and batch scripts, this allowed me to grab my own achievement data, and store it in my db for viewing on the web. This worked great apart from the inherent problem of have macros physically opening and closing browsers every 15 minutes of my virtual machine, every day or so the scripts would break or Firefox would freeze, so I was always having to keep on top of it, not ideal.

A few months later I came across XboxAPI.com, this handy (FREE) site wraps up all the xbox.com functionality and provides users with a simple to use api to get game info, friends, achievements and other data. So I scrapped my macros and jumped on this api, this worked spot on and still does. The only downside is that due to constraints on the Xbox.com website the achievement images were only available in black and white and this didn’t look to great on my website.

So my next plan was to use PhantomJS, after a few weeks of playing around with it on and off I didn’t get too far, then I came across this script on github, after a quick test, it did indeed log me into Xbox.com and grab my friends page. So a few hours later and some tweaks I’ve got it to get the users latest game achievement data in JSON, its not polished and could break at any time if Microsoft change the Xbox.com website, so bear that in mind. But hopefully it’ll give someone a good place to jump off from. :)

/*
This script provides a quick and dirty way of getting your most recent game achievement json data.

This is a lightly modified version of Jabbslad's script found here: https://gist.github.com/1653745

James Coverdale - imjam.es 2012

*/

var fs = require('fs'),
    system = require('system');

var name = "";

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 /Details/.test(url):
	    // We got the achievements
            parseGame();
            phantom.exit();
            break;
        case /Activity$/.test(url):
            // We did it!
            parseFriends();
            break;
        default:
            // uh oh we hit an unexpected url
            phantom.exit(1);
        }
    }
}

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

//This whole function will need reworking ideally, must be a better way of grabbing the json
//but this works for now
function parseGame() {

	var index = page.content.indexOf("broker.publish(routes.activity.details.load, ") + 45;
	var lastindex = page.content.indexOf("</script>", index);

	var json = page.content.substring(index, lastindex);
	json = json.split("});").join("");
	json = json + "}";

	// You could just push the json to the console to allow use in the command line, i.e. pipe it to another script.
	f = null;
	f = fs.open(name + '.json', "w");
	f.write(json);
	f.close();

}

function parseFriends() {

   try {

	   var contents = page.content.substring(page.content.indexOf("<div class=\"activityPage activity\">"), page.content.indexOf("</ol>  <div class=\"clearfix\"></div></div>"));

	   // Again this string manip is not ideal, too much that could go wrong if the site changes
	   var index = contents.indexOf("<a href=\"/en-US/Activity/Details?titleId=") + 41;
	   var url = contents.substring(index, contents.indexOf("\">", index));
	   var nameindex = contents.indexOf("alt=\"", index) + 5;
	   name = contents.substring(nameindex, contents.indexOf("\"", nameindex));

	   // make the game name lower case and remove any spaces
	   name = name.toLowerCase().split(" ").join("");

	   // Load the latest game page
	   page.open(encodeURI('https://live.xbox.com/en-US/Activity/Details?titleId=' + url));
    }
    catch (e) {
       console.log(e);
    }
}

/*
* 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/Activity'));

The script logs in, gets the game ID of the last game played (or currently being played), then heads to that game page for the user and pulls the JSON from the page and saves it as a .json file. You could modify it to not save the JSON and just output it to the console if you want, but in my setup I needed the .json file to upload to my webserver in order to do extra processing upon it.

To run the script simple head to PhantomJS.org and download the relevant version, runs on Windows, Mac and Linux. then simple from the command line call…

phantomjs XboxScript.js username password

Simple, you will need to edit the name of the .js file to what you have saved it, and will probably need to point to the phantomjs executable on your system.

Sky Broadband Settings

After my Sky router broke, and sky told me I have to pay £35 for a new one, i decided I would try to use an old router I had lying around. Now Sky, unlike other ADSL providers do not allow you to use your own hardware, its even against their ToC’s!

So the first thing you need is your username and password, which Sky hide within the hardware, so head here, and get your username and password.

Now these are the settings I entered on my Safecom router, so your router may vary, but generally the settings will be similar or use different names.

Data Encapsulation: VC-Mux
VPI Number: 0
VCI Number: 38
Schedule type: CBR
WAN type: PPP over ATM
Authentication: CHAP (I didnt have this option, but you may :))

I hope this info helps someone, and bear in mind using your own router/modem with Sky broadband is against their ToC and they CAN detect that your using different hardware and MAY end your service some point in the future.

ROC Osmotherley

I always like a bit of an explore, whether it be Geocaching or finding some caves to climb through. After my friend linked me to a few articles about the old ROC posts (they were built in the 50′s and were used to gather information about a nuclear attack) dotted around the UK, I decided to see if there were any near me. There were a few, some knocked down, some locked down on private land, but there was one at Osmotherley, about 15 mile away, and this one was in fairly good nick, to say it was left in 1968!

So a few google searches later I had the location of the ROC post, but couldnt work out how we’d be able to walk there, so a quick look at a OS map, I saw part of the Cleveland Way went right by it. So armed with some torches, the gps and my camera me and Mike went off on walk. I thought I’d get a few caches while on the way (would be rude not to).

Finally after a 4 mile walk, we went the long way, we were within touching distance of the post, we just had to figure out how to climb up to it, after a few fruitless attempts to wade through the bracken and other foliage we found the right path up to the top, and the ROC was in view!

A quick dash over the field and we were there, the iron lid was just lying on top, not locked down, good news! :) We removed the lid and got a view of the bunker, now we were a bit apprehensive, neither of us wanting to go down first, after a bit of “you go, no you go” I decided I best head down first. The bunker is just 2 rooms, the larger bedroom and a small toilet, i imagine there would of been more equipment down there back in the 60′s, but all that was left apart from the rubbish was the old beds and a massive battery.

When we got out of the bunker we were met by a big thunderstorm, although my picture taking skills doesn’t do it justice :p

More photos can be found in my Flickr set.

Lazy

Haven’t posted in a while, and this one isn’t going to be that interesting either. :p

Last week me and @Bonxy and a few other lads headed down to FCS 2012 (French Car Show) at Donnington Racetrack. We’ve put all the pictures here, fcs.bonxy.net (Although you can find them via our own Flickr pages as well :) )

I’ve also been playing around with ifttt, they just done a big update, so i’ve decided to auto post all my pictures to a separate blog, just as a sort of archive really.

 

Thats it. :)

Isle of Skye

Having got back at the beginning of the week from the Isle of Skye, we camped for a week up there, I’ve finally managed to sort through a few of the pictures I took (over 450), so as well as posting a few of them on my flickr account, I thought I’d put a few up in a blog post.

We set off early on the Saturday morning, heading for Fort William, about 1.5/2 hours north of Glasgow, we spent two nights here. The original plan was for me and my dad to climb to the top of Ben Nevis, the highest mountain in Britain, 1344m, but we seriously underestimated the height and the length of the climb, so decided against it. I still want to climb to the top, so will need to go back in the summer one time ( with @Bonxy ;)).

So instead we did a MUCH tamer walk from our campsite up to old fort, Dun Deardail ( 350metres ) which took around 2 hours ish. You got some great views of Glen Nevis and other surrounding hills/mountains.

On monday we packed up and headed for Portree on the Isle of Skye, we camped here for the rest of the week. We managed todo a few good walks while we were here, there was a great coastal walk from the campsite into the town which added on maybe 2 miles or so, but again the scenery was great, and i managed to get a few good pictures during this walk.

Later in the week we headed up to Staffin to have a walk on the beach, turned out there isnt much of a beach here, just a bit of sand, plenty of rocks and a jetty. But the area is famous for all the dinosaur remains that have been found nearby, plus there are some dinosaur footprints on the rocks by the sea, there was a sign with a map pointing out where they were but having a good look for 15 minutes I couldnt spot them :(

On our last day we headed up to see the ‘Old Man of Storr‘ which is basically a big rock spire sticking up out of the ground that is apart of the Storr hills, again we underestimated how steep the climb/walk up would be, so about 30 minutes in my mam and jamie turned back, leaving my dad to press on. The first part of the walk you are surrounded by trees, so you dont get to see anything above or where the path is going, eventually me and my dad emerged from the trees with a great view of the Storr hills and the Old Man.

Old Man of Storr

Unfortunately my dad didnt fancy walking up to the old man, so leaving my dad with my bag and the dog I set off up by myself. After 20 mins of walking the steep path then another scary(fun) scramble/climb I got to the base of the Old Man, and the views were pretty spectacular.

Unfortunately my photo taking skills with my new camera are not much to shout about, so the few pictures I did take of the view, don’t really do it justice. Another thing I realised i need is a bigger lens, well one that’ll let me zoom in more :p I knew I’d want to get one eventually, but there were plenty of photos I wanted to take while i was away where I couldn’t zoom in enough to pick out the detail, still my birthday is only a month or so away :D

I gots a new camera!

After wanting one for a while, but deciding they were too expensive, I then got to play with @RebekahEmse‘s DSLR the other weekend, this made my mind up, I had to have one. :)

To please @Bonxy

So I got the Nikon D3000, its a few years old now, 2009 I think, but I didn’t fancy paying too much, I managed to get this for £310 from Argos. There’s lots to figure out, and i’m only just getting the hang of it after a few days, still loads to learn. I’ve mainly been playing with the shutter speed, seeing what happens, when not doing that I’ve got it happily in auto mode :p