Update mytelly.co.uk guide.

Test:

```sh
npm test -- mytelly.co.uk

> test
> run-script-os mytelly.co.uk

> test:win32
> SET "TZ=Pacific/Nauru" && npx jest --runInBand mytelly.co.uk

 PASS  sites/mytelly.co.uk/mytelly.co.uk.test.js
  √ can generate valid url (4 ms)
  √ can parse response (135 ms)
  √ can handle empty guide (1 ms)

Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        4.146 s, estimated 5 s
Ran all test suites matching /mytelly.co.uk/i.
```

Grab:

```sh
npm run grab -- --site=mytelly.co.uk

> grab
> npx tsx scripts/commands/epg/grab.ts --site=mytelly.co.uk

starting...
config:
  output: guide.xml
  maxConnections: 1
  gzip: false
  site: mytelly.co.uk
loading channels...
  found 488 channel(s)
run #1:
  [1/976] mytelly.co.uk (en) - 63/mtv-xmas - Dec 7, 2024 (14 programs)
  [2/976] mytelly.co.uk (en) - 63/mtv-xmas - Dec 8, 2024 (11 programs)
  ...
  [975/976] mytelly.co.uk (en) - TNTSports6.uk - Dec 8, 2024 (3 programs)
  [976/976] mytelly.co.uk (en) - SkySportsMix.uk - Dec 8, 2024 (17 programs)
  saving to "guide.xml"...
  done in 00h 04m 16s
```

Signed-off-by: Toha <tohenk@yahoo.com>
This commit is contained in:
Toha
2024-12-07 21:06:43 +07:00
parent ce4f3e6935
commit 4aa23fa862
4 changed files with 1042 additions and 1242 deletions

View File

@@ -8,126 +8,161 @@ dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
const detailedGuide = false
const tz = 'Europe/London'
const nworker = 10
module.exports = {
site: 'mytelly.co.uk',
days: 2,
url: function ({ date, channel }) {
url({ date, channel }) {
return `https://www.mytelly.co.uk/tv-guide/listings/channel/${
channel.site_id
}.html?dt=${date.format('YYYY-MM-DD')}`
},
parser: function ({ content, date, channel }) {
async parser({ content, date, channel }) {
const programs = []
const items = parseItems(content)
items.forEach(item => {
const prev = programs[programs.length - 1]
const $item = cheerio.load(item)
let start = parseStart($item, date, channel)
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
})
})
if (content) {
const queues = []
const $ = cheerio.load(content)
$('table.table > tbody > tr').toArray()
.forEach(el => {
const td = $(el).find('td:eq(1)')
const title = td.find('h5 a')
if (detailedGuide) {
queues.push(title.attr('href'))
} else {
const subtitle = td.find('h6')
const time = $(el).find('td:eq(0)')
const dateString = `${date.format('YYYY-MM-DD')} ${time.text().trim()}`
let start = dayjs.tz(dateString, 'YYYY-MM-DD H:mm a', tz)
const prev = programs[programs.length - 1]
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: parseText(title),
subTitle: parseText(subtitle),
start,
stop
})
}
})
}
return programs
},
async channels() {
const channels = {}
const axios = require('axios')
const _ = require('lodash')
const queues = [{ t: 'p', u: 'https://www.mytelly.co.uk/getform' }]
const providers = [
'-3000053',
'-4000118',
'-11000199',
'-1000007',
'-2000007',
'-12000220',
'-5000136',
'-10000178'
]
const regions = [
'Cambridgeshire',
'Channel Islands',
'Cumbria',
'East',
'East Midlands',
'Yorkshire &amp; Lincolnshire',
'London',
'North East',
'North West',
'Northern Ireland',
'Oxfordshire',
'Scotland (Borders)',
'Scotland (Central)',
'Scotland (North)',
'South',
'South East',
'South West',
'Wales',
'West',
'West Midlands',
'Yorkshire'
]
const channels = []
for (let provider of providers) {
for (let region of regions) {
const data = await axios
.post(`https://www.mytelly.co.uk/tv-guide/schedule`, null, {
params: {
provider,
region,
TVperiod: 'Night',
date: dayjs().format('YYYY-MM-DD'),
st: 0,
u_time: 1955,
is_mobile: 1
let n = Math.min(nworker, queues.length)
const workers = []
const cb = (queue, res) => {
// process form -> provider
if (queue.t === 'p') {
const $ = cheerio.load(res)
$('#guide_provider option').toArray()
.forEach(el => {
const opt = $(el)
const provider = opt.attr('value')
queues.push({ t: 'r', u: 'https://www.mytelly.co.uk/getregions', params: { provider } })
})
}
// process provider -> region
if (queue.t === 'r') {
const now = dayjs()
for (const r of Object.values(res)) {
const params = {
provider: queue.params.provider,
region: r.title,
TVperiod: 'Night',
date: now.format('YYYY-MM-DD'),
st: 0,
u_time: now.format('HHmm'),
is_mobile: 1
}
queues.push({ t: 's', u: 'https://www.mytelly.co.uk/tv-guide/schedule', params })
}
}
// process schedule -> channels
if (queue.t === 's') {
const $ = cheerio.load(res)
$('.channelname')
.each((i, el) => {
const name = $(el).find('center > a:eq(1)').text()
const url = $(el).find('center > a:eq(1)').attr('href')
const [, number, slug] = url.match(/\/(\d+)\/(.*)\.html$/)
const site_id = `${number}/${slug}`
if (channels[site_id] === undefined) {
channels[site_id] = {
lang: 'en',
site_id,
name
}
}
})
.then(r => r.data)
.catch(console.log)
const $ = cheerio.load(data)
$('.channelname').each((i, el) => {
const name = $(el).find('center > a:eq(1)').text()
const url = $(el).find('center > a:eq(1)').attr('href')
const [, number, slug] = url.match(/\/(\d+)\/(.*)\.html$/)
channels.push({
lang: 'en',
name,
site_id: `${number}/${slug}`
})
})
}
// increase worker
if (queues.length > workers.length && workers.length < nworker) {
let nw = Math.min(nworker, queues.length)
if (n < nw) {
n = nw
createWorker()
}
}
}
const createWorker = () => {
while (workers.length < n) {
startWorker()
}
}
const startWorker = () => {
const worker = () => {
if (queues.length) {
const q = queues.shift()
axios
.post(q.u, q.params || {})
.then(response => {
if (response.data) {
cb(q, response.data)
}
worker()
})
.catch(console.error)
} else {
workers.splice(workers.indexOf(worker), 1)
}
}
workers.push(worker)
worker()
}
createWorker()
await new Promise(resolve => {
const interval = setInterval(() => {
if (workers.length === 0) {
clearInterval(interval)
resolve()
}
}, 500)
})
return _.uniqBy(channels, 'site_id')
return Object.values(channels)
}
}
function parseStart($item, date, channel) {
const timeString = $item('td:eq(0)').text().trim()
const dateString = `${date.format('YYYY-MM-DD')} ${timeString}`
return dayjs.tz(dateString, 'YYYY-MM-DD H:mm a', 'Europe/London')
}
function parseTitle($item) {
return $item('td:eq(1)').text().trim()
}
function parseItems(content) {
const $ = cheerio.load(content)
return $('table.table > tbody > tr').toArray()
function parseText($item) {
return $item.text()
.replace(/\t/g, '')
.replace(/\n/g, ' ')
.replace(/ /g, ' ')
.trim()
}