Files
epg/scripts/commands/readme/update.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-02-26 23:21:44 +03:00
const { file, markdown, parser, logger, api, table } = require('../../core')
2022-01-14 23:57:03 +03:00
const { program } = require('commander')
const _ = require('lodash')
2022-10-26 04:41:43 +03:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs'
2022-01-15 18:27:19 +03:00
2022-01-14 23:57:03 +03:00
const options = program
2022-10-22 04:35:11 +03:00
.option('-c, --config <config>', 'Set path to config file', '.readme/readme.json')
2022-01-14 23:57:03 +03:00
.parse(process.argv)
.opts()
async function main() {
2022-05-09 12:58:01 +03:00
await api.countries.load().catch(console.error)
2022-10-26 04:41:43 +03:00
const logPath = `${LOGS_DIR}/guides/update.log`
2022-10-27 05:39:45 +03:00
let results = await parser.parseLogs(logPath)
let files = results.reduce((acc, curr) => {
2022-10-26 04:41:43 +03:00
if (acc[curr.filename]) {
acc[curr.filename].channels++
} else {
acc[curr.filename] = {
country: curr.country,
channels: 1,
filename: curr.filename
}
2022-02-27 18:04:20 +03:00
}
2022-02-03 06:22:42 +03:00
2022-10-26 04:41:43 +03:00
return acc
}, {})
2022-01-14 23:57:03 +03:00
2022-10-22 04:35:11 +03:00
let data = []
2022-10-26 04:41:43 +03:00
for (const filename in files) {
const item = files[filename]
const country = api.countries.find({ code: item.country })
2022-01-21 23:15:39 +03:00
if (!country) continue
2022-01-14 23:57:03 +03:00
2022-10-22 04:35:11 +03:00
data.push([
country.name,
`${country.flag}&nbsp;${country.name}`,
2022-10-26 04:41:43 +03:00
item.channels,
`<code>https://iptv-org.github.io/epg/guides/${filename}.xml</code>`
2022-10-22 04:35:11 +03:00
])
2022-01-14 23:57:03 +03:00
}
2022-10-27 05:39:45 +03:00
data = _.orderBy(
data,
[item => item[0], item => (item[3].includes('_en') ? Infinity : item[2])],
['asc', 'desc']
)
2022-10-22 04:35:11 +03:00
data = data.map(i => {
i.shift()
return i
})
data = Object.values(_.groupBy(data, item => item[0]))
2022-01-14 23:57:03 +03:00
2022-10-22 04:35:11 +03:00
const output = table.create(data, [
2022-02-03 06:50:13 +03:00
'Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
2022-02-03 06:22:42 +03:00
'Channels',
2022-10-22 04:35:11 +03:00
'EPG'
2022-02-03 06:22:42 +03:00
])
2022-01-14 23:57:03 +03:00
2022-02-03 06:22:42 +03:00
await file.create('./.readme/_countries.md', output)
2022-10-26 04:41:43 +03:00
await updateReadme()
2022-01-14 23:57:03 +03:00
}
2022-10-26 04:41:43 +03:00
main()
2022-01-14 23:57:03 +03:00
async function updateReadme() {
2022-02-03 06:22:42 +03:00
logger.info('updating readme.md...')
2022-01-14 23:57:03 +03:00
const config = require(file.resolve(options.config))
await file.createDir(file.dirname(config.build))
await markdown.compile(options.config)
}