admin
2019-05-14 aa784ab65cc24caf45a4c38af7e5fdeb526eb393
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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() {
 
        }
    }
 
}