Files
epg/sites/dsmart.com.tr/dsmart.com.tr.config.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-11-06 17:59:35 +03:00
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
2023-01-15 21:24:36 +03:00
const customParseFormat = require('dayjs/plugin/customParseFormat')
2021-11-06 17:59:35 +03:00
dayjs.extend(utc)
2023-01-15 21:24:36 +03:00
dayjs.extend(customParseFormat)
2021-11-06 17:59:35 +03:00
module.exports = {
site: 'dsmart.com.tr',
2023-01-10 12:40:01 +03:00
days: 2,
request: {
cache: {
ttl: 60 * 1000 // 60 seconds response cache
}
},
2021-11-06 17:59:35 +03:00
url({ date, channel }) {
2022-01-13 21:57:40 +03:00
return `https://www.dsmart.com.tr/api/v1/public/epg/schedules?page=1&limit=500&day=${date.format(
2021-11-11 16:37:44 +03:00
'YYYY-MM-DD'
)}`
2021-11-06 17:59:35 +03:00
},
2023-01-15 21:24:36 +03:00
parser: function ({ content, channel }) {
2021-11-06 17:59:35 +03:00
let offset = -1
let programs = []
2021-11-11 16:37:44 +03:00
const items = parseItems(content, channel)
2023-01-15 21:24:36 +03:00
items.forEach((item, i) => {
const prev = programs[programs.length - 1]
let start
if (prev) {
start = parseStart(item, prev.stop)
} else {
start = parseStart(item, dayjs.utc(item.day))
}
let duration = parseDuration(item)
let stop = start.add(duration, 's')
2021-11-06 17:59:35 +03:00
programs.push({
title: item.program_name,
category: item.genre,
2023-01-15 21:24:36 +03:00
description: item.description.trim(),
start,
stop
2021-11-06 17:59:35 +03:00
})
})
return programs
}
}
function parseStart(item, date) {
2023-01-15 21:24:36 +03:00
const time = dayjs.utc(item.start_date)
2021-11-06 17:59:35 +03:00
2023-01-15 21:24:36 +03:00
return dayjs.utc(`${date.format('YYYY-MM-DD')} ${time.format('HH:mm:ss')}`, 'YYYY-MM-DD HH:mm:ss')
2021-11-06 17:59:35 +03:00
}
2023-01-15 21:24:36 +03:00
function parseDuration(item) {
const [_, H, mm, ss] = item.duration.match(/(\d+):(\d+):(\d+)$/)
2021-11-11 16:37:44 +03:00
2023-01-15 21:24:36 +03:00
return parseInt(H) * 3600 + parseInt(mm) * 60 + parseInt(ss)
2021-11-11 16:37:44 +03:00
}
function parseItems(content, channel) {
2023-01-15 21:24:36 +03:00
const data = JSON.parse(content)
if (!data || !data.data || !Array.isArray(data.data.channels)) return null
const channelData = data.data.channels.find(i => i._id == channel.site_id)
2021-11-06 17:59:35 +03:00
2023-01-15 21:24:36 +03:00
return channelData && Array.isArray(channelData.schedule) ? channelData.schedule : []
2021-11-06 17:59:35 +03:00
}