wip
This commit is contained in:
parent
d39781b3c7
commit
a1149e9836
|
@ -4,6 +4,7 @@ const { Command } = require('commander')
|
||||||
const program = new Command()
|
const program = new Command()
|
||||||
const utils = require('./utils')
|
const utils = require('./utils')
|
||||||
const { name, version, description } = require('../package.json')
|
const { name, version, description } = require('../package.json')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
program
|
program
|
||||||
.name(name)
|
.name(name)
|
||||||
|
@ -20,7 +21,7 @@ async function main() {
|
||||||
const config = utils.loadConfig(options.config)
|
const config = utils.loadConfig(options.config)
|
||||||
if (options.debug) console.log(config)
|
if (options.debug) console.log(config)
|
||||||
const client = utils.createHttpClient(config)
|
const client = utils.createHttpClient(config)
|
||||||
const channels = utils.parseChannels(config.channels)
|
const channels = utils.parseChannels(options.config, config.channels)
|
||||||
const utcDate = utils.getUTCDate()
|
const utcDate = utils.getUTCDate()
|
||||||
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
|
const dates = Array.from({ length: config.days }, (_, i) => utcDate.add(i, 'd'))
|
||||||
|
|
||||||
|
@ -71,8 +72,6 @@ async function main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
const xml = utils.convertToXMLTV({ config, channels, programs })
|
const xml = utils.convertToXMLTV({ config, channels, programs })
|
||||||
const outputDir = utils.getDirectory(config.output)
|
|
||||||
utils.createDir(outputDir)
|
|
||||||
utils.writeToFile(config.output, xml)
|
utils.writeToFile(config.output, xml)
|
||||||
|
|
||||||
console.log(`File '${config.output}' successfully saved`)
|
console.log(`File '${config.output}' successfully saved`)
|
||||||
|
|
31
src/utils.js
31
src/utils.js
|
@ -10,13 +10,13 @@ dayjs.extend(utc)
|
||||||
axiosCookieJarSupport(axios)
|
axiosCookieJarSupport(axios)
|
||||||
|
|
||||||
const utils = {}
|
const utils = {}
|
||||||
|
|
||||||
utils.loadConfig = function (file) {
|
utils.loadConfig = function (file) {
|
||||||
if (!file) throw new Error('Path to [site].config.js is missing')
|
if (!file) throw new Error('Path to [site].config.js is missing')
|
||||||
console.log(`Loading '${file}'...`)
|
console.log(`Loading '${file}'...`)
|
||||||
|
|
||||||
const configPath = path.resolve(process.cwd(), file)
|
const configPath = path.resolve(file)
|
||||||
const config = require(configPath)
|
const config = require(configPath)
|
||||||
const channelsPath = path.resolve(this.getDirectory(configPath), `${config.site}.channels.xml`)
|
|
||||||
|
|
||||||
return Object.assign(
|
return Object.assign(
|
||||||
{},
|
{},
|
||||||
|
@ -26,20 +26,18 @@ utils.loadConfig = function (file) {
|
||||||
days: 1,
|
days: 1,
|
||||||
cookie: '',
|
cookie: '',
|
||||||
lang: 'en',
|
lang: 'en',
|
||||||
channels: channelsPath,
|
|
||||||
delay: 3000
|
delay: 3000
|
||||||
},
|
},
|
||||||
config
|
config
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.parseChannels = function (file) {
|
utils.parseChannels = function (configPath, channelsFilename) {
|
||||||
if (!file) throw new Error('Path to [site].channels.xml is missing')
|
if (!channelsFilename) throw new Error('Path to [site].channels.xml is missing')
|
||||||
console.log(`Loading '${file}'...`)
|
console.log(`Loading '${path.join(path.dirname(configPath), channelsFilename)}'...`)
|
||||||
|
|
||||||
const xml = fs.readFileSync(path.resolve(__dirname, file), {
|
const channelsPath = path.resolve(path.dirname(configPath), channelsFilename)
|
||||||
encoding: 'utf-8'
|
const xml = fs.readFileSync(channelsPath, { encoding: 'utf-8' })
|
||||||
})
|
|
||||||
const result = convert.xml2js(xml)
|
const result = convert.xml2js(xml)
|
||||||
const site = result.elements.find(el => el.name === 'site')
|
const site = result.elements.find(el => el.name === 'site')
|
||||||
const channels = site.elements.find(el => el.name === 'channels')
|
const channels = site.elements.find(el => el.name === 'channels')
|
||||||
|
@ -122,17 +120,12 @@ utils.convertToXMLTV = function ({ config, channels, programs }) {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.getDirectory = function (file) {
|
|
||||||
return path.dirname(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.createDir = function (dir) {
|
|
||||||
if (!fs.existsSync(path.resolve(dir))) {
|
|
||||||
fs.mkdirSync(path.resolve(dir))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
utils.writeToFile = function (filename, data) {
|
utils.writeToFile = function (filename, data) {
|
||||||
|
const dir = path.resolve(path.dirname(filename))
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
fs.writeFileSync(path.resolve(filename), data)
|
fs.writeFileSync(path.resolve(filename), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue