mirror of
https://github.com/iptv-org/epg
synced 2026-04-14 22:59:24 -04:00
Merge branch 'master' of https://github.com/iptv-org/epg into be-2957-s1
This commit is contained in:
@@ -7,7 +7,6 @@ import { Storage } from '@freearhey/storage-js'
|
||||
import { Channel } from '../../models'
|
||||
import * as sdk from '@iptv-org/sdk'
|
||||
import { Command } from 'commander'
|
||||
import readline from 'readline'
|
||||
|
||||
interface ChoiceValue {
|
||||
type: string
|
||||
@@ -20,17 +19,6 @@ interface Choice {
|
||||
default?: boolean
|
||||
}
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
readline
|
||||
.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
})
|
||||
.on('SIGINT', function () {
|
||||
process.emit('SIGINT')
|
||||
})
|
||||
}
|
||||
|
||||
const program = new Command()
|
||||
|
||||
program.argument('<filepath>', 'Path to *.channels.xml file to edit').parse(process.argv)
|
||||
@@ -43,12 +31,7 @@ let channelsFromXML = new Collection<Channel>()
|
||||
main(filepath)
|
||||
process.on('SIGINT', () => {
|
||||
save(filepath, channelsFromXML)
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
save(filepath, channelsFromXML)
|
||||
process.exit(0)
|
||||
if (process.platform === 'win32') process.kill(0)
|
||||
})
|
||||
|
||||
export default async function main(filepath: string) {
|
||||
@@ -90,8 +73,7 @@ export default async function main(filepath: string) {
|
||||
}
|
||||
|
||||
async function selectChannel(channel: epgGrabber.Channel): Promise<string> {
|
||||
const query = escapeRegex(channel.name)
|
||||
const similarChannels = searchChannels(query)
|
||||
const similarChannels = searchChannels(channel.name)
|
||||
const choices = getChoicesForChannel(similarChannels).all()
|
||||
|
||||
const selected: ChoiceValue = await select({
|
||||
@@ -199,7 +181,3 @@ function save(filepath: string, channelsFromXML: Collection<Channel>) {
|
||||
console.log()
|
||||
logger.info(`File '${filepath}' successfully saved`)
|
||||
}
|
||||
|
||||
function escapeRegex(string: string) {
|
||||
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
|
||||
}
|
||||
|
||||
144
scripts/commands/guides/update.ts
Normal file
144
scripts/commands/guides/update.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { HTMLTableRow, HTMLTableDataItem, HTMLTableColumn } from '../../types/htmlTable'
|
||||
import epgGrabber, { EPGGrabber } from 'epg-grabber'
|
||||
import AxiosMockAdapter from 'axios-mock-adapter'
|
||||
import { Storage } from '@freearhey/storage-js'
|
||||
import { Channel, Worker } from '../../models'
|
||||
import { Collection } from '@freearhey/core'
|
||||
import { ROOT_DIR } from '../../constants'
|
||||
import { Logger } from '@freearhey/core'
|
||||
import { HTMLTable } from '../../core'
|
||||
import epgParser from 'epg-parser'
|
||||
import axios from 'axios'
|
||||
|
||||
async function main() {
|
||||
const logger = new Logger({ level: process.env.NODE_ENV === 'test' ? -999 : 3 })
|
||||
const rootStorage = new Storage(ROOT_DIR)
|
||||
const workers = new Map<string, Worker>()
|
||||
|
||||
logger.info('loading workers.txt...')
|
||||
const workersTxt = await rootStorage.load('workers.txt')
|
||||
|
||||
workersTxt.split('\r\n').forEach((host: string) => {
|
||||
if (!host) return
|
||||
|
||||
const worker = new Worker({ host })
|
||||
|
||||
workers.set(host, worker)
|
||||
})
|
||||
|
||||
for (const worker of workers.values()) {
|
||||
logger.info(`processing "${worker.host}"...`)
|
||||
|
||||
const client = axios.create({
|
||||
baseURL: worker.getBaseUrl(),
|
||||
timeout: 60000
|
||||
})
|
||||
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
const mock = new AxiosMockAdapter(client)
|
||||
if (worker.host === 'example.com') {
|
||||
mock.onGet('worker.json').reply(404)
|
||||
} else {
|
||||
const testStorage = new Storage('tests/__data__/input/guides_update')
|
||||
mock.onGet('worker.json').reply(200, await testStorage.load('worker.json'))
|
||||
mock.onGet('channels.xml').reply(200, await testStorage.load('channels.xml'))
|
||||
mock.onGet('guide.xml').reply(200, await testStorage.load('guide.xml'))
|
||||
}
|
||||
}
|
||||
|
||||
const workerJson = await client
|
||||
.get('worker.json')
|
||||
.then(res => res.data)
|
||||
.catch(err => {
|
||||
worker.status = err.status
|
||||
logger.error(err.message)
|
||||
})
|
||||
|
||||
if (!workerJson) {
|
||||
worker.status = 'MISSING_WORKER_CONFIG'
|
||||
logger.error('Unable to load "workers.json"')
|
||||
continue
|
||||
}
|
||||
|
||||
worker.channelsPath = workerJson.channels
|
||||
worker.guidePath = workerJson.guide
|
||||
|
||||
if (!worker.channelsPath) {
|
||||
worker.status = 'MISSING_CHANNELS_PATH'
|
||||
logger.error('The "channels" property is missing from the workers config')
|
||||
continue
|
||||
}
|
||||
|
||||
if (!worker.guidePath) {
|
||||
worker.status = 'MISSING_GUIDE_PATH'
|
||||
logger.error('The "guide" property is missing from the workers config')
|
||||
continue
|
||||
}
|
||||
|
||||
const channelsXml = await client
|
||||
.get(worker.channelsPath)
|
||||
.then(res => res.data)
|
||||
.catch(err => {
|
||||
worker.status = err.status
|
||||
logger.error(err.message)
|
||||
})
|
||||
|
||||
if (!channelsXml) continue
|
||||
|
||||
const parsedChannels = EPGGrabber.parseChannelsXML(channelsXml)
|
||||
worker.channels = new Collection(parsedChannels).map(
|
||||
(channel: epgGrabber.Channel) => new Channel(channel.toObject())
|
||||
)
|
||||
|
||||
const guideXml = await client
|
||||
.get(worker.guidePath)
|
||||
.then(res => res.data)
|
||||
.catch(err => {
|
||||
worker.status = err.status
|
||||
logger.error(err.message)
|
||||
})
|
||||
|
||||
if (!guideXml) continue
|
||||
|
||||
const parsedGuide = epgParser.parse(guideXml)
|
||||
worker.lastUpdated = parsedGuide.date
|
||||
|
||||
worker.status = 'OK'
|
||||
}
|
||||
|
||||
logger.info('creating guides table...')
|
||||
const rows = new Collection<HTMLTableRow>()
|
||||
workers.forEach((worker: Worker) => {
|
||||
rows.add(
|
||||
new Collection<HTMLTableDataItem>([
|
||||
{ value: worker.host },
|
||||
{ value: worker.getStatusEmoji(), align: 'center' },
|
||||
{ value: worker.getChannelsCount().toString(), align: 'right' },
|
||||
{ value: worker.getLastUpdated(), align: 'left' },
|
||||
{
|
||||
value:
|
||||
worker.status === 'OK'
|
||||
? `<a href="${worker.getChannelsUrl()}">${worker.channelsPath}</a><br><a href="${worker.getGuideUrl()}">${worker.guidePath}</a>`
|
||||
: ''
|
||||
}
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
logger.info('updating guides.md...')
|
||||
const table = new HTMLTable(
|
||||
rows,
|
||||
new Collection<HTMLTableColumn>([
|
||||
{ name: 'Host', align: 'left' },
|
||||
{ name: 'Status', align: 'left' },
|
||||
{ name: 'Channels', align: 'left' },
|
||||
{ name: 'Last Updated', align: 'left' },
|
||||
{ name: 'Links', align: 'left' }
|
||||
])
|
||||
)
|
||||
const guidesTemplate = await new Storage().load('scripts/templates/_guides.md')
|
||||
const guidesContent = guidesTemplate.replace('_TABLE_', table.toString())
|
||||
await rootStorage.save('GUIDES.md', guidesContent)
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user