package com.yeshi.fanli.util.dataoke;
|
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.select.Elements;
|
import org.yeshi.utils.HttpUtil;
|
|
import com.google.gson.Gson;
|
import com.yeshi.fanli.dto.dataoke.DaTaoKeApiResult;
|
import com.yeshi.fanli.dto.taobao.TaoBaoShopDTO;
|
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetail;
|
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class DaTaoKeApiUtil {
|
final static String API_KEY = "a083abb893";
|
final static String API_KEY_2 = "b7a5ea2cd9";
|
static Gson gson = new Gson();
|
|
public static DaTaoKeApiResult goodsList(int page) {
|
List<DaTaoKeDetail> list = new ArrayList<>();
|
String url = String.format("http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=%s&v=2&page=%s",
|
Math.random() > 0.5 ? API_KEY : API_KEY_2, page + "");
|
String result = HttpUtil.get(url);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
System.out.println(resultJson);
|
JSONArray array = resultJson.optJSONArray("result");
|
for (int i = 0; i < array.size(); i++) {
|
list.add(gson.fromJson(array.optJSONObject(i).toString(), DaTaoKeDetail.class));
|
}
|
return new DaTaoKeApiResult(resultJson.optJSONObject("data").optInt("total_num"),
|
resultJson.optJSONObject("data").optString("update_time"), list);
|
}
|
|
|
/**
|
* 获取大淘客品牌优选 品牌id
|
* @return
|
*/
|
public static List<String> getBrandIdList() {
|
List<String> listId = null;
|
try {
|
Document doc = Jsoup.connect("http://www.dataoke.com/brandFeature").get();
|
Elements els = doc.getElementsByTag("script");
|
for (int i = 0; i < els.size(); i++) {
|
if (els.get(i).html().contains("var brandData")) {
|
|
JSONObject data = JSONObject
|
.fromObject(els.get(i).html().replace("var brandData =", "").trim().split("};")[0] + "}");
|
listId = convertList(data);
|
break;
|
}
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return listId;
|
}
|
|
|
public static List<String> convertList(JSONObject data) {
|
List<String> listId = new ArrayList<String>();
|
|
// 萌趣新生力 夏日好食光栏目
|
JSONArray arrayActs = data.optJSONObject("acts").optJSONArray("list");
|
for (int i = 0; i < arrayActs.size(); i++) {
|
JSONObject item = arrayActs.optJSONObject(i);
|
listId.add(item.optString("brand_id"));
|
}
|
|
// 热推爆款
|
JSONArray arrayPushs = data.optJSONObject("pushs").optJSONArray("list");
|
for (int i = 0; i < arrayPushs.size(); i++) {
|
JSONObject item = arrayPushs.optJSONObject(i);
|
listId.add(item.optString("brand_id"));
|
}
|
|
// 热销品牌榜
|
JSONArray arrayRanks = data.optJSONObject("ranks").optJSONArray("list");
|
for (int i = 0; i < arrayRanks.size(); i++) {
|
JSONObject item = arrayRanks.optJSONObject(i);
|
// long id = item.optLong("id");
|
// String title =item.optString("title");
|
JSONArray goods = item.optJSONArray("goods");
|
for (int j = 0; j < goods.size(); j++) {
|
JSONObject good = goods.optJSONObject(j);
|
listId.add(good.optString("brand_id"));
|
}
|
}
|
return listId;
|
}
|
|
|
/**
|
* 获取店铺下前4个商品、店铺介绍
|
* @param brandId
|
* @return
|
*/
|
public static TaoBaoShopDTO getDynamicShopInfo(String brandId) {
|
if (brandId == null) {
|
return null;
|
}
|
TaoBaoShopDTO taoBaoShopDTO = null;
|
|
Document doc;
|
try {
|
doc = Jsoup.connect("http://www.dataoke.com/brandSingle?id=" + brandId).get();
|
Elements els = doc.getElementsByTag("script");
|
for (int i = 0; i < els.size(); i++) {
|
if (els.get(i).html().contains("var brandData")) {
|
|
JSONObject data = JSONObject
|
.fromObject(els.get(i).html().replace("var brandData =", "").trim().split("};")[0] + "}");
|
taoBaoShopDTO = convertTaoBaoShopDTO(data);
|
break;
|
}
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return taoBaoShopDTO;
|
|
}
|
|
public static TaoBaoShopDTO convertTaoBaoShopDTO(JSONObject data) {
|
TaoBaoShopDTO taoBaoShopDTO = new TaoBaoShopDTO();
|
JSONObject item = data.optJSONObject("act");
|
taoBaoShopDTO.setSellerId(item.optLong("seller_id"));
|
taoBaoShopDTO.setBrandId(item.optString("brand_id"));
|
taoBaoShopDTO.setBrandDes(item.optString("brand_des"));
|
int userType = 0;
|
|
List<TaoBaoGoodsBrief> listGoods = new ArrayList<TaoBaoGoodsBrief>();
|
JSONArray arrayGoods = data.optJSONArray("goods");
|
for (int i = 0; i < arrayGoods.size(); i++) {
|
if (i > 3) {
|
break;
|
}
|
JSONObject itemGoods = arrayGoods.optJSONObject(i);
|
TaoBaoGoodsBrief taoBaoGoods = new TaoBaoGoodsBrief();
|
taoBaoGoods.setAuctionId(itemGoods.optLong("goodsid"));
|
taoBaoGoods.setBiz30day(itemGoods.optInt("xiaoliang"));
|
taoBaoGoods.setCouponAmount(new BigDecimal(itemGoods.optString("quan_jine")));
|
taoBaoGoods.setCouponInfo(String.format("满%s元减%s元", itemGoods.optString("quan_tiaojian"),
|
MoneyBigDecimalUtil.getWithNoZera(new BigDecimal(itemGoods.optString("quan_jine")))));
|
taoBaoGoods.setCouponLeftCount(itemGoods.optInt("quan_num"));
|
taoBaoGoods.setCouponStartFee(new BigDecimal(itemGoods.optString("quan_tiaojian")));
|
taoBaoGoods.setCouponTotalCount(itemGoods.optInt("quan_num"));
|
taoBaoGoods.setPictUrl(itemGoods.optString("pic"));
|
taoBaoGoods.setPictUrlWhite(itemGoods.optString("pic"));
|
taoBaoGoods.setSellerId(itemGoods.optLong("seller_id"));
|
taoBaoGoods.setShopTitle("");
|
taoBaoGoods.setTitle(itemGoods.optString("title"));
|
taoBaoGoods.setUserType(itemGoods.optInt("istmall"));
|
taoBaoGoods.setZkPrice(new BigDecimal(itemGoods.optString("yuanjia")));
|
taoBaoGoods.setTkRate(new BigDecimal(itemGoods.optString("yongjin")));
|
taoBaoGoods.setTkCommFee(new BigDecimal("0"));
|
taoBaoGoods.setState(0);
|
|
listGoods.add(taoBaoGoods);
|
|
userType = itemGoods.optInt("istmall");
|
}
|
taoBaoShopDTO.setListGoods(listGoods);
|
taoBaoShopDTO.setUserType(userType);
|
return taoBaoShopDTO;
|
}
|
|
class DaTaoKeResult {
|
Date updateTime;
|
List<DaTaoKeDetail> dataList;
|
|
public DaTaoKeResult(Date updateTime, List<DaTaoKeDetail> dataList) {
|
this.updateTime = updateTime;
|
this.dataList = dataList;
|
}
|
|
public DaTaoKeResult() {
|
|
}
|
}
|
|
}
|