yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java
@@ -1,5 +1,9 @@
package com.yeshi.fanli.util.pinduoduo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
@@ -9,6 +13,7 @@
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
@@ -24,6 +29,7 @@
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -145,7 +151,7 @@
      try {
         client.executeMethod(pm);
         String result = pm.getResponseBodyAsString();
         LogHelper.test("拼多多猜你喜欢:"+result);
         LogHelper.test("拼多多猜你喜欢:" + result);
         JSONObject json = JSONObject.fromObject(result);
         JSONArray array = json.optJSONArray("data");
         if (array != null) {
@@ -348,8 +354,7 @@
      }
      return null;
   }
   /**
    * 多多进宝爬取数据-品牌好货
    * 
@@ -359,8 +364,7 @@
   public static PDDGoodsResult getTodaySaleGoods() {
      JSONObject params = new JSONObject();
      params.put("type", 1);
      params.put("sortType",3);
      params.put("sortType", 3);
      HttpClient client = new HttpClient();
      PostMethod pm = new PostMethod("https://jinbao.pinduoduo.com/network/api/common/queryTopGoodsList");
@@ -406,7 +410,6 @@
      return goodsResult;
   }
   private static PDDGoodsDetail parseTodaySaleGoods(JSONObject json) {
      PDDGoodsDetail goods = new PDDGoodsDetail();
      goods.setMallName(json.optString("mallName"));
@@ -419,7 +422,7 @@
      if (json.optString("mallId") != null) {
         goods.setMallId(json.getLong("mallId"));
      }
      if (json.optString("goodsImageUrl") != null) {
         goods.setGoodsImageUrl(json.optString("goodsImageUrl"));
      }
@@ -431,7 +434,7 @@
      if (json.optString("minGroupPrice") != null) {
         goods.setMinGroupPrice(json.getLong("minGroupPrice"));
      }
      if (json.optString("minNormalPrice") != null) {
         goods.setMinNormalPrice(json.getLong("minNormalPrice"));
      }
@@ -477,4 +480,50 @@
      return goods;
   }
   public static String getJDGoodsJS() {
      InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("pddGoods.js");
      StringBuilder sb = new StringBuilder();
      String line;
      BufferedReader br = new BufferedReader(new InputStreamReader(input));
      try {
         while ((line = br.readLine()) != null) {
            sb.append(line);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
      return sb.toString();
   }
   static String jdGoodsJs = null;
   static ScriptEngine engine = null;
   static {
      if (jdGoodsJs == null)
         jdGoodsJs = getJDGoodsJS();
      ScriptEngineManager manager = new ScriptEngineManager();
      engine = manager.getEngineByName("javascript");
      try {
         engine.eval(jdGoodsJs);
      } catch (ScriptException e) {
         e.printStackTrace();
      }
   }
   public static String getPDDGoodsId(String url) {
      try {
         if (engine instanceof Invocable) {
            Invocable in = (Invocable) engine;
            Object goodsId = in.invokeFunction("getGoodsId", url);
            if (goodsId!=null)
               return goodsId.toString().trim();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
}