Getting Your Stats From the Android Marketplace


A few weeks ago I mentioned one way to get your developer stats off the Android Developer Console automatically. Unfortunately, despite being very awesome, Firefox + MozRepl is not super-great for this task. When a plugin is updated, Firefox hangs on startup. That's fine, but it kinda sucks for scripting. I'm sure there's a way around it, but that difficulty makes a good excuse for coming back to solve this problem the right way.

Following is a PHP script that uses cURL to login to the Developer Console and grab the market stats. Unfortunately, Google's app is written in GWT and the its Javascript is completely obfuscated. The market stats are fetched as JSON data and then somehow parsed, but I haven't been able to figure out how exactly. If you run this script (or just look using Firebug), you'll see that the JSON is a gigantic array. While the data of interest are clearly present in this array (total downloads, current installed base, rating, etc..), I haven't been able to figure out how to parse it reliably. If you've tried this and figured it out, I'd love to know!

This script was assembled from a bunch of random PHP/cURL tutorials and may contain redundancy, unnecessary cURL settings, etc. Python fans, see the comments of my other post on this topic where a kind soul has demonstrated the same thing in Python using mechanize.

//setup a temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

//do google authorization
$data = array('accountType' => 'GOOGLE',
         'Email' => 'YOUR_ACCOUNT_EMAIL_HERE',
         'Passwd' => 'YOUR_ACCOUNT_PW_HERE',
         'source' => '',
         'service' => 'androiddeveloper');  

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);

//grab the AUTH token for later
$auth = '';
if($info['http_code'] == 200) {
   preg_match('/Auth=(.*)/', $output, $matches);
   if(isset($matches[1])) {
       $auth = $matches[1];

//login to Android Market
//this results in a 302
//I think this is necessary for a cookie to be set
$ch = curl_init ("$auth");
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

//go to the Developer Console
$ch = curl_init ("");
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

//grab the JSON data
//perm and postdata seem to have changed in the last 6 months
//if the script isn't working, try using firebug to inspect the Request when
// gets fetched
$perm = "81E29277804F7729E9B743A43B2EFD07";
$headers = array(
   "Content-Type: text/x-gwt-rpc; charset=utf-8",
   "X-GWT-Permutation: $perm",
//not sure what x-gwt-permutation means, I think it may have to do with which version of GWT they serve based on your browser
$postdata = "5|0|4||09C42EAE15B55219550B2D800FAC1644||getFullAssetInfosForUser|1|2|3|4|0|";
$ch = curl_init ("");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

//now what?!?
$output = json_decode(substr($output, 4));

If you run this script and are willing to send me your stats, that would be super-helpful. Maybe I'll be able to get enough data to figure out why some apps have more fields than others. With only 3 apps currently on the market, I don't have much to go on. Feel free to obscure your data, but please make the changes obvious and note whether the app is free/paid and what part of the market it appears on (games/apps and sub-category). Here is a link to my best guesses so far in an Excel worksheet: market-json