-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
194 lines (173 loc) · 6.6 KB
/
app.js
File metadata and controls
194 lines (173 loc) · 6.6 KB
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
// 初始化 redis
const Redis = require('ioredis');
const redis = new Redis({
port: 6379, // Redis port
host: "r-bp14pz4hoqv1zweufhpd.redis.rds.aliyuncs.com", // Redis host
password: "wQAQ9Ry2qkr8twM",
db: 10,
});
// 初始化 headless 浏览器
const PCR = require("puppeteer-chromium-resolver");
let pcr;
let browser;
(async () => {
pcr = await PCR();
browser = await pcr.puppeteer.launch({
defaultViewport: {
width: 100,
height: 100,
},
headless: true,
args: ['--no-sandbox'],
executablePath: pcr.executablePath
}).catch(function (error) {
console.log(error);
});
onReady()
})();
// 资源释放
process.on('exit', async function () {
await browser.close()
});
async function getAllCategoryUrls() {
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'),
await page.goto('https://www.brandymelvilleonline.com.cn/');
let categorys = await page.evaluate('[...document.querySelectorAll(\'.sticky-menu a\')].map(a => { return { href: a.href, name: a.innerText }}).filter(h => h.href.startsWith(\'https://www.brandymelvilleonline.com.cn/\'))');
// 移除父节点
const hrefList = categorys.map(c => c.href);
categorys = categorys.filter(c => {
let number = hrefList.find(h => h.includes(c.href + '/'));
if(number) {
return false;
}
// 去除上新和基础款重复
const exclude = [
'https://www.brandymelvilleonline.com.cn/basics',
'https://www.brandymelvilleonline.com.cn/just-in',
];
if(exclude.includes(c.href)) {
return false
}
return true;
});
await page.close();
return categorys;
}
async function getDetailUrl(url) {
const page = await browser.newPage();
await page.goto(url);
const realUrl = await page.evaluate('document.querySelector(\'.quick_view_link_container a\')?.href');
console.log(`getDetailUrl: ${url} => ${realUrl}`);
await page.close();
return realUrl;
}
async function fetchByUrls(urls) {
console.log(`=======fetchByUrls start.=======`);
console.time('fetchByUrls');
const page = await browser.newPage();
const items = [];
for(const url of urls) {
console.log(`=======开始获取 【 ${url.name} 】 列表=======`);
console.time(`=======开始获取 【 ${url.name} 】 列表=======`);
let nextHref = url.href;
while(nextHref) {
console.time(`fetch ${nextHref}`);
await page.goto(nextHref);
let map = (await page.evaluate('' +
'[...document.querySelectorAll(\'.products-grid .wrapper-items .product-item\')].map(a => {\n' +
' return {\n' +
' href: a.querySelector(\'.swiper-container .quickViewContainer\').href,\n' +
' cover: [...a.querySelectorAll(\'.swiper-container .swiper-wrapper .product-item-image a img\')].map(a => a.src),\n' +
' name: a.querySelector(\'.product-item-info .product-item-name a\').innerText,\n' +
' price: a.querySelector(\'.price-box .price\').innerText\n' +
' }\n' +
'})' +
''))
.map((a) => {
return Object.assign(a, {
category: url.name,
})
});
console.timeEnd(`fetch ${nextHref}`);
items.push(
...map
);
// 下一页连接
nextHref = await page.evaluate('document.querySelector(\'a.next\')?.href');
}
console.timeEnd(`=======开始获取 【 ${url.name} 】 列表=======`);
}
// {
// href: 'https://www.brandymelvilleonline.com.cn/catalog/ajax_product/view/id/10399',
// cover: [
// 'https://alicdn.brandymelvilleonline.com.cn/media/catalog/product/cache/3/image/414x621/9df78eab33525d08d6e5fb8d27136e95/m/h/mh369-z320s0020000_2.jpg',
// 'https://alicdn.brandymelvilleonline.com.cn/media/catalog/product/cache/3/image/414x621/9df78eab33525d08d6e5fb8d27136e95/m/h/mh369-z320s0020000_1x.jpg',
// 'https://alicdn.brandymelvilleonline.com.cn/media/catalog/product/cache/3/image/414x621/9df78eab33525d08d6e5fb8d27136e95/m/h/mh369-z320s0020000_1.jpg',
// 'https://alicdn.brandymelvilleonline.com.cn/media/catalog/product/cache/3/image/414x621/9df78eab33525d08d6e5fb8d27136e95/m/h/mh369-z320s0020000_2.jpg',
// 'https://alicdn.brandymelvilleonline.com.cn/media/catalog/product/cache/3/image/414x621/9df78eab33525d08d6e5fb8d27136e95/m/h/mh369-z320s0020000_1x.jpg'
// ],
// name: 'Helena Top',
// price: '¥ 160',
// category: '新款上市'
// }
const obj = {};
// 处理类型合并及ID提取
items.map(item => {
const splits = item.href.split('/');
const id = splits[splits.length - 1];
let objElement = obj[id];
if(!objElement) {
obj[id] = {
...item,
category: [item.category],
id
};
} else {
// 合并类型
objElement.category.push(item.category)
}
});
for(let objKey in obj) {
const good = obj[objKey];
const hset = await redis.hset('goods', good.id, JSON.stringify(good));
if(hset === 1) {
await page.goto(`https://tgbot.lbyczf.com/sendMessage/9qvms1saka190aje?text=${encodeURIComponent(
`
BrandyMelville 上新啦!
【 ${good.category} 】
【 ${good.name} 】
💰 【 ${good.price} 】
🔗 【 ${await getDetailUrl(good.href)} 】
`)}&photo=${good.cover[0]}`)
}
}
console.timeEnd('fetchByUrls');
console.log(`=======fetchByUrls end.=======`);
await page.close();
return 0;
}
async function onReady() {
async function mointGood() {
// 监控所有
// await fetchByUrls(await getAllCategoryUrls());
// // 监控包包
try {
await fetchByUrls([
{
name: '手提包&双肩包',
href: 'https://www.brandymelvilleonline.com.cn/accessories/bags-backpacks',
}
]);
} catch (e) {
console.error(e)
}
let s = Math.random() * 30;
let timeout = Math.ceil(s * 1000);
console.log(`${timeout}ms后再次刷新...`);
setTimeout(() => {
mointGood();
}, timeout);
}
mointGood();
}