-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (128 loc) · 4.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
import fetch from 'node-fetch';
import { DateTime } from 'luxon';
import fs from 'fs';
import checkbox from '@inquirer/checkbox';
const baseUrl = 'http://YOUR-ENIGMA2-BOX-IP';
const dummyUrl = 'DUMMY-URL-FOR-XMLTV-DOCUMENT';
const xmlpath = 'DIR-WHERE-TO-STORE-THE-XML-FILES';
const defaultLang = 'en';
const dataFileName = 'bouquetes.json';
let enabledBouquetes;
const readDataFile = async () => {
try {
const data = await fs.promises.readFile(dataFileName, 'utf-8');
enabledBouquetes = JSON.parse(data);
} catch (err) {
enabledBouquetes = null;
}
};
const getEpg = async (bRef) => {
const response = await fetch(`${baseUrl}/api/epgmulti?bRef=${bRef}`);
return response.json();
};
const getBouquetes = async () => {
const response = await fetch(`${baseUrl}/api/bouquets`);
return response.json();
};
const getServices = async (sRef) => {
const response = await fetch(`${baseUrl}/api/getservices?sRef=${sRef}`);
return response.json();
};
const channelsRefs = {};
const getXMLTVHeader = () => `
<tv generator-info-name="enigma2epg grabber" generator-info-url="${dummyUrl}">
`;
const buildPiconPath = (ref) => {
const segments = ref.slice(0, -1).split(':');
segments[2] = '1';
return `${segments.join('_')}.png`;
};
const getFilename = (title) => `${title.replace(/[ /:]/g, '')}.xml`;
const xmlSafe = (string) => {
return string.replace(/[&"'<>\t\n\r]/g, (match) => {
const replacements = {
'&': '&',
'"': '"',
"'": ''',
'<': '<',
'>': '>',
'\t': '	',
'\n': '
',
'\r': '
',
};
return replacements[match];
});
};
const getXMLTVChannels = (channels) => {
return channels.services
.map((c) => `
<channel id="${xmlSafe(c.servicename)}">
<display-name lang="${defaultLang}">${xmlSafe(c.servicename)}</display-name>
<icon src="${baseUrl}/picon/${buildPiconPath(c.servicereference)}" />
<url>${dummyUrl}</url>
</channel>`
)
.join('');
};
const parseEPG = (epg) => {
return epg.events
.map((e) => `
<programme start="${DateTime.fromSeconds(e.begin_timestamp).toFormat('yyyyMMddHHmm00')} +0000" stop="${DateTime.fromSeconds(e.begin_timestamp + e.duration_sec).toFormat('yyyyMMddHHmm00')} +0000" channel="${channelsRefs[e.sref]}">
<title lang="${defaultLang}">${xmlSafe(e.title)}</title>
<desc lang="${defaultLang}">${xmlSafe(e.longdesc)}</desc>
<category lang="${defaultLang}">${xmlSafe(e.genre)}</category>
</programme>`
)
.join('');
};
const buildXMLTV = (epg, channels) => {
const xmltv = getXMLTVHeader() + `
${getXMLTVChannels(channels)}
${parseEPG(epg)}
</tv>`;
return xmltv;
};
(async () => {
await readDataFile();
const bouquetes = await getBouquetes();
if (!enabledBouquetes) {
let cnt = 0;
enabledBouquetes = await checkbox({
message: 'Select the bouquetes:',
choices: bouquetes.bouquets.map((bItem) => {
return {
name: `${bItem[1]}`,
value: cnt++,
};
}),
pageSize: 20,
loop: false,
});
fs.promises.writeFile(dataFileName, JSON.stringify(enabledBouquetes))
.then(() => {
console.log(`Successfully saved the processing bouquetes.
Next time if you want to change the list to be processed delete
the file "${dataFileName}" from this directory and manually re-run
it with "node index.js".
`);
})
.catch((err) => {
console.error(err);
});
}
for (const bIndex of enabledBouquetes) {
const bRef = bouquetes.bouquets[Number(bIndex)][0];
const bName = bouquetes.bouquets[Number(bIndex)][1];
const services = await getServices(bRef);
services.services.forEach((s) => (channelsRefs[s.servicereference] = s.servicename));
const epg = await getEpg(bRef);
const xmltv = buildXMLTV(epg, services).replaceAll('&', '&');
const filename = `${xmlpath}/${getFilename(bName)}`;
try {
await fs.promises.writeFile(filename, xmltv);
console.log(`Generated file ${filename}`);
} catch (err) {
console.error(err);
}
}
})();