| | |
| | | package com.yeshi.fanli.util.jd;
|
| | |
|
| | | 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;
|
| | |
| | |
|
| | | return null;
|
| | | }
|
| | | |
| | |
|
| | | public static String getJDGoodsJS() {
|
| | | InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("jdGoods.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 (Exception e) {
|
| | | }
|
| | | }
|
| | |
|
| | | public static String getJDGoodsId(String url) {
|
| | | try {
|
| | | if (url.startsWith("https://item.m.jd.com/product/") || url.startsWith("http://item.m.jd.com/product/")
|
| | | || url.startsWith("https://item.jd.com/") || url.startsWith("http://item.jd.com/")) {
|
| | | String preUrl = url.split("\\?")[0];
|
| | | String index = preUrl.split("/")[preUrl.split("/").length - 1];
|
| | | index = index.split("\\.")[0];
|
| | | return index.trim();
|
| | | } else if (url.startsWith("https://item.m.jd.com/ware/view.action?")) {
|
| | | String preUrl = url.substring(url.indexOf("?") + 1, url.length());
|
| | | String[] sts = preUrl.split("&");
|
| | | for (String st : sts) {
|
| | | if (st.startsWith("wareId=")) {
|
| | | return st.split("=")[1].trim();
|
| | | }
|
| | | }
|
| | | 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;
|
| | | }
|