forked from vexorian/dizquetv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (141 loc) · 6.41 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
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
const db = require('diskdb')
const fs = require('fs')
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const api = require('./src/api')
const dbMigration = require('./src/database-migration');
const video = require('./src/video')
const HDHR = require('./src/hdhr')
const xmltv = require('./src/xmltv')
const Plex = require('./src/plex');
const channelCache = require('./src/channel-cache');
const constants = require('./src/constants')
console.log(
` \\
dizqueTV ${constants.VERSION_NAME}
.------------.
|:::///### o |
|:::///### |
':::///### o |
'------------'
`);
for (let i = 0, l = process.argv.length; i < l; i++) {
if ((process.argv[i] === "-p" || process.argv[i] === "--port") && i + 1 !== l)
process.env.PORT = process.argv[i + 1]
if ((process.argv[i] === "-d" || process.argv[i] === "--database") && i + 1 !== l)
process.env.DATABASE = process.argv[i + 1]
}
process.env.DATABASE = process.env.DATABASE || './.dizquetv'
process.env.PORT = process.env.PORT || 8000
if (!fs.existsSync(process.env.DATABASE)) {
if (fs.existsSync("./.pseudotv")) {
throw Error(process.env.DATABASE + " folder not found but ./.pseudotv has been found. Please rename this folder or create an empty " + process.env.DATABASE + " folder so that the program is not confused about.");
}
fs.mkdirSync(process.env.DATABASE)
}
if(!fs.existsSync(path.join(process.env.DATABASE, 'images')))
fs.mkdirSync(path.join(process.env.DATABASE, 'images'))
db.connect(process.env.DATABASE, ['channels', 'plex-servers', 'ffmpeg-settings', 'plex-settings', 'xmltv-settings', 'hdhr-settings', 'db-version', 'client-id'])
initDB(db)
let xmltvInterval = {
interval: null,
lastRefresh: null,
updateXML: () => {
let channels = db['channels'].find()
channels.forEach( (channel) => {
// if we are going to go through the trouble of loading the whole channel db, we might
// as well take that opportunity to reduce stream loading times...
channelCache.saveChannelConfig( channel.number, channel );
});
channels.sort((a, b) => { return a.number < b.number ? -1 : 1 })
let xmltvSettings = db['xmltv-settings'].find()[0]
xmltv.WriteXMLTV(channels, xmltvSettings).then(async () => { // Update XML
xmltvInterval.lastRefresh = new Date()
console.log('XMLTV Updated at ', xmltvInterval.lastRefresh.toLocaleString())
let plexServers = db['plex-servers'].find()
for (let i = 0, l = plexServers.length; i < l; i++) { // Foreach plex server
var plex = new Plex(plexServers[i])
await plex.GetDVRS().then(async (dvrs) => { // Refresh guide and channel mappings
if (plexServers[i].arGuide)
plex.RefreshGuide(dvrs).then(() => { }, (err) => { console.error(err, i) })
if (plexServers[i].arChannels && channels.length !== 0)
plex.RefreshChannels(channels, dvrs).then(() => { }, (err) => { console.error(err, i) })
}).catch( (err) => {
console.log("Couldn't tell Plex to refresh channels for some reason.");
});
}
}, (err) => {
console.error("Failed to write the xmltv.xml file. Something went wrong. Check your output directory via the web UI and verify file permissions?", err)
})
},
startInterval: () => {
let xmltvSettings = db['xmltv-settings'].find()[0]
if (xmltvSettings.refresh !== 0) {
xmltvInterval.interval = setInterval(() => {
xmltvInterval.updateXML()
}, xmltvSettings.refresh * 60 * 60 * 1000)
}
},
restartInterval: () => {
if (xmltvInterval.interval !== null)
clearInterval(xmltvInterval.interval)
xmltvInterval.startInterval()
}
}
xmltvInterval.updateXML()
xmltvInterval.startInterval()
let hdhr = HDHR(db)
let app = express()
app.use(bodyParser.json({limit: '50mb'}))
app.get('/version.js', (req, res) => {
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
res.write( `
function setUIVersionNow() {
setTimeout( setUIVersionNow, 1000);
var element = document.getElementById("uiversion");
if (element != null) {
element.innerHTML = "${constants.VERSION_NAME}";
}
}
setTimeout( setUIVersionNow, 1000);
` );
res.end();
});
app.use('/images', express.static(path.join(process.env.DATABASE, 'images')))
app.use(express.static(path.join(__dirname, 'web/public')))
app.use('/images', express.static(path.join(process.env.DATABASE, 'images')))
app.use(api.router(db, xmltvInterval))
app.use(video.router(db))
app.use(hdhr.router)
app.listen(process.env.PORT, () => {
console.log(`HTTP server running on port: http://*:${process.env.PORT}`)
let hdhrSettings = db['hdhr-settings'].find()[0]
if (hdhrSettings.autoDiscovery === true)
hdhr.ssdp.start()
})
function initDB(db) {
dbMigration.initDB(db);
if (!fs.existsSync(process.env.DATABASE + '/font.ttf')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/font.ttf')))
fs.writeFileSync(process.env.DATABASE + '/font.ttf', data)
}
if (!fs.existsSync(process.env.DATABASE + '/images/dizquetv.png')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/dizquetv.png')))
fs.writeFileSync(process.env.DATABASE + '/images/dizquetv.png', data)
}
if (!fs.existsSync(process.env.DATABASE + '/images/generic-error-screen.png')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/generic-error-screen.png')))
fs.writeFileSync(process.env.DATABASE + '/images/generic-error-screen.png', data)
}
if (!fs.existsSync(process.env.DATABASE + '/images/generic-offline-screen.png')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/generic-offline-screen.png')))
fs.writeFileSync(process.env.DATABASE + '/images/generic-offline-screen.png', data)
}
if (!fs.existsSync(process.env.DATABASE + '/images/loading-screen.png')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/loading-screen.png')))
fs.writeFileSync(process.env.DATABASE + '/images/loading-screen.png', data)
}
}