mirror of
https://github.com/iptv-org/epg.git
synced 2025-05-09 16:40:07 -04:00
Added update-codes
script
This commit is contained in:
parent
f1b17a38e7
commit
aad0fc4acc
3 changed files with 76 additions and 2 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -8,9 +8,11 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dayjs": "^1.10.4",
|
"dayjs": "^1.10.4",
|
||||||
"epg-grabber": "^0.2.5",
|
"epg-grabber": "^0.2.5",
|
||||||
|
"glob": "^7.1.6",
|
||||||
"html-to-text": "^7.0.0",
|
"html-to-text": "^7.0.0",
|
||||||
"jsdom": "^16.5.0",
|
"jsdom": "^16.5.0",
|
||||||
"parse-duration": "^0.4.4"
|
"parse-duration": "^0.4.4",
|
||||||
|
"xml-js": "^1.6.11"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@types/tough-cookie": {
|
"node_modules/@types/tough-cookie": {
|
||||||
|
|
|
@ -7,8 +7,10 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dayjs": "^1.10.4",
|
"dayjs": "^1.10.4",
|
||||||
"epg-grabber": "^0.2.5",
|
"epg-grabber": "^0.2.5",
|
||||||
|
"glob": "^7.1.6",
|
||||||
"html-to-text": "^7.0.0",
|
"html-to-text": "^7.0.0",
|
||||||
"jsdom": "^16.5.0",
|
"jsdom": "^16.5.0",
|
||||||
"parse-duration": "^0.4.4"
|
"parse-duration": "^0.4.4",
|
||||||
|
"xml-js": "^1.6.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
70
scripts/update-codes.js
Normal file
70
scripts/update-codes.js
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
const fs = require('fs')
|
||||||
|
const glob = require('glob')
|
||||||
|
const path = require('path')
|
||||||
|
const convert = require('xml-js')
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
let codes = {}
|
||||||
|
console.log('Starting...')
|
||||||
|
glob('sites/**/*.xml', null, function (er, files) {
|
||||||
|
files.forEach(filename => {
|
||||||
|
const channels = parseChannels(filename)
|
||||||
|
channels.forEach(channel => {
|
||||||
|
if (!codes[channel.xmltv_id + channel.name]) {
|
||||||
|
codes[channel.xmltv_id + channel.name] = {
|
||||||
|
name: channel.name,
|
||||||
|
code: channel.xmltv_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const sorted = Object.values(codes).sort((a, b) => {
|
||||||
|
if (a.name < b.name) return -1
|
||||||
|
if (a.name > b.name) return 1
|
||||||
|
return 0
|
||||||
|
})
|
||||||
|
writeToFile('codes.csv', convertToCSV(sorted))
|
||||||
|
console.log('Done')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeToFile(filename, data) {
|
||||||
|
console.log(`Saving all codes to '${filename}'...`)
|
||||||
|
const dir = path.resolve(path.dirname(filename))
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFileSync(path.resolve(filename), data)
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertToCSV(arr) {
|
||||||
|
let string = 'Channel Name,EPG Code (tvg-id)\n'
|
||||||
|
for (const item of arr) {
|
||||||
|
string += `${item.name},${item.code}\n`
|
||||||
|
}
|
||||||
|
|
||||||
|
return string
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseChannels(filename) {
|
||||||
|
if (!filename) throw new Error('Path to [site].channels.xml is missing')
|
||||||
|
console.log(`Loading '${filename}'...`)
|
||||||
|
|
||||||
|
const xml = fs.readFileSync(path.resolve(filename), { encoding: 'utf-8' })
|
||||||
|
const result = convert.xml2js(xml)
|
||||||
|
const site = result.elements.find(el => el.name === 'site')
|
||||||
|
const channels = site.elements.find(el => el.name === 'channels')
|
||||||
|
|
||||||
|
return channels.elements
|
||||||
|
.filter(el => el.name === 'channel')
|
||||||
|
.map(el => {
|
||||||
|
const channel = el.attributes
|
||||||
|
channel.name = el.elements.find(el => el.type === 'text').text
|
||||||
|
|
||||||
|
return channel
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
Loading…
Add table
Add a link
Reference in a new issue