diff --git a/sites/programme-tv.vini.pf/programme-tv.vini.pf.config.js b/sites/programme-tv.vini.pf/programme-tv.vini.pf.config.js new file mode 100644 index 00000000..267d7bc4 --- /dev/null +++ b/sites/programme-tv.vini.pf/programme-tv.vini.pf.config.js @@ -0,0 +1,94 @@ +const dayjs = require('dayjs') +const axios = require('axios') + +const API = { + url: `https://programme-tv.vini.pf/programmesJSON` +} + +module.exports = { + site: 'programme-tv.vini.pf', + url: API.url, + request: { + method: 'POST', + timeout: 30000, + data({ date }) { + return { + dateDebut: `${date.subtract(10, 'h').format('YYYY-MM-DDTHH:mm:ss')}-10:00` + } + } + }, + logo({ channel }) { + return channel.logo + }, + parser: async function ({ content, channel, date }) { + const programs = [] + const items = parseItems(content, channel) + if (items.length) { + for (let hours of [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]) { + const nextContent = await loadNextItems(date, hours) + const nextItems = parseItems(nextContent, channel) + for (let item of nextItems) { + if (!items.find(i => i.nidP === item.nidP)) { + items.push(item) + } + } + } + } + + items.forEach(item => { + programs.push({ + title: item.titreP, + description: item.desc, + category: item.categorieP, + icon: item.srcP, + start: dayjs.unix(item.timestampDeb), + stop: dayjs.unix(item.timestampFin) + }) + }) + + return programs + }, + async channels({ country, lang }) { + const data = await axios + .post(`https://programme-tv.vini.pf/programmesJSON`) + .then(r => r.data) + .catch(console.log) + + return data.programmes.map(item => { + const channelId = item.url.replace('/', '') + return { + lang: 'fr', + site_id: channelId, + name: channelId, + logo: item.src + } + }) + } +} + +async function loadNextItems(date, hours) { + date = date.add(hours, 'h') + + return axios + .post( + API.url, + { + dateDebut: `${date.subtract(10, 'h').format('YYYY-MM-DDTHH:mm:ss')}-10:00` + }, + { + responseType: 'arraybuffer' + } + ) + .then(res => res.data.toString()) + .catch(console.log) +} + +function parseItems(content, channel) { + if (!content) return [] + const data = JSON.parse(content) + if (!data || !Array.isArray(data.programmes)) return [] + const channelData = data.programmes.find(i => i.url === `/${channel.site_id}`) + if (!channelData) return [] + + return channelData.programmes || [] +}