mirror of
https://github.com/iptv-org/epg
synced 2025-12-16 10:26:41 -05:00
Fixes linter errors
This commit is contained in:
@@ -1,89 +1,89 @@
|
||||
const dayjs = require('dayjs')
|
||||
const axios = require('axios')
|
||||
const cheerio = require('cheerio')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const timezone = require('dayjs/plugin/timezone')
|
||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
dayjs.extend(customParseFormat)
|
||||
|
||||
module.exports = {
|
||||
site: 'zuragt.mn',
|
||||
days: 2,
|
||||
url: function ({ channel, date }) {
|
||||
return `https://m.zuragt.mn/channel/${channel.site_id}/?date=${date.format('YYYY-MM-DD')}`
|
||||
},
|
||||
request: {
|
||||
maxRedirects: 0,
|
||||
validateStatus: function (status) {
|
||||
return status >= 200 && status < 303
|
||||
}
|
||||
},
|
||||
parser: async function ({ content, date }) {
|
||||
let programs = []
|
||||
const items = parseItems(content)
|
||||
for (let item of items) {
|
||||
const prev = programs[programs.length - 1]
|
||||
const $item = cheerio.load(item)
|
||||
let start = parseStart($item, date)
|
||||
if (prev) {
|
||||
if (start.isBefore(prev.start)) {
|
||||
start = start.add(1, 'd')
|
||||
date = date.add(1, 'd')
|
||||
}
|
||||
prev.stop = start
|
||||
}
|
||||
const stop = start.add(30, 'm')
|
||||
programs.push({
|
||||
title: parseTitle($item),
|
||||
start,
|
||||
stop
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
},
|
||||
async channels() {
|
||||
let html = await axios
|
||||
.get('https://www.zuragt.mn/')
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
let $ = cheerio.load(html)
|
||||
|
||||
const items = $('.tv-box > ul > li').toArray()
|
||||
return items
|
||||
.map(item => {
|
||||
const name = $(item).text().trim()
|
||||
const link = $(item).find('a').attr('href')
|
||||
|
||||
if (!link) return null
|
||||
|
||||
const [, site_id] = link.match(/\/channel\/(.*)\//) || [null, null]
|
||||
|
||||
return {
|
||||
lang: 'mn',
|
||||
site_id,
|
||||
name
|
||||
}
|
||||
})
|
||||
.filter(Boolean)
|
||||
}
|
||||
}
|
||||
|
||||
function parseTitle($item) {
|
||||
return $item('.program').text().trim()
|
||||
}
|
||||
|
||||
function parseStart($item, date) {
|
||||
const time = $item('.time')
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${time}`, 'YYYY-MM-DD HH:mm', 'Asia/Ulaanbaatar')
|
||||
}
|
||||
|
||||
function parseItems(content) {
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $('body > div > div > div > ul > li').toArray()
|
||||
}
|
||||
const dayjs = require('dayjs')
|
||||
const axios = require('axios')
|
||||
const cheerio = require('cheerio')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const timezone = require('dayjs/plugin/timezone')
|
||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
dayjs.extend(customParseFormat)
|
||||
|
||||
module.exports = {
|
||||
site: 'zuragt.mn',
|
||||
days: 2,
|
||||
url: function ({ channel, date }) {
|
||||
return `https://m.zuragt.mn/channel/${channel.site_id}/?date=${date.format('YYYY-MM-DD')}`
|
||||
},
|
||||
request: {
|
||||
maxRedirects: 0,
|
||||
validateStatus: function (status) {
|
||||
return status >= 200 && status < 303
|
||||
}
|
||||
},
|
||||
parser: async function ({ content, date }) {
|
||||
let programs = []
|
||||
const items = parseItems(content)
|
||||
for (let item of items) {
|
||||
const prev = programs[programs.length - 1]
|
||||
const $item = cheerio.load(item)
|
||||
let start = parseStart($item, date)
|
||||
if (prev) {
|
||||
if (start.isBefore(prev.start)) {
|
||||
start = start.add(1, 'd')
|
||||
date = date.add(1, 'd')
|
||||
}
|
||||
prev.stop = start
|
||||
}
|
||||
const stop = start.add(30, 'm')
|
||||
programs.push({
|
||||
title: parseTitle($item),
|
||||
start,
|
||||
stop
|
||||
})
|
||||
}
|
||||
|
||||
return programs
|
||||
},
|
||||
async channels() {
|
||||
let html = await axios
|
||||
.get('https://www.zuragt.mn/')
|
||||
.then(r => r.data)
|
||||
.catch(console.log)
|
||||
let $ = cheerio.load(html)
|
||||
|
||||
const items = $('.tv-box > ul > li').toArray()
|
||||
return items
|
||||
.map(item => {
|
||||
const name = $(item).text().trim()
|
||||
const link = $(item).find('a').attr('href')
|
||||
|
||||
if (!link) return null
|
||||
|
||||
const [, site_id] = link.match(/\/channel\/(.*)\//) || [null, null]
|
||||
|
||||
return {
|
||||
lang: 'mn',
|
||||
site_id,
|
||||
name
|
||||
}
|
||||
})
|
||||
.filter(Boolean)
|
||||
}
|
||||
}
|
||||
|
||||
function parseTitle($item) {
|
||||
return $item('.program').text().trim()
|
||||
}
|
||||
|
||||
function parseStart($item, date) {
|
||||
const time = $item('.time')
|
||||
|
||||
return dayjs.tz(`${date.format('YYYY-MM-DD')} ${time}`, 'YYYY-MM-DD HH:mm', 'Asia/Ulaanbaatar')
|
||||
}
|
||||
|
||||
function parseItems(content) {
|
||||
const $ = cheerio.load(content)
|
||||
|
||||
return $('body > div > div > div > ul > li').toArray()
|
||||
}
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
// npm run channels:parse -- --config=./sites/zuragt.mn/zuragt.mn.config.js --output=./sites/zuragt.mn/zuragt.mn.channels.xml
|
||||
// npm run grab -- --site=zuragt.mn
|
||||
|
||||
const { parser, url, request } = require('./zuragt.mn.config.js')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||
dayjs.extend(customParseFormat)
|
||||
dayjs.extend(utc)
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const date = dayjs.utc('2023-01-15', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: 'mnb',
|
||||
xmltv_id: 'MNB.mn'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel, date })).toBe('https://m.zuragt.mn/channel/mnb/?date=2023-01-15')
|
||||
})
|
||||
|
||||
it('can generate valid request object', () => {
|
||||
expect(request.maxRedirects).toBe(0)
|
||||
expect(request.validateStatus(302)).toBe(true)
|
||||
})
|
||||
|
||||
it('can parse response', async () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'))
|
||||
let results = await parser({ content, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2023-01-14T23:00:00.000Z',
|
||||
stop: '2023-01-15T00:00:00.000Z',
|
||||
title: '“Цагийн хүрд” мэдээллийн хөтөлбөр'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', async () => {
|
||||
const result = await parser({ content: '' })
|
||||
expect(result).toMatchObject([])
|
||||
})
|
||||
// npm run channels:parse -- --config=./sites/zuragt.mn/zuragt.mn.config.js --output=./sites/zuragt.mn/zuragt.mn.channels.xml
|
||||
// npm run grab -- --site=zuragt.mn
|
||||
|
||||
const { parser, url, request } = require('./zuragt.mn.config.js')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const dayjs = require('dayjs')
|
||||
const utc = require('dayjs/plugin/utc')
|
||||
const customParseFormat = require('dayjs/plugin/customParseFormat')
|
||||
dayjs.extend(customParseFormat)
|
||||
dayjs.extend(utc)
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const date = dayjs.utc('2023-01-15', 'YYYY-MM-DD').startOf('d')
|
||||
const channel = {
|
||||
site_id: 'mnb',
|
||||
xmltv_id: 'MNB.mn'
|
||||
}
|
||||
|
||||
it('can generate valid url', () => {
|
||||
expect(url({ channel, date })).toBe('https://m.zuragt.mn/channel/mnb/?date=2023-01-15')
|
||||
})
|
||||
|
||||
it('can generate valid request object', () => {
|
||||
expect(request.maxRedirects).toBe(0)
|
||||
expect(request.validateStatus(302)).toBe(true)
|
||||
})
|
||||
|
||||
it('can parse response', async () => {
|
||||
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.html'))
|
||||
let results = await parser({ content, date })
|
||||
results = results.map(p => {
|
||||
p.start = p.start.toJSON()
|
||||
p.stop = p.stop.toJSON()
|
||||
return p
|
||||
})
|
||||
|
||||
expect(results[0]).toMatchObject({
|
||||
start: '2023-01-14T23:00:00.000Z',
|
||||
stop: '2023-01-15T00:00:00.000Z',
|
||||
title: '“Цагийн хүрд” мэдээллийн хөтөлбөр'
|
||||
})
|
||||
})
|
||||
|
||||
it('can handle empty guide', async () => {
|
||||
const result = await parser({ content: '' })
|
||||
expect(result).toMatchObject([])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user