From 51a4ff5d777028d52a19c314a99f796334cb7b51 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 23 十一月 2019 18:30:01 +0800 Subject: [PATCH] 配置文件修改 --- fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java | 123 ++++++++++++++++++++++++++++++++++------ 1 files changed, 103 insertions(+), 20 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java index 6a44fe1..37f2d29 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java +++ b/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,12 +364,11 @@ 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"); - pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"); + pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3730.400 QQBrowser/10.5.3805.400"); pm.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); pm.setRequestHeader("Referer", "https://jinbao.pinduoduo.com/promotion/hot-promotion"); pm.setRequestBody(params.toString()); @@ -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,23 +480,103 @@ 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; + Object goodsId = in.invokeFunction("getGoodsId", url); + if (goodsId!=null) + return goodsId.toString().trim(); } } catch (Exception e) { + e.printStackTrace(); } return null; } + public static PDDGoodsDetail getPDDGoodsInfo(String id) { + try { + Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id=" + id) + .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36").get(); + Elements els = doc.getElementsByTag("script"); + for (int i = 0; i < els.size(); i++) { + if (els.get(i).html().contains("window.rawData")) { + String dataJS = els.get(i).html().replace("window.", "var "); + dataJS += "function getData(){return JSON.stringify(rawData);}"; + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("javascript"); + try { + engine.eval(dataJS); + if (engine instanceof Invocable) { + Invocable in = (Invocable) engine; + String jsonStr = in.invokeFunction("getData").toString(); + JSONObject json = JSONObject.fromObject(jsonStr); + + JSONObject goods = json.optJSONObject("store").optJSONObject("initDataObj") + .optJSONObject("goods"); + + String imageData = goods.optString("viewImageData"); + imageData = imageData.replace("[", "").replace("]", ""); + String[] images = imageData.split(","); + + String image = images[0].trim(); + if (image.startsWith("\"")) + image =image.substring(1, image.length()-1); + if (image.endsWith("\"")) + image =image.substring(0, image.length()-2); + + + PDDGoodsDetail goodsDetail = new PDDGoodsDetail(); + goodsDetail.setGoodsImageUrl("http:" + image); + goodsDetail.setGoodsThumbnailUrl("http:" + image); + goodsDetail.setGoodsName(goods.optString("goodsName")); + goodsDetail.setGoodsId(goods.optLong("goodsID")); + return goodsDetail; + } + } catch (Exception e) { + e.printStackTrace(); + } + break; + } + } + } catch (Exception e1) { + e1.printStackTrace(); + } + return null; + } + } -- Gitblit v1.8.0