-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
161 lines (148 loc) · 5.36 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
159
160
161
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 video = require('./src/video')
const HDHR = require('./src/hdhr')
const xmltv = require('./src/xmltv')
const Plex = require('./src/plex')
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 || './.pseudotv'
process.env.PORT = process.env.PORT || 8000
if (!fs.existsSync(process.env.DATABASE))
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', 'xmltv-settings', 'hdhr-settings'])
initDB(db)
let xmltvInterval = {
interval: null,
lastRefresh: null,
updateXML: () => {
let channels = db['channels'].find()
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) })
})
}
}, (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.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) {
let ffmpegSettings = db['ffmpeg-settings'].find()
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/pseudotv.png')) {
let data = fs.readFileSync(path.resolve(path.join(__dirname, 'resources/pseudotv.png')))
fs.writeFileSync(process.env.DATABASE + '/images/pseudotv.png', data)
}
if (ffmpegSettings.length === 0) {
db['ffmpeg-settings'].save({
ffmpegPath: '/usr/bin/ffmpeg',
offset: 0,
threads: 4,
videoEncoder: 'libx264',
videoResolution: '1280x720',
videoFrameRate: 30,
videoBitrate: 10000,
audioBitrate: 192,
audioChannels: 2,
audioRate: 48000,
bufSize: 1000,
audioEncoder: 'ac3',
logFfmpeg: false,
args: `-threads 4
-ss STARTTIME
-re
-i INPUTFILE
-t DURATION
-map VIDEOSTREAM
-map AUDIOSTREAM
-c:v libx264
-c:a ac3
-ac 2
-ar 48000
-b:a 192k
-b:v 10000k
-s 1280x720
-r 30
-flags cgop+ilme
-sc_threshold 1000000000
-minrate:v 10000k
-maxrate:v 10000k
-bufsize:v 1000k
-metadata service_provider="PseudoTV"
-metadata CHANNELNAME
-f mpegts
-output_ts_offset TSOFFSET
-muxdelay 0
-muxpreload 0
OUTPUTFILE`
})
}
let xmltvSettings = db['xmltv-settings'].find()
if (xmltvSettings.length === 0) {
db['xmltv-settings'].save({
cache: 12,
refresh: 4,
file: `${process.env.DATABASE}/xmltv.xml`
})
}
let hdhrSettings = db['hdhr-settings'].find()
if (hdhrSettings.length === 0) {
db['hdhr-settings'].save({
tunerCount: 1,
autoDiscovery: true
})
}
}