-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
472 lines (312 loc) · 10.6 KB
/
app.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//Node app for saving reddit content from user account.
//Copyright Jacob Beneski 2017
// 1) Create reddit helper object
// 2) Get saved content in a promise
// 3) Filter promise for wanted data
// 4) Export filtered data to desired format
const {app, BrowserWindow} = require('electron');
const http = require('http');
const url = require('url');
const ipcMain = require('electron').ipcMain;
const request = require('request');
const snoowrap = require('snoowrap');
const level = require('level');
require('dotenv').config();
const saver = require('../image-list-saver/image-saver');
var argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.help('h')
.alias('h', 'help')
.alias('l', 'limit')
.alias('o', 'output')
.alias('f', 'format')
.alias('g', 'gui')
.choices('f', ['csv', 'json', 'txt', 'html', 'excel'])
.alias('n', 'name')
.alias('d', 'download')
.describe('l', 'How far back to go in history.')
.describe('o', 'Choose the output location of the file.')
.describe('f', 'Format of file to output.')
.describe('n', 'The name of the output file.')
.describe('d', 'Download Images')
.describe('g', 'Enable or Disable the GUI')
.default('o', __dirname)
.default('f', 'html')
.default('n', 'output')
.default('g', true)
.argv;
//var config = require('./config');
const fh = require('./filehelper');
//TODO Check if GUI should be enabled.
let mainWindow;
//Setup Reddit OAuth Variable
//TODO move reddit oauth to external module
const REDDIT_AUTH = 'https://www.reddit.com/api/v1/authorize.compact';
const CLIENT_ID = process.env.CLIENT_ID;
const RESPONSE_TYPE = 'code';
let state = 'jacobacon-' + Math.floor(Math.random() * 10000);
const REDIRECT_URI = 'http://127.0.0.1:3000/auth/reddit/callback';
const DURATION = 'permanent';
const SCOPES = ['identity', 'edit', 'history', 'mysubreddits',
'privatemessages', 'read', 'save'];
let authURL = REDDIT_AUTH + '?client_id=' + CLIENT_ID + '&response_type=' + RESPONSE_TYPE +
'&state=' + state + '&redirect_uri=' + REDIRECT_URI + '&duration=' + DURATION + '&scope=' + SCOPES.join(' ');
let accessToken;
let refreshToken;
let timeToExpire;
//Used to get an access token
let authCode;
let callbackServer;
let db = level(__dirname + '/.database');
var content;
//End Variables
//When the app is ready, check access token is valid. Redirect to reddit, refresh or redirect to the app.
app.on('ready', () => {
//Load the Access Token From the Database
db.get('accessToken', (err, access) => {
//If it is not in DB, redirect the user to reddit login page.
if (err) {
console.error('No Valid Access Token, The User Needs to Provide Us Access' + '\n' + err);
showLogin();
} else {
accessToken = access;
db.get('refreshToken', (err, refresh) => {
if (err) {
console.error('No Refresh Token');
}
refreshToken = refresh;
if ((accessToken) && (refreshToken)) { //Access token and refresh token both exist.
showApp();
} else if (!refresh) { //Access Token is expired, refresh it.
//TODO Handle expired tokens.
console.log('No Refresh Token, you will need to login again in an hour.');
showApp();
}
});
}
});
mainWindow = new BrowserWindow({
height: 600,
width: 800,
show: false
});
});
ipcMain.on('get-comments', () => {
console.log('Getting Comments');
getContent((err, data) => {
if (err) {
console.error(err);
} else {
content = data;
mainWindow.webContents.send('set-comments', data);
//console.log(data);
}
});
});
ipcMain.on('logout', () => {
console.log('Logging Out...');
logout();
});
ipcMain.on('show-settings', () => {
console.log('Showing Settings');
showSettings();
});
ipcMain.on('export-content', () => {
console.log('Exporting Content');
processSaved(content);
});
ipcMain.on('download-images', ()=> {
console.log('Downloading Images');
let urls = [];
for(let i = 0; i < content.length; i++){
console.log(content[i].url);
urls.push(content[i].url);
}
try {
saver.download(urls, __dirname + '/downloads', (err, data) => {
console.log(err);
console.log(data);
})
} catch (error){
console.log(error);
}
});
function showLogin() {
mainWindow.loadURL(authURL);
mainWindow.show();
//Create a server for callback
callbackServer = http.createServer(function (req, res) {
console.log('Request:' + req.method + ' to ' + req.url);
res.writeHead(200, 'OK');
res.write('<h1>Recieved callback...</h1>');
res.end();
//Handle the Callback URL
handleCallback(req.url);
}).listen(3000);
console.log('Ready on port 3000');
}
function handleCallback(resp) {
console.log('We got: ' + resp);
if (resp.includes('error')) {
console.error('An Error Occured');
}
//TODO Change this to use correct URL parsing to prevent the code from being too short sometimes.
let returnState = resp.substring(28, 42);
authCode = resp.substring(48);
console.log('State: ' + returnState + ' code: ' + authCode);
if (state != returnState) {
console.error("Returned State Didn't Match Expected. Try to login again");
showLogin();
//TODO Handle this Problem better
} else {
getAccessToken(function (err, access, refresh) {
if (err) {
console.error(err);
}
accessToken = access;
refreshToken = refresh;
timeToExpire = ((new Date).getTime() / 1000) + 3600;
db.batch()
.put('accessToken', access)
.put('refreshToken', refresh)
.put('expireTime', timeToExpire)
.write((err) => {
console.error(err);
});
console.log('Stored Access Code in Database');
console.log('Showing the app');
console.log('Token Will Expire at: ' + timeToExpire);
showApp();
});
}
callbackServer.close();
}
function getAccessToken(callback) {
let tokenURL = 'https://' + CLIENT_ID + '@www.reddit.com/api/v1/access_token';
request.post(tokenURL, {
form: {
grant_type: 'authorization_code',
client_id: CLIENT_ID,
client_secret: '',
code: authCode,
redirect_uri: REDIRECT_URI
}, headers: {},
json: true
}, function (err, res, body) {
// assert.equal(typeof body, 'object')
if (err) {
callback(err, null, null)
} else if (body) {
let string = JSON.stringify(body);
let objectValue = JSON.parse(string);
console.log('The Access token is: ' + objectValue['access_token']);
let accessToken = objectValue['access_token'];
let refreshToken = objectValue['refresh_token'];
callback(null, accessToken, refreshToken);
}
})
}
/*
function processJSON(stringValue) {
let string = JSON.stringify(stringValue);
let objectValue = JSON.parse(string);
console.log('The Access token is: ' + objectValue['access_token']);
let accessToken = objectValue['access_token'];
let refreshToken = objectValue['refresh_token'];
}
*/
/*
const reddit = new snoowrap({
userAgent: config.userAgent,
clientId: config.clientId,
clientSecret: config.clientSecret,
refreshToken: config.refreshToken
});
if (!argv.limit) {
console.log('Going Back with No Limit');
reddit.getMe().getSavedContent({limit: Infinity, depth: Infinity}).then(function (saved) {
processSaved(saved);
});
} else {
console.log('Limit in place of: ', argv.limit);
reddit.getMe().getSavedContent({limit: argv.limit, depth: argv.limit}).then(function (saved) {
processSaved(saved);
});
}
*/
//TODO Move to helper class
function getContent(callback) {
const reddit = new snoowrap({
userAgent: 'reddit-downloader',
clientId: CLIENT_ID,
clientSecret: '',
refreshToken: refreshToken,
accessToken: accessToken
});
let content;
//Gets Saved Content from Reddit
reddit.getMe().getSavedContent({limit: Infinity, depth: Infinity}).then((savedContent) => {
//console.log(savedContent);
content = savedContent;
console.log('Calling Callback');
callback(null, content);
});
}
function processSaved(saved) {
console.log('Found ', saved.length, 'saved items.');
for (var i = 0; i < saved.length; i++) {
var line = [];
line.push(saved[i].title);
line.push(saved[i].url);
fh.buildArray(line);
}
console.log('Built Array');
fh.writeFile(argv.output, argv.name, 'html');
}
function showSettings(){/*
let settingsWindow = new BrowserWindow({parent: mainWindow, modal: true, show: false});
settingsWindow.loadURL('https://www.google.com');
settingsWindow.once('ready-to-show', () => {
settingsWindow.show();
});
console.log("Showing Settings")
*/
mainWindow.loadURL('file://' + __dirname + '/app/views/imageview.html');
}
function showApp() {
//mainWindow.loadURL('file://' + __dirname + '/app/views/index.html');
mainWindow.loadURL('file://' + __dirname + '/app/views/loading.html');
mainWindow.show();
getContent((err, data) => {
if (err)
console.error(err);
else {
console.log('Got Callback: ');
content = data;
mainWindow.loadURL('file://' + __dirname + '/app/views/index.html');
setTimeout(() => {
console.log('Sending Data');
mainWindow.webContents.send('set-comments', data);
}, 200);
console.log('Data Was Sent');
}
});
}
function logout() {
db.batch()
.del('accessToken')
.del('refreshToken')
.del('expireTime')
.write((err) => {
if (err)
console.error(err);
});
authCode = null;
accessToken = null;
refreshToken = null;
timeToExpire = null;
mainWindow.webContents.session.clearStorageData([], (data) => {
console.log('Cleared Login Data');
});
showLogin();
}