From 207dc8655711cddac2653e18b51e58a88dba2084 Mon Sep 17 00:00:00 2001 From: yj <Administrator@192> Date: 星期五, 06 三月 2020 18:14:36 +0800 Subject: [PATCH] 发圈处理 --- fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java | 62 ++++++++++++++++++++++++------- 1 files changed, 48 insertions(+), 14 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 37f2d29..3979375 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 @@ -9,13 +9,17 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -368,7 +372,8 @@ 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.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("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()); @@ -512,12 +517,30 @@ } public static String getPDDGoodsId(String url) { + if (url == null) + return null; + String link = url; + if (link.contains("://p.pinduoduo.com/")) {// 鎷煎澶氱殑鐭摼 + HttpClient client = new HttpClient(); + client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); + PostMethod pm = new PostMethod(link); + try { + client.executeMethod(pm); + Header location = pm.getResponseHeader("Location"); + if (location != null) + link = location.getValue(); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } try { if (engine instanceof Invocable) { Invocable in = (Invocable) engine; - Object goodsId = in.invokeFunction("getGoodsId", url); - if (goodsId!=null) + Object goodsId = in.invokeFunction("getGoodsId", link); + if (goodsId != null) return goodsId.toString().trim(); } } catch (Exception e) { @@ -525,12 +548,13 @@ } 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(); + .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")) { @@ -545,23 +569,22 @@ 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); + image = image.substring(1, image.length() - 1); if (image.endsWith("\"")) - image =image.substring(0, image.length()-2); - - + image = image.substring(0, image.length() - 2); + PDDGoodsDetail goodsDetail = new PDDGoodsDetail(); - goodsDetail.setGoodsImageUrl("http:" + image); + goodsDetail.setGoodsImageUrl("http:" + image); goodsDetail.setGoodsThumbnailUrl("http:" + image); goodsDetail.setGoodsName(goods.optString("goodsName")); goodsDetail.setGoodsId(goods.optLong("goodsID")); @@ -579,4 +602,15 @@ return null; } + public static List<String> getPDDShortLinksFromText(String text) { + String regex = "(https://p\\.pinduoduo\\.com/)[0-9A-Za-z]{1,20}"; + Pattern pattern = Pattern.compile(regex); + Matcher m = pattern.matcher(text); + List<String> urlList = new ArrayList<>(); + while (m.find()) { + urlList.add(m.group()); + } + return urlList; + } + } -- Gitblit v1.8.0