yujian
2019-08-12 e068c9acf06cff7673fadfca686128f6d6d66e20
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;
@@ -349,7 +355,6 @@
      return null;
   }
   
   /**
    * 多多进宝爬取数据-品牌好货
    * 
@@ -360,7 +365,6 @@
      JSONObject params = new JSONObject();
      params.put("type", 1);
      params.put("sortType",3);
      HttpClient client = new HttpClient();
      PostMethod pm = new PostMethod("https://jinbao.pinduoduo.com/network/api/common/queryTopGoodsList");
@@ -405,7 +409,6 @@
      return goodsResult;
   }
   private static PDDGoodsDetail parseTodaySaleGoods(JSONObject json) {
      PDDGoodsDetail goods = new PDDGoodsDetail();
@@ -478,22 +481,49 @@
      return goods;
   }
   
   public static String getPDDGoodsId(String url) {
   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 {
         if (url.startsWith("https://mobile.yangkeduo.com/goods.html?")
               || url.startsWith("http://mobile.yangkeduo.com/goods.html?")||url.contains("yangkeduo.com/duo_coupon_landing.html?")) {
            String preUrl = url.substring(url.indexOf("?") + 1, url.length());
            String[] sts = preUrl.split("&");
            for (String st : sts) {
               if (st.startsWith("goods_id=")) {
                  return st.split("=")[1].trim();
         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;
            String goodsId = in.invokeFunction("getGoodsId", url).toString();
            if (!StringUtil.isNullOrEmpty(goodsId))
               return goodsId.trim();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
      return null;
   }
   
}