yujian
2019-07-29 cdd852f8e8eff7eb2e8f7c3fceaa955b9fc3f9d3
fanli/src/main/java/com/yeshi/fanli/util/taobao/DaTaoKeUtil.java
@@ -5,25 +5,29 @@
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.yeshi.fanli.dto.dataoke.DingDongQiangDTO;
import com.yeshi.fanli.entity.bus.clazz.GoodsClass;
import com.yeshi.fanli.entity.taobao.TaoBaoCoupon;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoHead;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetail;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -366,67 +370,45 @@
    * 
    * @return
    */
   public static List<TaoBaoGoodsBrief> getDingDongQiang() {
      List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
   public static List<DingDongQiangDTO> getDingDongQiang() {
      List<DingDongQiangDTO> list = new ArrayList<>();
      Document doc = null;
      try {
         Document doc = Jsoup.connect("http://dataoke.yeshitv.com/index.php?r=ddq/wap")
               .userAgent(
                     "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Mobile Safari/537.36")
               .get();
         Elements els = doc.getElementsByTag("script");
         for (int i = 0; i < els.size(); i++) {
            Element item = els.get(i);
            if (item.toString().length() > 5000) {
               String html = item.html();
               int endIndex = html.indexOf("} ;") + 1;
               int startIndex = html.indexOf("data =");
               String json = html.substring(startIndex + "data =".length(), endIndex).trim();
               JSONObject root = JSONObject.fromObject(json);
               Iterator<String> keys = root.keySet().iterator();
               List<String> ids = new ArrayList<>();
               while (keys.hasNext()) {
                  String key = keys.next();
                  JSONArray array = root.optJSONObject(key).optJSONArray("list");
                  for (int j = 0; j < array.size(); j++) {
                     if (TimeUtil.convertToTimeTemp(array.optJSONObject(j).optString("paiqi"),
                           "yyyy-MM-dd HH:mm:ss") < System.currentTimeMillis())
                        ids.add(array.optJSONObject(j).optString("id"));
         doc = Jsoup.connect("http://www.dataoke.com/ddq").get();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
      Elements els = doc.getElementsByTag("script");
      for (int i = 0; i < els.size(); i++) {
         String content = els.get(i).html();
         if (content.indexOf("var dataDef") > -1) {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("javascript");
            try {
               String js = "function getData(){return  JSON.stringify(dataDef);}" + content;
               engine.eval(js);
               if (engine instanceof Invocable) {
                  Invocable in = (Invocable) engine;
                  String data = in.invokeFunction("getData").toString();
                  System.out.println(data);
                  JSONObject dataJson = JSONObject.fromObject(data);
                  for (Iterator<String> its = dataJson.keys(); its.hasNext();) {
                     String key = its.next();
                     JSONArray array = dataJson.optJSONArray(key);
                     List<DaTaoKeDetail> detailList = new ArrayList<>();
                     for (int n = 0; n < array.size(); n++) {
                        detailList.add(new DaTaoKeDetail(array.optJSONObject(n).optLong("id")));
                     }
                     list.add(new DingDongQiangDTO(key, detailList));
                  }
               }
               Collections.shuffle(ids);
               List<Long> goodsIdList = new ArrayList<>();
               ids.parallelStream().forEach(id -> {
                  try {
                     long auctionId = getAuctionId(Long.parseLong(id));
                     goodsIdList.add(auctionId);
                  } catch (Exception e) {
                  }
               });
               goodsIdList.parallelStream().forEach(goods -> {
                  try {
                     TaoBaoGoodsBrief gds = TaoKeApiUtil.searchGoodsDetail(goods);
                     if (gds != null)
                        goodsList.add(gds);
                  } catch (Exception e) {
                  }
               });
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
      return goodsList;
      return list;
   }
   //
   public static List<TaoBaoCoupon> getRecommendCouple() {
      List<TaoBaoCoupon> couponList = new ArrayList<>();
@@ -536,36 +518,44 @@
      // 标题+券后价+包邮+描述
      String desc = detail.getdTitle();
      desc += "\n";
      desc += String.format("券后价%s【%s元】包邮秒杀 ",Math.random()>0.5?"💰":"", MoneyBigDecimalUtil.getWithNoZera(detail.getPrice()).toString())
            + (Math.random()>0.5?getRandomCommonEmoji():"");
      desc += String.format("券后价%s【%s元】包邮秒杀 ", Math.random() > 0.5 ? "💰" : "",
            MoneyBigDecimalUtil.getWithNoZera(detail.getPrice()).toString())
            + (Math.random() > 0.5 ? getRandomCommonEmoji() : "");
      desc += "\n";
//      desc += detail.getIntroduce();
      String introduce = detail.getIntroduce().replace(",", ",").replace("。", ",").replace("!", ",")
            .replace(",", ",");
      introduce=introduce.replace(",,,", ",").replace(",,", ",");
      // desc += detail.getIntroduce();
      String introduce = detail.getIntroduce().replace(",", ",").replace("。", ",").replace("!", ",").replace(",",
            ",");
      introduce = introduce.replace(",,,", ",").replace(",,", ",");
      String[] sts = introduce.split(",");
      introduce = "";
      for (int i = 0; i < sts.length; i++) {
         if (i % 2 == 1) {// 需要换行
            introduce += sts[i] + (Math.random()>0.5?getRandomCommonEmoji():i==sts.length-1?"":",") + "\n";
            introduce += sts[i] + (Math.random() > 0.5 ? getRandomCommonEmoji() : i == sts.length - 1 ? "" : ",")
                  + "\n";
         } else
            introduce += sts[i] + (Math.random()>0.5?getRandomCommonEmoji():i==sts.length-1?"":",");
            introduce += sts[i] + (Math.random() > 0.5 ? getRandomCommonEmoji() : i == sts.length - 1 ? "" : ",");
      }
      desc+=introduce;
      desc += introduce;
      return desc;
   }
   public static TaoBaoGoodsBrief filterTaoBaoGoods(TaoBaoGoodsBrief goods, DaTaoKeDetail detail) {
      if (detail != null) {
      if (detail != null && goods != null) {
         // 重新设置标题与券价格
         goods.setTitle(detail.getdTitle());
         goods.setCouponAmount(detail.getQuanPrice());
         if (new BigDecimal(detail.getQuanCondition()).compareTo(new BigDecimal(0)) > 0)
            goods.setCouponInfo(String.format("满%s元减%s元", detail.getQuanCondition(),
                  MoneyBigDecimalUtil.getWithNoZera(detail.getQuanPrice()).toString()));
         else
            goods.setCouponInfo(String.format("%s元无条件券", detail.getQuanPrice()));
         if (!StringUtil.isNullOrEmpty(detail.getPic()))
            goods.setPictUrlWhite(detail.getPic());
      }
      return goods;
   }
   public static TaoBaoGoodsBrief filterTaoBaoGoods(TaoBaoGoodsBrief goods, DaTaoKeDetailV2 detail) {
      if (detail != null && goods != null) {
         // 重新设置标题与券价格
         goods.setTitle(detail.getDtitle());
         if (!StringUtil.isNullOrEmpty(detail.getMainPic()))
            goods.setPictUrlWhite(detail.getMainPic());
      }
      return goods;
   }