From 91a6d207b26db44111cbf141e0733c382cf58899 Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Tue, 14 Jan 2025 23:19:56 +0300 Subject: [PATCH] Create telebilbao.es.config.js --- sites/telebilbao.es/telebilbao.es.config.js | 70 +++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 sites/telebilbao.es/telebilbao.es.config.js diff --git a/sites/telebilbao.es/telebilbao.es.config.js b/sites/telebilbao.es/telebilbao.es.config.js new file mode 100644 index 00000000..8355ce3f --- /dev/null +++ b/sites/telebilbao.es/telebilbao.es.config.js @@ -0,0 +1,70 @@ +const dayjs = require('dayjs') +const cheerio = require('cheerio') +const table2array = require('table2array') +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) + +require('dayjs/locale/es') + +module.exports = { + site: 'telebilbao.es', + days: 1, + url: 'https://www.telebilbao.es/programacion-2/', + request: { + cache: { + ttl: 24 * 60 * 60 * 1000 // 1 day + } + }, + parser({ content, date }) { + let programs = [] + const items = parseItems(content, date) + items.forEach(item => { + const prev = programs[programs.length - 1] + 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: item.title, + start, + stop + }) + }) + + return programs + } +} + +function parseStart(item, date) { + return dayjs.tz(`${date.format('YYYY-MM-DD')} ${item.time}`, 'YYYY-MM-DD HH:mm', 'Europe/Madrid') +} + +function parseItems(content, date) { + const $ = cheerio.load(content) + const tableHtml = $('table.programacion').html() + let tableArray = table2array(`${tableHtml}
`) + const day = date.locale('es').format('dddd\nD MMMM').toUpperCase() + if (!tableArray[0]) return [] + const indexOfColumn = tableArray[0].indexOf(day) + tableArray.pop() + const items = [] + tableArray.forEach(row => { + items.push({ + time: row[0], + title: row[indexOfColumn] + }) + }) + + return items.filter(i => Boolean(i.time)) +}