Guest User

RevMob PHP revenue scraper

a guest
Feb 4th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.51 KB | None | 0 0
  1. <?
  2. // Fetches your RevMob revenue & impressions statistics so you can update your own local admin reports.
  3. //
  4. // author: Jeff Marshall (10coders)
  5. // contact: [email protected]
  6.  
  7. require_once("simple_html_dom.php");
  8.  
  9.  
  10. // define your console.revmob.com login information here:
  11. $REVMOB_EMAIL = "[email protected]";
  12. $REVMOB_PASSWORD = "yourpassword";
  13.  
  14. // list out your revmob application ids here.  I group them by project name.
  15. $REVMOB_APPS = array("game1" => array("ios" => "XXXXXXXXXXXXXXXXXXXXXXXX", "android" => "XXXXXXXXXXXXXXXXXXXXXXXX"),
  16.                      "game2" => array("ios" => "XXXXXXXXXXXXXXXXXXXXXXXX", "android" => "XXXXXXXXXXXXXXXXXXXXXXXX"),
  17.                      "game3" => array("ios" => "XXXXXXXXXXXXXXXXXXXXXXXX", "android" => "XXXXXXXXXXXXXXXXXXXXXXXX"));
  18.  
  19.  
  20.  
  21. class Net {
  22.   function FetchURL($url, $postargs=array(), $options=array()) {
  23.     if (!$url) return array();
  24.     $use_get = array_key_exists('use_get', $options) && $options['use_get'] ? $options['use_get'] : null;
  25.     $response_headers = array_key_exists('response_headers', $options) ? $options['response_headers'] : false;
  26.     $cookiefile = array_key_exists('cookiefile', $options) ? $options['cookiefile'] : null;
  27.  
  28.     $chunks = parse_url($url);
  29.     $port = array_key_exists('port', $chunks) ? $chunks['port'] : null;
  30.  
  31.     $ch = curl_init();
  32.     $qstr = "";
  33.     if ($use_get && $postargs)
  34.       $qstr = "?".http_build_query($postargs);
  35.  
  36.     curl_setopt($ch, CURLOPT_URL,$url.$qstr);
  37.     if ($port)
  38.       curl_setopt($ch, CURLOPT_PORT, $port);
  39.  
  40.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  41.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  42.     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  43.     curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0");
  44.     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  45.     if ($cookiefile) {
  46.       curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
  47.       curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
  48.     }
  49.     if (!$use_get) {
  50.       curl_setopt($ch, CURLOPT_POST, 1);
  51.       curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
  52.     }
  53.  
  54.     if ($response_headers) {
  55.       curl_setopt($ch, CURLOPT_HEADER, 1);
  56.     }
  57.  
  58.     $data = curl_exec($ch);
  59.     $info = curl_getinfo($ch);
  60.     $error = curl_error($ch);
  61.  
  62.     if ($response_headers) {
  63.       $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  64.       $res['response_headers'] = substr($data, 0, $header_size);
  65.       $res['data'] = substr($data, $header_size);
  66.     } else {
  67.       $res['data'] = $data;
  68.     }
  69.     $res['info'] = $info;
  70.     $res['error'] = $error;
  71.     return $res;
  72.   }
  73. }
  74.  
  75.  
  76. class RevenueFetcher {
  77.  
  78.   var $revenues = array();
  79.  
  80.  
  81.   // grab revenue data from revmob
  82.   public function grab_revmob() {
  83.     // step 1: fetch the login page to get our session and the login form's authenticity token
  84.     // returns: _mobile_ads_session in cookie and authenticity_token in login form
  85.     global $REVMOB_EMAIL, $REVMOB_PASSWORD;
  86.     $cookiefile = tempnam("/tmp", "curlcookie");
  87.     $res = Net::FetchURL("https://console.revmob.com/",
  88.                          array(),
  89.                          array("response_headers" => true, "cookiefile" => $cookiefile));
  90.  
  91.     // parse out authenticity_token
  92.     // <input name="authenticity_token" type="hidden" value="uFHD/bgBV4J8RiQ/rfa99UXw5+0qn4FH0fsbFkdyV2w=" />
  93.     $matches = array();
  94.     $authenticity_token = null;
  95.     if (preg_match("/<input\s+name=\"authenticity_token\" .*value=\"(.*)\"/", $res['data'], $matches))
  96.       $authenticity_token = $matches[1];
  97.  
  98.     if (!$authenticity_token) {
  99.       print "revmob login: error fetching authenticity_token\n";
  100.       return;
  101.     }
  102.  
  103.     // step 2: log in
  104.     // requires: email, password
  105.     // returns: _mobile_ads_session in cookie
  106.     $res = Net::FetchURL("https://console.revmob.com/users/sessions",
  107.                          array("email" => $REVMOB_EMAIL, "password" => $REVMOB_PASSWORD,
  108.                                "authenticity_token" => $authenticity_token,
  109.                                "utf8" => "&#x2713;",
  110.                                "commit" => "Login",
  111.                                "vendor" => ""),
  112.                          array("cookiefile" => $cookiefile));
  113.  
  114.     // step 3: fetch revenue by app
  115.     // requires: app id in URL
  116.     // returns: javascript table inserted into dom
  117.     global $REVMOB_APPS, $ENGINE_FILEROOT;
  118.     foreach ($REVMOB_APPS as $project=>$app_ids) {
  119.       foreach ($app_ids as $platform=>$app_id) {
  120.         $res = Net::FetchURL("https://console.revmob.com/mobile_apps/$app_id",
  121.                              array(),
  122.                              array("use_get" => true, "cookiefile" => $cookiefile));
  123.         // pull the revenue table out of the javascript dom append call
  124.         $data = str_replace("\/", "/", $res['data']);
  125.  
  126.         $html_start = strpos($data, "$('body').append(\"");
  127.         $html_end = strpos($data, "\");");
  128.         if ($html_start === false || $html_end === false) {
  129.           print "revmob: unknown javascript revenue table format\n";
  130.           continue;
  131.         }
  132.         $revtable = substr($data, $html_start, $html_end - $html_start);
  133.         $html =  str_get_html($revtable);
  134.  
  135.         // get headers rows
  136.         $thead = $html->find('thead', 0);
  137.         $headers = array();
  138.         $i = 0;
  139.         foreach($thead->find('th') as $th) {
  140.           $headers[strtolower($th->plaintext)] = $i;
  141.           $i++;
  142.         }
  143.  
  144.         // walk the rows of data
  145.         foreach($html->find('tr') as $tr) {
  146.           $day = $tr->find('td', $headers["day"]);
  147.           $impressions = $tr->find('td', $headers["impressions"]);
  148.           $cpm = $tr->find('td', $headers["ecpm"]);
  149.           $revenue = $tr->find('td', $headers["revenue"]);
  150.           if (!$day || !$day->innertext || !$revenue || !$revenue->innertext)
  151.             continue;
  152.  
  153.           $day_dt = new DateTime($day->innertext);
  154.  
  155.           $rev = str_replace(",", "", str_replace("$", "", $revenue->innertext));
  156.           $impressions = str_replace(",", "", $impressions->innertext);
  157.           $cpm = str_replace(",", "", str_replace("$", "", $cpm->innertext));
  158.  
  159.           $this->_add_revenues($project, "revmob", $platform, $rev, $day_dt, $impressions, $cpm);
  160.         }
  161.       }
  162.     }
  163.   }
  164.  
  165.  
  166.   // add this bit of revenue to the list of revenue we are tracking
  167.   protected function _add_revenues($project, $source, $platform, $revenue, $day, $impressions=null, $cpm=null) {
  168.     $this->revenues[] = array('source' => $source,
  169.                               'platform' => $platform,
  170.                               'project' => $project,
  171.                               'revenue' => $revenue,
  172.                               'day' => $day,
  173.                               'impressions' => $impressions,
  174.                               'cpm' => $cpm);
  175.   }
  176.  
  177.  
  178.   // prints the fetched revenue information for debugging
  179.   public function show() {
  180.     foreach ($this->revenues as $r)
  181.       print "Revenue [" . $r['day']->format("Y-m-d") . "] {$r['source']}.{$r['platform']} on {$r['project']}: \$" . number_format($r['revenue'], 2) . "   impressions=" . number_format($r['impressions']) . "\n";
  182.   }
  183.  
  184.  
  185.   // exercise for the reader: commit this data to your local admin revenue database
  186.   public function save() {
  187.     foreach ($this->revenues as $r) {
  188.       //$r['source']
  189.       //$r['platform']
  190.       //$r['project']
  191.       //$r['revenue']
  192.       //$r['day']
  193.       //$r['impressions']
  194.       //$r['cpm']
  195.     }
  196.   }
  197.  
  198. }
  199.  
  200.  
  201.  
  202. // fetch the revenue data
  203. $fetcher = new RevenueFetcher();
  204. $fetcher->grab_revmob();
  205. $fetcher->show();
  206. $fetcher->save();
Advertisement
Add Comment
Please, Sign In to add comment