-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (91 loc) · 2.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const app = require('express')();
const express = require('express');
const nunjucks = require('nunjucks');
const snoowrap = require('snoowrap');
const json2csv = require('json2csv');
const r = new snoowrap({
userAgent: '',
clientId: '',
clientSecret: '',
refreshToken: ''
});
r.config({
requestDelay: 500
});
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.use(express.static('static'));
app.get('/', (request, resolve) => {
/* No request made yet; render the page */
if (!request.query.hasOwnProperty('threadId')) {
resolve.render('index.njk');
return;
}
/* Request made; make the API call */
r.getSubmission(request.query.threadId).expandReplies({
limit: Infinity,
depth: Infinity
}).then((response) => {
/* Convert response to JSON */
let json = thread2json(response);
/* Convert JSON to CSV */
let csv = json2csv({
data: json,
fields: ['author', 'content', 'timestamp']
});
/* Set browser file headers */
resolve.set({
"Content-Disposition": "attachment; filename=" + request.query.threadId + ".csv"
});
/* Send CSV */
resolve.send(csv);
});
});
app.listen(3000);
/**
* Converts raw reddit API response to JSON object
*
* @param response
* @return object
*/
function thread2json(redditResponse) {
let output = [
reduceReply(redditResponse)
];
for (let c = 0; c < redditResponse.comments.length; c++) {
dig(output, redditResponse.comments[c]);
}
return output;
}
/**
* Recursively adds thread responses to
* the given container array
*/
let dig = function(container, reply) {
if (reply.body != '[removed]' && reply.body != '[deleted]' && reply.body.length > 0) {
container.push(reduceReply(reply));
}
if (reply.replies.length === 0) {
return;
}
for (let r = 0; r < reply.replies.length; r++) {
dig(container, reply.replies[r]);
}
}
/**
* Throws out extraneous reply info
*
* @param reddit reply
* @return object
*/
let reduceReply = function(reply) {
let day = new Date(reply.created * 1000);
return {
author: reply.author.name,
content: reply.hasOwnProperty('selftext') ? reply.selftext : reply.body,
timestamp: day.toString()
}
}