package com.yeshi.fanli.util.taobao;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
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 org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
|
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.tag.PageEntity;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.TimeUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
/**
|
* 大淘客api接口
|
*
|
* @author Administrator
|
*
|
*/
|
public class DaTaoKeUtil {
|
|
public static List<GoodsClass> goodsClasses;
|
static {
|
goodsClasses = new ArrayList<>();
|
goodsClasses.add(new GoodsClass(4L, "居家日用"));
|
goodsClasses.add(new GoodsClass(6L, "美食"));
|
goodsClasses.add(new GoodsClass(2L, "母婴"));
|
goodsClasses.add(new GoodsClass(3L, "美妆"));
|
goodsClasses.add(new GoodsClass(1L, "女装"));
|
goodsClasses.add(new GoodsClass(8L, "数码家电"));
|
goodsClasses.add(new GoodsClass(7L, "文娱车品"));
|
goodsClasses.add(new GoodsClass(10L, "内衣"));
|
goodsClasses.add(new GoodsClass(14L, "家装家纺"));
|
goodsClasses.add(new GoodsClass(5L, "鞋品"));
|
goodsClasses.add(new GoodsClass(9L, "男装"));
|
goodsClasses.add(new GoodsClass(12L, "配饰"));
|
goodsClasses.add(new GoodsClass(13L, "户外运动"));
|
goodsClasses.add(new GoodsClass(11L, "箱包"));
|
}
|
|
public static final String HOST = "http://dataoke.yeshitv.com";
|
|
public static void search(String key) {
|
String header = "Mozilla/5.0 (Linux; Android 6.0; M5 Note Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043024 Safari/537.36 MicroMessenger/6.5.4.1000 NetType/WIFI Language/zh_CN";
|
try {
|
String searchUrl = String.format("http://dataoke.yeshitv.com/index.php?r=%s&kw=%s",
|
URLEncoder.encode("index/search", "UTF-8"));
|
Document root = Jsoup.connect(searchUrl).header("User-Agent", header).timeout(60000).get();
|
Elements items = root.getElementsByAttributeValue("class", "goods-list").get(0)
|
.getElementsByAttributeValue("class", "goods-item");
|
if (items.size() > 0) {
|
String code = getTKL(HOST + items.get(0).getElementsByTag("a").attr("href"));
|
int count = 1;
|
while (com.yeshi.fanli.util.StringUtil.isNullOrEmpty(code) && count < items.size() && count < 5) {
|
code = getTKL(HOST + items.get(count).getElementsByTag("a").attr("href"));
|
count++;
|
}
|
}
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
// 获取淘口�?
|
public static String getTKL(String url) {
|
String header = "Mozilla/5.0 (Linux; Android 6.0; M5 Note Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043024 Safari/537.36 MicroMessenger/6.5.4.1000 NetType/WIFI Language/zh_CN";
|
try {
|
Document root = Jsoup.connect(url).header("User-Agent", header).timeout(60000).get();
|
return root.getElementsByAttributeValue("class", "tuwen-tkl").get(0)
|
.getElementsByAttributeValue("class", "tkl").get(0).getElementById("code1_ios").text().trim();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static List<GoodsClass> getGoodsType() {
|
List<GoodsClass> list = new ArrayList<>();
|
String[] names = new String[] { "女装", "男装", "内衣", "母婴", "化妆品", "居家", "鞋包配饰", "美食", "文体车品", "数码家电" };
|
int[] ids = new int[] { 1, 9, 10, 2, 3, 4, 5, 6, 7, 8 };
|
for (int i = 0; i < names.length; i++) {
|
GoodsClass gc = new GoodsClass();
|
gc.setId(Long.parseLong(ids[i] + ""));
|
gc.setName(names[i]);
|
list.add(gc);
|
}
|
return list;
|
}
|
|
public static TaoBaoSearchResult getGoodsByType(int type) {
|
TaoBaoSearchResult result = new TaoBaoSearchResult();
|
result.setNavList(null);
|
List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
|
JSONArray array = getGoods("http://dataoke.yeshitv.com/index.php?r=index/cat&cid=" + type);
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject data = array.optJSONObject(i);
|
TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief();
|
goods.setAuctionId(data.optLong("url"));
|
goods.setBiz30day(data.optInt("nums"));
|
goods.setPictUrl(data.optString("img").split(".jpg")[0] + ".jpg");
|
goods.setCouponAmount(new BigDecimal(data.optString("quan")));
|
goods.setZkPrice(new BigDecimal(data.optString("beforPrice")));
|
goods.setCouponStartFee(new BigDecimal(0));
|
goods.setTitle(data.optString("title"));
|
goods.setTkCommFee(new BigDecimal(0));
|
goods.setTkRate(new BigDecimal(0));
|
goodsList.add(goods);
|
}
|
PageEntity pe = new PageEntity(1, 20, 1000);
|
result.setPageEntity(pe);
|
result.setTaoBaoGoodsBriefs(goodsList);
|
TaoBaoHead head = new TaoBaoHead();
|
head.setDocsfound(1000);
|
result.setTaoBaoHead(head);
|
|
return result;
|
}
|
|
public static TaoBaoSearchResult searchByKey(String kw) {
|
TaoBaoSearchResult result = new TaoBaoSearchResult();
|
result.setNavList(null);
|
List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
|
JSONArray array = null;
|
try {
|
array = getGoods("http://dataoke.yeshitv.com/index.php?r=l&kw=" + URLEncoder.encode(kw, "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject data = array.optJSONObject(i);
|
TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief();
|
goods.setAuctionId(data.optLong("url"));
|
goods.setBiz30day(data.optInt("nums"));
|
goods.setPictUrl(data.optString("img").split(".jpg")[0] + ".jpg");
|
goods.setCouponAmount(new BigDecimal(data.optString("quan")));
|
goods.setZkPrice(new BigDecimal(data.optString("beforPrice")));
|
goods.setCouponStartFee(new BigDecimal(0));
|
goods.setTitle(data.optString("title"));
|
goods.setTkCommFee(new BigDecimal(0));
|
goods.setTkRate(new BigDecimal(0));
|
goodsList.add(goods);
|
}
|
PageEntity pe = new PageEntity(1, 20, 1000);
|
result.setPageEntity(pe);
|
result.setTaoBaoGoodsBriefs(goodsList);
|
TaoBaoHead head = new TaoBaoHead();
|
head.setDocsfound(1000);
|
result.setTaoBaoHead(head);
|
|
return result;
|
}
|
|
public static JSONArray getGoods(String pageUrl) {
|
if (pageUrl == null)
|
return null;
|
|
Document doc = null;
|
try {
|
doc = Jsoup.connect(pageUrl)
|
.userAgent(
|
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1")
|
.get();
|
} catch (IOException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
if (null == doc) {
|
return null;
|
}
|
|
Elements elements = doc.getElementsByClass("goods-item");
|
JSONArray jsa = new JSONArray();
|
|
for (int i = 0; i < elements.size(); i++) {
|
JSONObject obj = new JSONObject();
|
Element element = elements.get(i);
|
|
String img = element.getElementsByAttributeValue("class", "img").get(0).getElementsByTag("img").get(0)
|
.attr("data-original");
|
if (StringUtil.isNullOrEmpty(img))
|
img = element.getElementsByAttributeValue("class", "img").get(0).getElementsByTag("img").get(0)
|
.attr("src");
|
if (img.startsWith("//")) {
|
img = "http:" + img;
|
}
|
String title = element.getElementsByAttributeValue("class", "title").get(0)
|
.getElementsByAttributeValue("class", "text").get(0).text();
|
String price = element.getElementsByAttributeValue("class", "price-wrapper").get(0)
|
.getElementsByAttributeValue("class", "price").get(0).text().replace("¥", "");
|
String coupon = element.getElementsByClass("img").get(0).getElementsByClass("coupon-wrapper").get(0)
|
.getElementsByTag("b").get(0).text().replace("元", "");
|
String afterPrice = price + "";
|
String beforePrice = Float.parseFloat(price) + Float.parseFloat(coupon) + "";
|
String nums = element.getElementsByAttributeValue("class", "sold-num").get(0).text();
|
String[] chars = element.getElementsByAttributeValue("class", "img").get(0).attr("href").split("id=");
|
String id = chars[1].split("&u=")[0];
|
|
String quanMoney = element.getElementsByClass("coupon-wrapper").get(0).getElementsByTag("b").get(0).text();
|
|
obj.put("img", img);
|
obj.put("title", title);
|
obj.put("afterPrice", afterPrice);
|
obj.put("beforPrice", beforePrice);
|
obj.put("nums", nums);
|
obj.put("url", id);
|
obj.put("quan", quanMoney);
|
jsa.add(obj);
|
}
|
return jsa;
|
}
|
|
public static long getAuctionId(long id) {
|
Document doc = null;
|
try {
|
doc = Jsoup.connect("http://dataoke.yeshitv.com/index.php?r=p/dnew&id=" + id + "&type=0&version=2")
|
.userAgent(
|
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1")
|
.get();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
String href = "";
|
Elements as = doc.getElementsByClass("goods_shop_cart").get(0).getElementsByTag("a");
|
if (as != null)
|
for (int i = 0; i < as.size(); i++) {
|
if (as.get(i).attr("href").contains("uland.taobao.com/coupon/edetail")) {
|
href = as.get(i).attr("href");
|
break;
|
}
|
}
|
|
String[] sts = href.split("&");
|
for (String st : sts) {
|
if (st.contains("itemId="))
|
return Long.parseLong(st.replace("itemId=", ""));
|
}
|
return 0;
|
}
|
|
public static List<Long> get9K9List(int page) {
|
|
String url = "";
|
|
Document doc = null;
|
|
// for (String st : sts) {
|
// if (st.contains("itemId="))
|
// return Long.parseLong(st.replace("itemId=", ""));
|
// }
|
// return 0;
|
return null;
|
}
|
|
/**
|
* 大淘客叮咚抢数据
|
*
|
* @return
|
*/
|
public static List<TaoBaoGoodsBrief> getDingDongQiang() {
|
List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
|
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"));
|
}
|
}
|
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 (IOException e) {
|
e.printStackTrace();
|
}
|
|
return goodsList;
|
|
}
|
|
//
|
|
public static List<TaoBaoCoupon> getRecommendCouple() {
|
List<TaoBaoCoupon> couponList = new ArrayList<>();
|
|
TaoBaoSearchResult result = new TaoBaoSearchResult();
|
result.setNavList(null);
|
JSONArray array = getGoods("http://dataoke.yeshitv.com/index.php?r=index/wap&u=487704");
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject data = array.optJSONObject(i);
|
TaoBaoCoupon coupon = new TaoBaoCoupon();
|
coupon.setAuctionId(data.optString("url"));
|
coupon.setAuctionUrl("");
|
coupon.setCouponAmount(new BigDecimal(data.optString("quan")));
|
coupon.setCouponCount(data.optInt("nums"));
|
coupon.setCouponinfo("");
|
coupon.setHongbao(new BigDecimal(0));
|
coupon.setPictUrl(data.optString("img").split(".jpg")[0] + ".jpg");
|
coupon.setTitle(data.optString("title"));
|
coupon.setZkPrice(new BigDecimal(data.optString("beforPrice")));
|
coupon.setCouponStartFee(new BigDecimal(0));
|
coupon.setQuanPrice(new BigDecimal(data.optString("afterPrice")));
|
couponList.add(coupon);
|
}
|
|
return couponList;
|
}
|
|
}
|