-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (50 loc) · 1.43 KB
/
index.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
const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const {buildSchema} = require('graphql');
const cheerio = require('cheerio');
const got = require('got');
const schema = buildSchema(`
type Query {
scraper (url: String): Scraper
}
type Scraper {
selectString(of: String!, attribute: String): String
selectList(of: String!, limit: Int = 5, ignoreEmpty:Boolean = false): [Scraper]
size:Int
raw:String
url:String
}
`);
const scraper = async ({url}) => {
const response = await got(url);
const $ = cheerio.load(response.body);
return {
url: () => url,
size: () => response.body.length,
raw: () => response.body,
selectString: ({of, attribute}) => (attribute ? $(of).attr(attribute) : $(of).text()),
selectList: ({of, limit, ignoreEmpty}) => {
let list = [];
$(of).each(function (i, e) {
if(list.length>=limit) return;
if(ignoreEmpty && !$(this).text().length) return;
list.push({
raw: () => $(this).html(),
selectString: ({of, attribute}) => (attribute ? $(this).find(of).attr(attribute) : $(this).find(of).text()),
});
});
return list;
},
}
};
const root = {
scraper
};
const app = express();
app.use('/graphql', graphqlHTTP({
schema : schema,
rootValue: root,
graphiql : true,
}));
app.listen(4000);
console.log(`🚀 Server ready at http://localhost:4000/graphql`);