-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
230 lines (186 loc) · 6.09 KB
/
index.tsx
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
import {Context, Hono} from 'hono';
import {serve} from '@hono/node-server';
import {serveStatic} from '@hono/node-server/serve-static';
import {BlankEnv, BlankInput} from 'hono/types';
import {html} from 'hono/html';
import {generateM3u} from './services/generate-m3u';
import {initDirectories} from './services/init-directories';
import {generateXml} from './services/generate-xmltv';
import {scheduleEntries} from './services/build-schedule';
import {cleanEntries} from './services/shared-helpers';
import {SERVER_PORT} from './services/port';
import { miscDbHandler } from './services/misc-db';
import { db } from './services/database';
import { IChannel, IMisc } from './services/shared-interfaces';
import {peacockHandler} from './services/peacock-handler';
import {abcHandler} from './services/abc-handler';
import { Layout } from './views/Layout';
import { Header } from './views/Header';
import {Main} from './views/Main';
import { Links } from './views/Links';
import { Prefix } from './views/Prefix';
import {Providers} from './views/Providers';
import {Provider} from './views/Provider';
import {Style} from './views/Style';
import {Script} from './views/Script';
import {version} from './package.json';
const PROVIDER_MAP = {
ABC: abcHandler,
Peacock: peacockHandler,
} as const;
const notFound = (c: Context<BlankEnv, '', BlankInput>) => c.text('404 not found', 404, {});
const shutDown = () => process.exit(0);
const schedule = async () => {
console.log('=== Getting events ===');
await peacockHandler.getSchedule();
await abcHandler.getSchedule();
console.log('=== Done getting events ===');
await cleanEntries();
console.log('=== Building the schedule ===');
await scheduleEntries();
console.log('=== Done building the schedule ===');
};
const app = new Hono();
app.use('/node_modules/*', serveStatic({root: './'}));
app.get('/', async c => {
// For Links
const protocol = c.req.header('x-forwarded-proto') || 'http';
const host = c.req.header('host') || '';
const fullDomain = `${protocol}://${host}`;
// For Prefix
const currentPrefix = await miscDbHandler.getCcPrefix();
// For Providers
const allChannels = await db.channels.find<IChannel>({});
const peacock = await db.misc.findOne<IMisc>({key: 'peacock'});
const peacockChannels = allChannels.filter(c => c.from.toLowerCase() === 'peacock');
const abc = await db.misc.findOne<IMisc>({key: 'abc'});
const abcChannels = allChannels.filter(c => c.from.toLowerCase() === 'abc');
return c.html(
html`<!DOCTYPE html>${(
<Layout>
<Header />
<Main>
<Links baseUrl={fullDomain} />
<Prefix currentPrefix={currentPrefix} />
<Providers>
<Provider channels={peacockChannels} name="Peacock" enabled={peacock.value} />
<Provider channels={abcChannels} name="ABC" enabled={abc.value} />
</Providers>
</Main>
<Style />
<Script />
</Layout>
)}`,
);
});
app.put('/update-prefix', async c => {
const body = await c.req.parseBody();
const prefix = body.cc_prefix as string;
let invalid = false;
if (!prefix || prefix.length < 8) {
invalid = true;
} else {
await miscDbHandler.setCcPrefix(prefix);
}
return c.html(
<Prefix currentPrefix={prefix} invalid={invalid} />,
200,
{
...(invalid && {
'HX-Trigger': `{"HXToast":{"type":"error","body":"Invalid URI"}}`,
}),
},
);
});
app.put('/provider/toggle/:provider', async c => {
const provider = c.req.param('provider');
const body = await c.req.parseBody();
const enabled = body['provider-enabled'] === 'on';
await miscDbHandler.setCanUseNetwork(provider.toLowerCase(), enabled);
if (enabled) {
await PROVIDER_MAP[provider].getSchedule();
}
const regex = new RegExp(provider, 'i');
const providerChannels = await db.channels.find<IChannel>({from: {$regex: regex}});
return c.html(
<Provider channels={providerChannels} name={provider} enabled={enabled} />,
200,
{
...(enabled && {
'HX-Trigger': `{"HXToast":{"type":"success","body":"Successfully enabled ${provider}"}}`,
}),
},
);
});
app.put('/channel/toggle/:id', async c => {
const channelId = c.req.param('id');
const body = await c.req.parseBody();
const enabled = body['channel-enabled'] === 'on';
const {name} = await db.channels.update<IChannel>({id: channelId}, {$set: {enabled}}, {returnUpdatedDocs: true});
return c.html(
<input
hx-target="this"
hx-swap="outerHTML"
type="checkbox"
checked={enabled ? true : false}
data-enabled={enabled ? 'true': 'false'}
hx-put={`/channel/toggle/${channelId}`}
hx-trigger="change"
name="channel-enabled"
/>,
200,
{
...(enabled && {
'HX-Trigger': `{"HXToast":{"type":"success","body":"Successfully enabled ${name}"}}`,
}),
},
);
});
app.get('/channels.m3u', async c => {
const m3uFile = await generateM3u();
if (!m3uFile) {
return notFound(c);
}
return c.body(m3uFile, 200, {
'Content-Type': 'application/x-mpegurl',
});
});
app.get('/xmltv.xml', async c => {
const xmlFile = await generateXml();
if (!xmlFile) {
return notFound(c);
}
return c.body(xmlFile, 200, {
'Content-Type': 'application/xml',
});
});
// 404 Handler
app.notFound(notFound);
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);
(async () => {
console.log(`=== ccEPG v${version} starting ===`);
initDirectories();
await peacockHandler.initialize();
await peacockHandler.refreshTokens();
await abcHandler.initialize();
await abcHandler.refreshTokens();
await schedule();
console.log('=== Starting Server ===');
serve(
{
fetch: app.fetch,
port: SERVER_PORT,
},
() => console.log(`Server started on port ${SERVER_PORT}`),
);
})();
// Check for events every 4 hours and set the schedule
setInterval(async () => {
await schedule();
}, 1000 * 60 * 60 * 4);
// Check for updated refresh tokens 30 minutes
setInterval(async () => {
await peacockHandler.refreshTokens();
await abcHandler.refreshTokens();
}, 1000 * 60 * 30);