-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.js
220 lines (197 loc) · 6.49 KB
/
https.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const https = require('https');
const writeFile = require('./fs.js').writeInCurrentFolder;
/**
* const values
*/
// 用于获取items地址,在函数generateVdianItemInListURL中有使用
const VDIAN_ITEM_URL = 'https://wd.api.weidian.com/wd/cate/getItemsForBuyer';
// 用于获取店铺所有分类号的地址,在generateVdianListURL中使用
const VDIAN_LIST_URL = 'https://wd.api.weidian.com/wd/cate/getList';
function getAllItemsAndDetail(userID) {
return getAllItemInShop(userID).then(getItemsInDetailFromListArray);
}
function getAllItemInShop(userID) {
var promise = new Promise((resolve, reject) => {
let result = [];
const listsInShop = getListsInShop(userID)
.then(listArray => {
let len = listArray.length;
let promiseContainer = [];
listArray.map(list => {
var itemsInList = getItemsInList(userID, list.cate_id, list.cate_item_num)
.then(val => {
val.cateInfo = list;
return val;
})
promiseContainer.push(itemsInList);
});
Promise.all(promiseContainer)
.then(result => {
resolve(result);
})
.catch(reject);
})
});
return promise;
}
function getItemsInDetailFromListArray(listArray) {
// 获取item中的详细信息(介绍图片地址,图片信息等)
var promiseContainer = [];
listArray.map(itemList => {
itemList.map(item => {
let itemDetail = getItemDetail(item.itemID)
.then(itemDetail => {
item.detailInfo = itemDetail;
return item;
});
promiseContainer.push(itemDetail);
});
})
return Promise.all(promiseContainer);
}
// 用于获取List分类下所有的商品信息
function generateCommodityURL(URL, userID, typeNum, start = 0, needNum = 100) {
var param = {
userID,
cate_id: typeNum,
limitStart: start,
limitNum: needNum
};
const httpReg = /https:\/\//i;
if (!httpReg.test(URL)) URL = 'https://' + URL;
return URL + '?param=' + JSON.stringify(param);
}
/**
* 返回一个用于获取List中包含Item信息的URL
* @param {int} userID 店家的ID
* @param {int} listNum 店家的分类号(List号)
* @param {int} itemNumInList 该分类下的item数目,默认值为10
* @return {string} 一个URL地址,返回值为分类下的item地址
*/
function generateVdianItemInListURL(userID, listNum, itemNumInList) {
return generateCommodityURL(VDIAN_ITEM_URL, userID, listNum, 0, itemNumInList);
}
/**
* 生成一个用于获取店铺下所有分类的URL地址
* @param {int} userID 店铺的ID(店家的ID)
* @return {string} 用于获取所有分类号的URL地址
*/
function generateVdianListURL(userID) {
return generateCommodityURL(VDIAN_LIST_URL, userID);
}
/**
* 生成一个用于获取item详细信息的URL地址
* @param {int} itemID 想要获取的item号
* @return {string} item的URL地址
*/
function generateVdianItemURL(itemID) {
return 'https://weidian.com/item.html?itemID=' + itemID + '&p=-1';
}
const itemsInListTestString = 'https://wd.api.weidian.com/wd/cate/getItemsForBuyer?param={"userID":"1686060","cate_id":"72843023","limitStart":0,"limitNum":70}';
const itemDetailTestURL = generateVdianItemURL(1848302313);
/**
* 用于发起https请求,并将结果保持在Promise中
* @param {string} requestOptions 请求的URL地址
* @return {Promise} 包含有请求结果的Promise对象
*/
function httpsRequest(requestOptions) {
var promise = new Promise((resolve, rejece) => {
var results = '';
var req = https.request(requestOptions, res => {
res.setEncoding('utf8');
console.log('https request send, url is ' + requestOptions);
res.on('data', data => {
results += data
});
})
req.on('close', _ => resolve(results));
req.on('end', _ => resolve(results))
req.end();
req.on('error', error => rejece(error));
})
return promise;
}
function getListsInShop(userID) {
var requestURL = generateVdianListURL(userID);
var promise = httpsRequest(requestURL).then(getListInfoFromString);
return promise;
}
function getListInfoFromString(str) {
var obj = JSON.parse(str);
var obj = obj.result;
var result = obj.map(val => ({
cate_id: val.cate_id,
cate_name: val.cate_name,
cate_item_num: val.cate_item_num,
description: val.description,
sort_num: val.sort_num,
}));
return result;
}
function getItemsInList(userID, listID, itemNum) {
var requestURL = generateVdianItemInListURL(userID, listID, itemNum);
var promise = httpsRequest(requestURL)
.then(extractItemsInListDetail);
return promise;
}
function extractItemsInListDetail(str) {
var obj = JSON.parse(str);
return obj.result.map(simplifyItemsInfoInList)
}
function simplifyItemsInfoInList(val) {
return {
itemID: val.itemID,
itemDescription: val.itemName,
itemPrice: val.price,
itemNumInStock: val.stock,
itemImg: imgDeleteParam(val.img),
itemCategory: val.cates
};
}
function getItemDetail(itemID) {
var requestURL = generateVdianItemURL(itemID);
var promise = httpsRequest(requestURL)
.then(getItemInfoFromHtmlString)
return promise;
}
function getItemInfoFromHtmlString(htmlInString) {
// if input is Buffer, transmit it to String;
htmlInString += '';
// extrace title
var itemNameReg = /<title>(.*)<\/title>/i;
var itemName = htmlInString.match(itemNameReg)[1];
// 微店的数据是放在最下面的表达式中,将其提取出来
var itemInfoReg = /(var itemInfo = {[\s\S]*?)};/i;
var itemInfo = htmlInString.match(itemInfoReg)[0];
// get itemInfo;
eval(itemInfo);;
var itemSimpleInfo = simplifItemInfo(itemInfo);
itemSimpleInfo.itemName = itemName;
// delete unused information
return itemSimpleInfo;
}
// 用于将服务器上获取的item详细信息进行简化,删除不需要的键值
function simplifItemInfo(itemInfo) {
var result = itemInfo.result;
var obj = {
itemID: result.itemID,
itemLogo: imgDeleteParam(result.itemLogo),
itemDescription: result.itemName,
itemPrice: result.price,
remote_area: result.remote_area,
itemImgs: result.thumbImgs.map(imgDeleteParam),
itemImgsDescription: result.titles,
sku: result.sku,
};
return obj;
}
function imgDeleteParam(URL) {
const URLReg = /https:\/\/[\w\.\-\/]*/ig;
return URL.match(URLReg)[0];
}
module.exports = {
getAllItemInfo: getAllItemsAndDetail,
getItemDetail: getItemDetail,
getListsInShop: getListsInShop,
getItemsInList: getItemsInList,
};