This commit is contained in:
freearhey 2021-03-13 16:17:50 +03:00
parent d39781b3c7
commit a1149e9836
2 changed files with 14 additions and 22 deletions

View File

@ -4,6 +4,7 @@ const { Command } = require('commander')
const program = new Command()
const utils = require('./utils')
const { name, version, description } = require('../package.json')
const path = require('path')
program
.name(name)
@ -20,7 +21,7 @@ async function main() {
const config = utils.loadConfig(options.config)
if (options.debug) console.log(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 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 outputDir = utils.getDirectory(config.output)
utils.createDir(outputDir)
utils.writeToFile(config.output, xml)
console.log(`File '${config.output}' successfully saved`)

View File

@ -10,13 +10,13 @@ dayjs.extend(utc)
axiosCookieJarSupport(axios)
const utils = {}
utils.loadConfig = function (file) {
if (!file) throw new Error('Path to [site].config.js is missing')
console.log(`Loading '${file}'...`)
const configPath = path.resolve(process.cwd(), file)
const configPath = path.resolve(file)
const config = require(configPath)
const channelsPath = path.resolve(this.getDirectory(configPath), `${config.site}.channels.xml`)
return Object.assign(
{},
@ -26,20 +26,18 @@ utils.loadConfig = function (file) {
days: 1,
cookie: '',
lang: 'en',
channels: channelsPath,
delay: 3000
},
config
)
}
utils.parseChannels = function (file) {
if (!file) throw new Error('Path to [site].channels.xml is missing')
console.log(`Loading '${file}'...`)
utils.parseChannels = function (configPath, channelsFilename) {
if (!channelsFilename) throw new Error('Path to [site].channels.xml is missing')
console.log(`Loading '${path.join(path.dirname(configPath), channelsFilename)}'...`)
const xml = fs.readFileSync(path.resolve(__dirname, file), {
encoding: 'utf-8'
})
const channelsPath = path.resolve(path.dirname(configPath), channelsFilename)
const xml = fs.readFileSync(channelsPath, { 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')
@ -122,17 +120,12 @@ utils.convertToXMLTV = function ({ config, channels, programs }) {
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) {
const dir = path.resolve(path.dirname(filename))
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
fs.writeFileSync(path.resolve(filename), data)
}