-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·82 lines (74 loc) · 2.26 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
const request = require("request-promise-native");
const tough = require("tough-cookie");
const fs = require("fs");
const getToken = () => process.env.CFP_TOKEN;
const constructCookie = () =>
// Put cookie in an jar which can be used across multiple requests
request.jar().setCookie(
new tough.Cookie({
key: "token",
value: getToken(),
domain: "api.cfp.io",
httpOnly: true,
maxAge: 31536000
}),
"https://api.cfp.io"
);
const getData = () => {
const value = {};
// Easy creation of the cookie - see tough-cookie docs for details
const cookie = constructCookie();
// ...all requests will include the cookie
const generateHttpCall = endpoint =>
Object.assign(
{},
{
method: "GET",
uri: `https://api.cfp.io/v0/${endpoint}`,
headers: {
Origin: "https://devfestnantes.cfp.io",
Referer: "https://devfestnantes.cfp.io/",
Cookie: `token=${getToken()}`
},
jar: cookie, // Tells rp to include cookies in jar that match uri,
json: true,
resolveWithFullResponse: false
}
);
return request(generateHttpCall("tracks"))
.then(data => Object.assign(value, { tracks: data }))
.then(() => request(generateHttpCall("formats")))
.then(data => Object.assign(value, { formats: data }))
.then(() => request(generateHttpCall("admin/sessions")))
.then(data => {
console.log(
`👷 Welcome ! Conf has ${value.tracks.length} tracks and ${value.formats
.length} formats`
);
return Object.assign(value, { sessions: data });
})
.catch(err => {
console.log("💩 AieAieAie!\n", err);
process.exit();
});
};
const transform = data => session =>
Object.assign(session, {
formatLabel: data.formats.find(({ id }) => id === session.format).name
});
const start = getData => {
getData()
.then(data => {
console.log(`📢 Format: ${JSON.stringify(data)}`);
return data;
})
.then(data => data.sessions.map(transform(data)) && data)
.then(data => fs.writeFileSync("./sessions.json", JSON.stringify(data)))
.catch(err => {
console.log("💩 AieAieAie!\n", err);
});
};
if (require.main === module) {
return start(getData);
}