php脚本生成google play url的下载链接,下载apk并自动反编译后获取android版本号

    需求:

    get the offer tracking link
    follow the redirect to get google play url
    Go to http://apps.evozi.com/apk-downloader/
    Enter google play url
    Download APK file
    Decompile APK file using https://code.google.com/p/android-apktool/wiki/ApktoolOptions#Decompile_Options
    Extract this file
    [app name]/smali/com/kochava/android/tracker/Global.smali
    Extract this line
    .field public static final SDK_VERSION:Ljava/lang/String; = "Android20141023"
    This output the Kochava SDK version numbe

    脚本的目的就是将上面的操作步骤自动化

    运行脚本之前必须在linux里安装apktool和java环境

    操作演示:

   

 上面输入的是应用的包名字,你也可以输入对应的google play url:     https://play.google.com/store/apps/details?hl=en&id=com.priceline.android.negotiator

  以下是代码:

 

  1 <?php
  2    fwrite(STDOUT, "please enter the google play url or app package name: ");
  3    $url = trim(fgets(STDIN));
  4    $name_reg="/id=([^&]+)/";
  5    preg_match($name_reg,$url,$name);
  6    if(empty($name)){
  7           $packagename=$url;
  8    }else{
  9           $packagename=$name[1];
 10    }
 11    function getParameter(){
 12       $url="http://apps.evozi.com/apk-downloader";
 13          $context=file_get_contents($url);
 14          preg_match("/var evoziJsData = {(.+)};/",$context,$json);
 15          $t=explode(",",$json[1]);
 16          foreach($t as $key=>$value){
 17              $value=str_replace(' ','',$value);
 18              $t[$key]=$value;
 19          }
 20          $token_value=explode(":",$t[1]);
 21          $t_value=$token_value[1];
 22          $dtrken=explode(":",$t[2]);
 23          $patten="/var ".$dtrken[1]." = '(.+)';/";         
 24          preg_match($patten,$context,$daw);
 25          $dtrken[1]=$daw[1];
 26          return array("t"=>$t_value,"dtrken"=>$dtrken);
 27    }
 28    function ajaxPost($packagename) {
 29          $tk=getParameter();
 30          $t=$tk['t'];
 31          $dtrken_key=$tk['dtrken'][0];
 32          $dtrken_value=$tk['dtrken'][1];
 33             $post_url='http://api.evozi.com/apk-downloader/download';
 34           $data = 'packagename='.$packagename.'&t='.$t.'&'.$dtrken_key.'='.$dtrken_value.'&fetch=false';
 35             $ch = curl_init();
 36             $headers = array('content-type: application/x-www-form-urlencoded;charset=UTF-8');
 37             curl_setopt($ch, CURLOPT_URL, $post_url);
 38             curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
 39             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 40             curl_setopt($ch, CURLOPT_POST, 1);
 41             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 42             $results = curl_exec($ch);
 43             curl_close($ch);
 44             $results = json_decode($results);
 45             return $results;
 46         }
 47         function getVersion($packagename){
 48               $version="/Android[0-9]+/";
 49               $path=$packagename."/smali/com/kochava/android/tracker";
 50               $filename1="Global.smali";
 51               $filename2="a.smali";
 52               if(is_dir($path)){
 53                     $file=scandir($path);
 54                     foreach($file as $key=>$value){
 55                           $filename=$value;
 56                           if(isset($filename)&file_exists($path."/".$filename)){
 57                                     $context=file_get_contents($path."/".$filename);
 58                                     preg_match($version,$context,$v);
 59                                     if(!empty($v)){
 60                                        $vs=$v[0];    
 61                                        break;
 62                                     }
 63                         }
 64                     }
 65                     if(isset($vs)){
 66                           $info=array("status"=>200,"description"=>"success detection in the file:".$filename,"version"=>$vs);
 67                      }else{
 68                           $info=array("status"=>203,"description"=>"failure detection in all the .smal file:(".$file."),we can't find the android version number");
 69                      }
 70               }else{
 71                    $info=array("status"=>201,"description"=>"there is no such directory:".$path);
 72               }
 73               $info=json_encode($info);
 74               return $info;
 75         }
 76         function getFile($file_path,$file_name){
 77             exec("sudo wget -O $file_name $file_path");
 78         }
 79         function deCompileApk($file_name){
 80                   print_r("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++start decompile apk,this may need a long time,please wait...++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL);
 81                   exec("sudo apktool d $file_name");
 82                   print_r("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++decompile end++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL);
 83         }
 84         function delFile($packagename,$file_name){
 85                   exec("sudo rm -f $file_name");
 86                   exec("sudo rm -rf $packagename");
 87         }
 88         $result=ajaxPost($packagename);
 89         if($result->status=="success"){
 90              $file_name=$packagename."."."apk";
 91            $downloadUrl=$result->url;
 92            $downloadUrl=preg_replace("/&/i", "\&", $downloadUrl);
 93        getFile($downloadUrl,$file_name);
 94        deCompileApk($file_name);
 95        $info=getVersion($packagename);
 96        delFile($packagename,$file_name);
 97         }else{
 98              $tinfo=array("status"=>204,"description"=>"we can't generate Download Link from http://apps.evozi.com/apk-downloader/,please check your input of google play url or app package name!");
 99              $info=json_encode($tinfo);
100         }
101         print_r($info.PHP_EOL);
102 ?>

 

你可能感兴趣的:(android)