Add ctc.ru

This commit is contained in:
shay
2025-06-22 11:45:03 -05:00
parent c5b983943f
commit 399261558f
5 changed files with 206 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<channels>
<channel site="ctc.ru" lang="ru" xmltv_id="STS.ru" site_id="ctc">STS</channel>
</channels>

View File

@@ -0,0 +1,95 @@
const dayjs = require('dayjs')
module.exports = {
site: 'ctc.ru',
days: 1,
url: ({ date }) => `https://ctc.ru/api/page/v2/programm/?date=${formatDate(date)}`,
parser({ content }) {
const programs = []
const items = parseItems(content)
for (const item of items) {
programs.push({
title: item.bubbleTitle,
// more like "films", "shows", "cartoons" - not a genre
category: item.bubbleSubTitle,
icons: parseIcons(item),
images: parseImages(item),
start: parseStart(item),
stop: parseStop(item),
// not sure if CTC uses this more like `premiere` but I don't have any
// additional info to use in the `premiere` field so I'm using this
// instead.
new: item.isPremiere ?? false,
url: item.bubbleUrl ? `https://ctc.ru${item.bubbleUrl}` : undefined,
rating: parseRating(item),
})
}
return programs
}
}
function formatDate(date) {
return dayjs(date).format('DD-MM-YYYY')
}
function parseIcons(item) {
const images = item.bubbleImage ?? []
// biggest first
const sorted = images.sort((a, b) => b.height - a.height)
return sorted.map((image) => ({
src: image.url,
width: image.width,
height: image.height,
}))
}
function parseImages(item) {
const images = item.trackImageUrl ?? []
// biggest first
const sorted = images.sort((a, b) => b.height - a.height)
// compile one image of each size since the content should be the same
const sizes = {}
for (const image of sorted) {
const maxRes = Math.max(image.width, image.height)
const item = {
type: 'backdrop',
// https://github.com/ektotv/xmltv/blob/801417b4b7aae38f13caa82d8bbfbed0a254ee5f/src/types.ts#L40-L48
size: maxRes > 400 ? '3' : maxRes > 200 ? '2' : '1',
orient: image.height > image.width ? 'P' : 'L',
value: image.url,
}
if (sizes[item.size]) continue
sizes[item.size] = item
}
// re-sort so the size 3 images are first
return Object
.values(sizes)
.sort((a, b) => Number(b.size) - Number(a.size))
}
function parseStart(item) {
return dayjs(item.startTime)
}
function parseStop(item) {
return dayjs(item.endTime)
}
function parseRating(item) {
if (item.ageLimit == null) return null
return {
// Not sure what the Russian system is actually called (if anything)
system: 'Russia',
value: `${item.ageLimit}+`,
}
}
function parseItems(content) {
const node = JSON.parse(content).content.find(n => n.type === 'tv-program')
if (node) return node.widgets
return []
}

View File

@@ -0,0 +1,91 @@
const { parser, url } = require('./ctc.ru.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)
const date = dayjs.utc('2025-06-22')
it('can generate valid url', () => {
expect(url({ date })).toBe('https://ctc.ru/api/page/v2/programm/?date=22-06-2025')
})
it('can parse response', () => {
const content = fs.readFileSync(path.resolve(__dirname, '__data__/content.json'))
let results = parser({ content, date })
results = results.map(p => {
p.start = p.start.toJSON()
p.stop = p.stop.toJSON()
return p
})
expect(results[0]).toMatchObject({
start: '2025-06-22T03:00:00.000Z',
stop: '2025-06-22T03:55:00.000Z',
title: 'Три кота',
category: 'Мультфильмы',
new: false,
url: 'https://ctc.ru/collections/multiki/',
icons: [
{
src: 'https://mgf-static-ssl.ctc.ru/images/ctc-entity-dictionary-category/5/iconurl/web/5f9027fcba9ac-125x125.png',
width: 125,
height: 125,
},
{
src: 'https://mgf-static-ssl.ctc.ru/images/ctc-entity-dictionary-category/5/iconurl/web/5f9027fc99587-60x60.png',
width: 60,
height: 60,
},
],
images: [
{
type: 'backdrop',
size: '3',
orient: 'L',
value: 'https://mgf-static-ssl.ctc.ru/images/ctc-entity-project/1129/horizontalcover/web/66c319f0a31b5-1740x978.jpeg',
},
{
type: 'backdrop',
size: '2',
orient: 'L',
value: 'https://mgf-static-ssl.ctc.ru/images/ctc-entity-project/1129/horizontalcover/web/66c319f20bac2-400x225.jpeg',
},
{
type: 'backdrop',
size: '1',
orient: 'L',
value: 'https://mgf-static-ssl.ctc.ru/images/ctc-entity-project/1129/horizontalcover/web/66c319f244f92-150x84.jpeg',
},
],
rating: {
system: 'Russia',
value: '0+',
},
})
})
it('can handle empty guide', () => {
const results = parser({
content: JSON.stringify({
isActive: true,
url: '/programm/',
header: [],
sidebar: [],
footer: [],
content: [],
seoTags: {},
ogMarkup: {},
userGeo: null,
userData: null,
meta: {},
activeFrom: null,
activeTo: null,
type: 'tv-program-page',
})
})
expect(results).toMatchObject([])
})

15
sites/ctc.ru/readme.md Normal file
View File

@@ -0,0 +1,15 @@
# ctc.ru
https://ctc.ru/programm
### Download the guide
```sh
npm run grab --- --site=ctc.ru
```
### Test
```sh
npm test --- ctc.ru
```