-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
61 lines (49 loc) · 1.56 KB
/
crawler.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
const Crawler = require("crawler");
const fs = require("fs");
const NodeCache = require("node-cache");
const myCache = new NodeCache();
module.exports = function (param = "", response, flag='index') {
if (myCache.has(flag)) {
response.json(myCache.take(flag));
return;
}
Arr = [];
const c = new Crawler({
maxConnections: 10,
callback: function (error, res, done) {
if (error) {
console.log(error);
done();
}
fuSelector(res, Arr);
console.log('We crawl again !!!');
myCache.set(flag, Arr, 20000);
response.json(Arr);
done();
},
});
const fuSelector = (res, Arr) => {
const $ = res.$;
$(".photo-grid-item")
.find("a img")
.each((index, item) => {
if(index % 2 !== 0)
{
return;
}
let photo =
item.attribs.src === undefined
? item.attribs["data-cfsrc"]
: item.attribs.src;
obj = {
photo: photo.includes("https://cdn.stocksnap.io/") ? photo : "",
width: item.attribs.width,
height: item.attribs.height,
description:
item.attribs.alt === undefined ? "unknown" : item.attribs.alt,
};
Arr.push(obj);
});
};
c.queue("https://stocksnap.io/" + param);
};