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

127 lines
2.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`
2023-01-08 12:21:02 +03:00
let log = await parser.parseLogs(logPath)
await createCountriesTable(log)
await createSourcesTable(log)
await updateReadme()
}
main()
async function createSourcesTable(log) {
let files = log
.filter(c => c.groupedBy === 'site+lang')
.reduce((acc, curr) => {
if (!acc[curr.filename]) {
acc[curr.filename] = {
site: curr.site,
lang: curr.lang,
channels: 0,
filename: curr.filename
}
}
2022-10-26 04:41:43 +03:00
acc[curr.filename].channels++
2023-01-08 12:21:02 +03:00
return acc
}, {})
let data = []
for (const filename in files) {
const item = files[filename]
data.push([
item.site,
item.lang,
item.channels,
`<code>https://iptv-org.github.io/epg/guides/${filename}.xml</code>`
])
}
data = _.orderBy(
data,
[item => item[0], item => (item[1] === 'en' ? Infinity : item[2])],
['asc', 'desc']
)
data = data.map(i => {
i.splice(1, 1)
return i
})
data = Object.values(_.groupBy(data, item => item[0]))
const output = table.create(data, ['Site', 'Channels', 'EPG'])
await file.create('./.readme/_sources.md', output)
}
async function createCountriesTable(log) {
let files = log
.filter(c => c.groupedBy === 'country')
.reduce((acc, curr) => {
if (!acc[curr.filename]) {
acc[curr.filename] = {
country: curr.country,
lang: curr.lang,
channels: 0,
filename: curr.filename
}
2022-10-26 04:41:43 +03:00
}
2022-02-03 06:22:42 +03:00
2023-01-08 12:21:02 +03:00
acc[curr.filename].channels++
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,
2023-01-08 12:21:02 +03:00
item.lang,
2022-10-22 04:35:11 +03:00
`${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,
2023-01-08 12:21:02 +03:00
[item => item[0], item => (item[1] === 'en' ? Infinity : item[3])],
2022-10-27 05:39:45 +03:00
['asc', 'desc']
)
2022-10-22 04:35:11 +03:00
data = data.map(i => {
2023-01-08 12:21:02 +03:00
i.splice(0, 2)
2022-10-22 04:35:11 +03:00
return i
})
data = Object.values(_.groupBy(data, item => item[0]))
2022-01-14 23:57:03 +03:00
const output = table.create(data, ['Country', 'Channels', 'EPG'])
2022-01-14 23:57:03 +03:00
2022-02-03 06:22:42 +03:00
await file.create('./.readme/_countries.md', output)
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)
}