forked from hearhellacopters/tablo2plex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
152 lines (116 loc) · 5.05 KB
/
Copy pathapp.js
File metadata and controls
152 lines (116 loc) · 5.05 KB
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
// @ts-check
const {
exit
} = require('./src/CommandLine');
const {
C_HEX,
CONST
} = require('./src/Constants');
const {
reqCreds,
makeLineup,
readCreds,
parseLineup,
cacheGuideData
} = require('./src/Device');
const FS = require('./src/FS');
const Logger = require('./src/Logger');
const Scheduler = require('./src/Scheduler');
const {runServer} = require('./src/Transmissions');
/**
* @type {Scheduler}
*/
var LINEUP_SCHEDULER;
/**
* @type {Scheduler}
*/
var GUIDE_SCHEDULER;
// Starts server
(async function () {
CONST.init();
if (CONST.ARGV.lineup) {
// rerun line pull
// creates new Scheduler file
Logger.info(`${C_HEX.yellow}Running forced one-time lineup/guide update...${C_HEX.reset}`);
if (!FS.fileExists(CONST.CREDS_FILE)) {
// creds need setting up
Logger.info(`No creds file found. Lets log into your Tablo account.`);
Logger.info(`${C_HEX.red}NOTE:${C_HEX.reset} Your password and email are never stored, but are transmitted in plain text.`);
Logger.info(`Please make sure you are on a trusted network before you continue.`);
await reqCreds();
}
LINEUP_SCHEDULER = new Scheduler(CONST.SCHEDULE_LINEUP, "Update channel lineup", CONST.LINEUP_UPDATE_INTERVAL, makeLineup);
await LINEUP_SCHEDULER.runTask();
if (CONST.CREATE_XML) {
GUIDE_SCHEDULER = new Scheduler(CONST.SCHEDULE_GUIDE, "Update guide data", CONST.GUIDE_UPDATE_INTERVAL, cacheGuideData);
await GUIDE_SCHEDULER.runTask();
}
Logger.info(`${C_HEX.green}Forced update complete.${C_HEX.reset}`);
await exit();
} else if (CONST.ARGV.creds) {
// creds need setting up
Logger.info(`${C_HEX.yellow}Running forced one-time credentials creation...${C_HEX.reset}`);
Logger.info(`${C_HEX.red}NOTE:${C_HEX.reset} Your password and email are never stored, but are transmitted in plain text.`);
Logger.info(`Please make sure you are on a trusted network before you continue.`);
LINEUP_SCHEDULER = new Scheduler(CONST.SCHEDULE_LINEUP, "Update channel lineup", CONST.LINEUP_UPDATE_INTERVAL, makeLineup);
await LINEUP_SCHEDULER.runTask();
Logger.info(`${C_HEX.green}Forced credentials creation complete.${C_HEX.reset}`);
await exit();
} else {
// Then run the server.
if (!FS.fileExists(CONST.CREDS_FILE)) {
// creds need setting up
Logger.info(`No creds file found. Lets log into your Tablo account.`);
Logger.info(`${C_HEX.red}NOTE:${C_HEX.reset} Your password and email are never stored, but are transmitted in plain text.`);
Logger.info(`Please make sure you are on a trusted network before you continue.`);
await reqCreds();
}
if (process.stdin.isTTY) {
console.log(`${C_HEX.yellow}-- Press '${C_HEX.green}x${C_HEX.yellow}' at anytime to exit.${C_HEX.reset}`);
console.log(`${C_HEX.yellow}-- Press '${C_HEX.green}l${C_HEX.yellow}' at anytime to request a new channel lineup / guide.${C_HEX.reset}`);
}
try {
await readCreds();
LINEUP_SCHEDULER = new Scheduler(CONST.SCHEDULE_LINEUP, "Update channel lineup", CONST.LINEUP_UPDATE_INTERVAL, makeLineup);
await LINEUP_SCHEDULER.scheduleNextRun();
await parseLineup();
if (CONST.CREATE_XML) {
GUIDE_SCHEDULER = new Scheduler(CONST.SCHEDULE_GUIDE, "Update guide data", CONST.GUIDE_UPDATE_INTERVAL, cacheGuideData);
await GUIDE_SCHEDULER.scheduleNextRun();
}
} catch (error) {
Logger.error("Could not read lineup file. Check permissions and rerun app with --lineup.");
return await exit();
}
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', async (key) => {
if (key.toString() == 'x' || key.toString() == 'X') { // x key
if (LINEUP_SCHEDULER) {
LINEUP_SCHEDULER.cancel();
}
if (GUIDE_SCHEDULER) {
GUIDE_SCHEDULER.cancel();
}
console.log(`${C_HEX.blue}Exiting Process...${C_HEX.reset}`);
setTimeout(() => {
process.exit(0);
}, 2000);
} else if (key.toString() == "l" || key.toString() == "L") { // l key
if (LINEUP_SCHEDULER) {
await LINEUP_SCHEDULER.runTask();
}
if (GUIDE_SCHEDULER) {
await GUIDE_SCHEDULER.runTask();
}
}
});
} else {
Logger.info('Running as service - no interactive key controls.');
}
// Core function here
runServer();
}
})();