-
Notifications
You must be signed in to change notification settings - Fork 1
/
website.ts
440 lines (376 loc) · 13.9 KB
/
website.ts
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
const accounts = require('./accounts.json');
const cheerio = require('cheerio');
const electron = require('electron');
const {remote} = require('electron');
const {session} = require('electron');
const {BrowserWindow} = require('electron');
const {ipcRenderer} = require('electron');
import * as request from "request-promise";
request.defaults({ jar: true });
interface SessionInfo {
rvt: string;
cookie: string;
payload?: any;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
//Debugging function
export function investigate(obj, space=0, path=obj, parent=null, grandparent=null){
console.log("Investigating "+path);
if (obj)
Object.keys(obj).forEach(function(k, i) {
try{
const type = typeof obj[k];
const recursive = (k=="parent" || k=="next" || k=="prev" || obj[k]==obj || obj[k]==parent || obj[k]==grandparent)
if (!recursive && space<5 && (type == 'object')) investigate(obj[k], space+1,path+'/'+k, obj, parent);
else
if (type == 'function') console.log(`${' '.repeat(space)} ${k}(${type})`);
else console.log(`${' '.repeat(space)} ${k}(${type}): ${obj[k]}`);
}
catch{console.log(k+" not viewable");}
});
}
//--------------------------------------------------------------
//--------------------------------------------------------------
//Webite, Object/Interface for websites and stuff.
export class website {
static test1(): string{return "inheritance works.<br/>\n"}
static test2(): string{return "overriding doesn't work.<br/>\n"}
static test3(): string{return "This is from the original Class...<br/>\n"}
static test4(): string{return this.test3()}
static initiated: boolean
static loaded: boolean
static window: any
static cookies: any[];
static siteName: string;
static account: any;
static username: string;
static password: string;
static service: string;
static session: any;
//Methods:
//-----------------------------------------------------------------
//------------------- UPDATE MAIN WINDOW --------------------------
//-----------------------------------------------------------------
static update(): void { //How much of this is even needed?
if (this.window){
const path = require('path');
const url = require('url');
this.window.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file',
slashes: true
})
)
} else if (document){
window.location.reload();
} else {
//const {ipcRenderer} = require('electron');
//ipcRenderer.send('please-refresh', "test");
}
}
//-----------------------------------------------------------------
//------------------- DO SETUP STUFF ------------------------------
//-----------------------------------------------------------------
//loadUpdateSettings, loads settings for when and what to update from file
static loadUpdateSettings(): void{
try{
this.username = accounts[this.siteName].username;
this.password = accounts[this.siteName].password;
}
catch{
this.username = "";
this.password = "";
}
}
//----------------------------------------------
//----------------------------------------------
//Initialize, sets up object
static init(): void{
console.error('Init():');
if (this.initiated==true) return;
this.initiated=true;
if (this.loaded==false) this.loadUpdateSettings();
//Open window to do web stuff.
try{
this.window = new BrowserWindow({width: 800, height: 600});
}
catch{
const { BrowserWindow } = require('electron').remote
this.window = new BrowserWindow({width: 800, height: 600});
}
// get all cookies
this.cookies = new Array
this.window.webContents.on('did-finish-load', () => {
this.window.webContents.session.cookies.get({}, async (error, cookies) => {
if (error) {
console.error('Error getting cookies:', error);
return;
}
// store cookies
cookies.forEach(c => {
//console.log("Cookie:"+JSON.stringify(c));
try{ this.cookies.push(c); }
catch(error){console.log("Failed: %s",error.message)}
});
})});
}
//-----------------------------------------------------------------
//--------------------- LOAD PAGE ---------------------------------
//-----------------------------------------------------------------
static async load(address, qs={}, method="GET"): Promise<string>{
console.log("Loading page: "+address+", qs:");
investigate(qs);
//Make sure that client is logged-in to RRL
this.session = this.session || await this.login_request();
//Set up basic options
let options={};
if (method=="GET"){
options = {
method: method,
uri: address,
qs: qs,
headers: { cookie: this.session.cookie }
};
}
//Special treatment for POST data
else if (method=="POST"){
const token = this.session.rvt
qs["__RequestVerificationToken:"] = token;
options = {
method: method,
uri: address,
form: qs,
headers: { cookie: this.session.cookie }
};
}
//send a request to provided address.
console.log("loading site: "+address);
return request(options).catch(function(err){
console.log("Error in load request! Request options: ");
console.log(options);
//Remove clutter from RRL error message
let txt = err.message;
txt = txt.split("col-md-12")[1]; //unique class before error message
txt = txt.split("Return home")[0]; //link text used after error message
txt = txt.replace(/<\/?p>..../g, "\n");
txt = txt.replace(/\\r\\n/g, "\n");
console.log("RRL Error message: "+txt);
return;
});
/*
try { return request(options); }
catch (err) { console.log("Error in load request: "+err.message); }
return;*/
}
//-----------------------------------------------------------------
//--------------------- SAVE TO SETTINGS --------------------------
//-----------------------------------------------------------------
//changeAccountDetails saves username and password in file.
//Encrypt later?
static changeAccountDetails(args=this): string{
return this._changeAccountDetails(args.service, args.username, args.password)
}
static _changeAccountDetails(service, username, password): string{
console.log(`${this.siteName}._changeAccountDetails(${service}, ${username}, ${password})`)
accounts[service]={};
accounts[service].username=username;
accounts[service].password=password;
investigate(accounts)
var fs = require('fs');
fs.writeFile("accounts.json", JSON.stringify(accounts, null, 2), function (err) {
if (err) return console.log(err);
console.log(JSON.stringify(accounts));
console.log('writing to ' + "accounts.json");
});
return "success"}
//Collecting data from the different RRL forms in one place seems reasonable.
//Can use it to consolidate all the different JSON files later.
static async saveSettings(args){
//Just add entries to local parameter object. There's probably a function for this.
for (const key in args){
if (key=="order" || key=="form") continue;
this[key]=args[key] //Maybe use parameters object here as well?
}
//TODO: add save-to-file here.
}
static async login_request(): Promise<SessionInfo> | undefined{
//Placeholder
return;
}
static async Login(
uri,
username: string = this.username,
password: string = this.password
): Promise<SessionInfo> | undefined {
//Placeholder
return;
}
}
//===============================================================
//===============================================================
//Object RRL, inherits from Site
export class RRL extends website{
static test(): string{return this.test1()+this.test2()+this.test3()+super.test3()+this.test4()}
static test2(): string{return "overwriting works.<br/>\n"}
static test3(): string{return "This is from the extended Class...<br/>\n"}
static siteName = "RRL";
static service = "RRL";
static fictions: any;
static releases: any;
static chapters: any;
static chaptersPerUpdate: number;
static hourOfDayToUpdate: number;
static weekdaysToUpdate: [];
static dateEachMonthToUpdate: number;
static daysBetweenUpdates: number;
static pageLoaded: string;
static baseURL = "https://deployment.royalroad.com";
static actionPage = "https://deployment.royalroad.com/account/betalogin";
static fictionsPage = "https://deployment.royalroad.com/my/fiction/";
static allFictionsPage = "https://deployment.royalroad.com/my/fictions/";
//static loginPage: "https://www.royalroad.com/account/login";
//static actionPage: "https://www.royalroad.com/account/externallogin";
//static fictionsPage = "https://www.royalroad.com/my/fictions/";
static session: any;
static userID: number;
static token: any;
static gettingToken: boolean;
static loggedIn: boolean;
static settingsLoaded=false;
static ipcMain: any;
static init(): void{
super.init();
}
//-----------------------------------------------------------------
//---------------------- LOG IN -----------------------------------
//-----------------------------------------------------------------
static async login_request(): Promise<SessionInfo> | undefined{
this.loadUpdateSettings();
console.log('Login Wrapper function... username: '+this.username);
if (this.loggedIn){
console.log("Aöready logged in. ");
return this.session;
}
this.session = await this.Login(this.actionPage, this.username, this.password);
//console.log("SESSION1: "+JSON.stringify(ret, null, 2));
this.userID = await this.GetUserId();
//console.log("SESSION2: "+JSON.stringify(ses, null, 2));
if (this.userID){
this.loggedIn=true;
console.log("Logged in. ");
}
return this.session;
}
//----------------------------------------------
//----------------------------------------------
static async Login(
uri: string = this.actionPage,
username: string = this.username,
password: string = this.password
): Promise<SessionInfo> | undefined {
console.log(`Logging in to '${uri}' as '${username}', with pw: '${password}'.`);
try {
const session: SessionInfo = await RRL.GetCookieAndRVT(uri);
if (!session) {
console.log("Failed to get Cookie and RVT.");
return;
}
const options = {
method: "POST",
simple: false,
uri,
form: {
email: username,
password: password,
remember: "false",
__RequestVerificationToken: session.rvt
},
headers: {
cookie: session.cookie
},
resolveWithFullResponse: true
};
const res = await request(options);
session.cookie = res.headers["set-cookie"];
session.payload = {
...session.payload,
username
};
return session;
}
catch (err) {
console.log("Error in login: "+err.message);
console.log(JSON.stringify(err, null, 2));
return;
}
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Get Cookie and __RequestVerificationToken
static async GetCookieAndRVT(uri: string): Promise<SessionInfo> | undefined {
console.log("Getting Cookie and RVT at URI '"+uri+"'");
try {
const options = {
method: "GET",
uri,
resolveWithFullResponse: true
};
const res = await request(options);
const cookie = res.headers["set-cookie"];
const rvtName: string = "__RequestVerificationToken";
const $ = cheerio.load(res.body);
const rvt = $(`input[name=${rvtName}]`).val();
this.cookies = cookie;
this.token = rvt;
return {
rvt,
cookie
};
} catch (err) {
console.log("Error in GetCookieAndRVT: "+err.message);
console.log(JSON.stringify(err, null, 2));
return;
}
}
//----------------------------------------------
//----------------------------------------------
static async GetUserId(
session: SessionInfo = this.session,
uri: string = this.baseURL
): Promise<number> | undefined {
console.log('Getting ID...');
//console.log('Cookie: '+session.cookie);
try {
const options = {
method: "GET",
uri: uri,
headers: {
cookie: session.cookie
}
};
const res = await request(options);
if (!session.payload.id) {
const $ = cheerio.load(res, { xmlMode: false });
// Positive Lookbehind Regular Expression. Thank you Regex101
let reg = /(?<=window.userId\s=\s)\d\d\d\d\d/gm;
let matches = reg.exec(res);
if (!!matches) {
session.payload = {
...session.payload,
userId: matches[0]
};
}
}
console.log("user id = "+session.payload.userId)
this.session = session;
return session.payload.userId;
}
catch (err) {
console.log("Error: "+err.message);
console.log(JSON.stringify(err, null, 2));
return;
}
}
}
module.exports = RRL;