-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.js
97 lines (83 loc) · 2.27 KB
/
init.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
var rp = require('request-promise');
var cheerio = require('cheerio');
var talks;
function scrapeTalks(){
const url = 'http://nordicapis.com/events/2016-platform-summit/';
rp(url).then( (html)=>{
var $ = cheerio.load(html);
talks = $('.schedliveinfo').parent().map((i,el)=>{
return {
'title': $(el).find('.schedliveinfo h3').text(),
'location': $(el).find('.schedliveinfo .loc').text(),
'time': $(el).find('.schedlivetime').text().replace(/\n/g, "").trim(),
'speakerURLs': $(el).find('.schedlivespeaker a').attr('href'),
};
});
console.log('Loaded ' + talks.length + ' talks');
})
.catch((err) => {console.log(err)});
}
function scrapeSpeaker(url){
const talkerURL = 'http://nordicapis.com/' + url;
return rp(talkerURL).then((html) => {
console.log("fetched speaker"+ url)
var $ = cheerio.load(html);
return {
'name': $('.basicinfo h1').text().trim(),
'role': $('.basicinfo h4').text().trim(),
'company': $('.basicinfo h5').text().trim(),
'handle': $('.fa-twitter-outline + a').text().trim(),
'description': $('.speakerdesc').text().replace(/\n/g, "").trim(),
};
})
.catch((err) => {console.log(err) });
}
scrapeTalks();
var { graphql, buildSchema } = require('graphql');
var typeDefs = `
type Query{
me: Speaker
speaker(id: String): Speaker
talks: [Talk]
}
type Speaker {
name: String
role: String
company: String
handle: String
description: String
url: String
}
type Talk {
title: String
location: String
time: String
speakers: [Speaker]
}
`;
const resolvers = {
Query:{
me: ()=>(scrapeSpeaker('speakers/joakim-lundborg')),
speaker: (id)=>(scrapeSpeaker(id)),
talks: () => { return talks },
},
Talk:{
speakers: (talk)=>{
return [scrapeSpeaker(talk.speakerURLs)]
}
}
}
var { makeExecutableSchema } = require('graphql-tools');
var schema = makeExecutableSchema({
typeDefs: typeDefs,
resolvers,
});
const express = require('express');
const graphqlHTTP = require('express-graphql');
const app = express();
app.use('/graphql', graphqlHTTP({
schema: schema ,
graphiql: true,
}));
app.listen(4000);
console.log('Starting server on port 4000');