返回顶部

收藏

爬去 ios app 下载量的代码

更多
package com.wecash.bi.job;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.google.gson.Gson;
import com.wecash.bi.util.Config;
import com.wecash.bi.util.WecashTool;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class IosAttentionJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String startDate=WecashTool.lastDay("yyyy-MM-dd", -3);
        String endDate=WecashTool.lastDay("yyyy-MM-dd", -1);
        writeIosNewUser(startDate,  endDate);                                                           
    }
    /**
     * @param startDate  开始时间  yyyy-MM-dd
     * @param endDate   结束时间  yyyy-MM-dd
     */
    public static void writeIosNewUser(String startDate,String endDate) {
        try {
           HttpClient httpclient = new HttpClient();
            PostMethod post = new PostMethod("https://idmsa.apple.com/appleauth/auth/signin");
            Map iosUser=new HashMap ();
            iosUser.put("accountName", Config.instace().getProperty("bi.wecash.ios.user"));
            iosUser.put("password",  Config.instace().getProperty("bi.wecash.ios.password"));
            iosUser.put("rememberMe", true);
            post.setRequestBody( new Gson().toJson(iosUser));
            Map cookie_m=new HashMap ();
            cookie_m.put("dssf",1);
            cookie_m.put("dssid2", "a7aaab83-0ba7-4eab-b003-1e2fb51ac453");
            cookie_m.put("s_vnum_n2_cn", "0%7C1");
            post.addRequestHeader("Content-Type","application/json");
            post.addRequestHeader("Cookie", buildCookie(cookie_m  )  );
            post.addRequestHeader("Accept-Language","zh-CN,zh;q=0.8");
            httpclient.executeMethod(post);
             Header[] ls = post.getResponseHeaders( "Set-Cookie"); 
             Map m = getCookie(cookie_m,ls);
             post = new PostMethod("https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa?");
             post.addRequestHeader("Cookie",  buildCookie(m )  );
             httpclient.executeMethod(post);
             ls = post.getResponseHeaders( "Set-Cookie"); 
             m = getCookie(cookie_m,ls);
             post = new PostMethod("https://analytics.itunes.apple.com/analytics/api/v1/data/time-series");
             post.addRequestHeader("Cookie", buildCookie(cookie_m  )  );
             post.addRequestHeader("X-Requested-By", "analytics.itunes.apple.com");
             post.addRequestHeader("Content-Type","application/json");
             post.setRequestBody(String.format(   "{\"adamId\":[\"863934534\"],\"startTime\":\"%sT00:00:00Z\",\"endTime\":\"%sT00:00:00Z\",\"frequency\":\"DAY\",\"measures\":[\"units\"],\"group\":null,\"dimensionFilters\":[]}"  , startDate,endDate) );
             httpclient.executeMethod(post);
             JSONObject jsonObject=JSONObject.fromObject( post.getResponseBodyAsString() );
             JSONArray arr = ((JSONObject)jsonObject.getJSONArray("results").get(0))  . getJSONArray("data");
             for (  Object o:arr ) {
                jsonObject=(JSONObject)o;
                String date=    jsonObject.getString("date").substring(0,10);
                  double value = jsonObject.getDouble("units");
                WxAttentionJob.dataDay(date, "iosTrigger","IOS激活", value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Map  getCookie(      Map m ,Header[] ls) {
         for (Header header : ls) {
              String v = header.getValue();
            int i1= v.indexOf("=");
             int i2 = v.indexOf(";", i1);
             m.put(  v.substring(0,i1), v.substring(i1+1, i2));
         }
        return m;
    }
    private static String  buildCookie(     Map m ,String ... cookie) {
          StringBuffer sb =new StringBuffer();
          for (int i = 0; i < cookie.length; i++) {
              sb.append(cookie[i]);
              sb.append("=");
              sb.append(m.get(cookie[i] ).toString() );
              sb.append(";");
        }
        return sb.toString();
    }

    private static String  buildCookie(     Map m  ) {
         Iterator i = m.keySet().iterator();
         StringBuffer sb =new StringBuffer();
         while (i.hasNext()) {
              String  k =(String) i.next();
          sb.append( k);
          sb.append("=");
          sb.append(m.get(k).toString() );
          sb.append(";");
        }
        return sb.toString();
    }

}

标签:lua

收藏

0人收藏

支持

0

反对

0

发表评论