Repository navigation
Expand file tree
/
Copy pathscrape.js
More file actions
69 lines (57 loc) · 1.81 KB
/
Copy pathscrape.js
File metadata and controls
69 lines (57 loc) · 1.81 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
const request = require('request'),
cheerio = require('cheerio');
module.exports.search = ( url, arg, callback ) => {
request(url, ( err, resp, body ) => {
if (err) {
return callback({
type: 'error-request',
status: {
txt: '',
code: 404,
bool: false
},
msg: err.message
}, undefined);
}
else if (resp.statusCode !== 200) {
return callback({
type: 'http-request',
status: {
txt: '',
code: 404,
bool: false
},
msg: 'request was refused website does not exist; not found.'
}, undefined);
}
const $ = cheerio.load(body),
pics = $('img'),
data = {
length: 0,
resp: [],
msg: `Searching for results for ${ arg } memes results`,
status: {
txt: 'OK',
code: 200,
bool: true
}
};
data['length'] = pics['length'];
if ( data['length'] === 0 ) {
data.msg = `No results ${ arg } memes results.`;
data.status = {
code: 404,
bool: false,
txt: ''
};
return callback(null, data);
}
pics.each(function( i ) {
var url_name = pics[i].name;
if ( url_name === 'img' ) {
data.resp.push( pics[i].attribs );
}
});
return callback(null, data);
})
}