Files
epg/sites/tvtv.us/tvtv.us.config.js
T

152 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-01-01 12:27:22 +03:00
const dayjs = require('dayjs')
2025-01-27 05:41:09 +03:00
let cachedPrograms = {}
2023-10-15 14:08:23 +03:00
module.exports = {
site: 'tvtv.us',
days: 2,
2025-01-27 04:50:14 +03:00
url({ date, channel }) {
2024-01-20 11:29:53 -06:00
return `https://www.tvtv.us/api/v1/lineup/USA-NY71652-X/grid/${date.toJSON()}/${date
2024-11-20 15:41:59 -06:00
.add(1, 'day')
2023-10-15 14:08:23 +03:00
.toJSON()}/${channel.site_id}`
},
2025-02-01 00:56:43 +03:00
request: {
headers: {
Accept: '*/*',
Connection: 'keep-alive',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
},
2025-03-31 09:12:29 +03:00
async parser(ctx) {
2023-10-15 14:08:23 +03:00
let programs = []
2025-01-27 05:41:09 +03:00
let queue = []
2023-10-15 14:08:23 +03:00
2025-03-31 09:12:29 +03:00
const items = parseItems(ctx.content)
2025-02-01 00:56:43 +03:00
for (const item of items) {
2025-01-27 04:50:14 +03:00
const start = dayjs(item.startTime)
const stop = start.add(item.duration, 'minute')
2023-10-15 14:08:23 +03:00
programs.push({
2025-01-27 05:41:09 +03:00
id: item.programId,
2023-10-15 14:08:23 +03:00
title: item.title,
2025-01-27 04:50:14 +03:00
subtitle: item.subtitle || null,
2023-10-15 14:08:23 +03:00
start,
stop
})
2025-01-27 05:41:09 +03:00
2025-03-31 09:12:29 +03:00
// NOTE: This part of the code is commented out because loading additional data leads either to error 429 Too Many Requests or to even greater delays between requests.
// if (item.programId && !cachedPrograms[item.programId]) {
// queue.push({
// programId: item.programId,
// url: `https://tvtv.us/api/v1/programs/${item.programId}`,
// httpAgent: ctx.request.agent,
// httpsAgent: ctx.request.agent,
// headers: module.exports.request.headers
// })
// }
2025-02-01 00:56:43 +03:00
}
2025-01-27 05:41:09 +03:00
2025-02-01 00:56:43 +03:00
const axios = require('axios')
for (const req of queue) {
await wait(5000)
const data = await axios(req)
.then(r => r.data)
.catch(console.error)
if (!data || !data.title) continue
2025-01-27 05:41:09 +03:00
cachedPrograms[req.programId] = data
2025-02-01 00:56:43 +03:00
}
2025-01-27 05:41:09 +03:00
programs.forEach(program => {
const data = cachedPrograms[program.id]
if (!data) return
program.description = data.description || null
program.image = data.image ? `https://tvtv.us${data.image}` : null
program.date = data.releaseYear ? data.releaseYear.toString() : null
program.directors = data.directors
program.categories = data.genres
program.actors = parseActors(data)
program.writers = parseWriters(data)
program.producers = parseProducers(data)
program.ratings = parseRatings(data)
program.season = parseSeason(data)
program.episode = parseEpisode(data)
2023-10-15 14:08:23 +03:00
})
return programs
}
}
2025-01-27 05:41:09 +03:00
function parseEpisode(data) {
if (!data?.seriesEpisode?.seasonEpisode) return null
const [, episode] = data.seriesEpisode.seasonEpisode.match(/Episode (\d+)/) || [null, null]
return episode ? parseInt(episode) : null
}
function parseSeason(data) {
if (!data?.seriesEpisode?.seasonEpisode) return null
const [, season] = data.seriesEpisode.seasonEpisode.match(/Season (\d+);/) || [null, null]
return season ? parseInt(season) : null
}
function parseRatings(data) {
2025-02-01 00:56:43 +03:00
return Array.isArray(data.ratings)
? data.ratings.map(rating => ({
value: rating.code,
system: rating.body
}))
: []
2025-01-27 05:41:09 +03:00
}
function parseWriters(data) {
return data.crew.filter(member => member.role.includes('Writer')).map(member => member.name)
}
function parseProducers(data) {
return data.crew.filter(member => member.role.includes('Producer')).map(member => member.name)
}
function parseActors(data) {
return data.cast.map(actor => {
const guest = actor.role.includes('Guest Star') ? 'yes' : undefined
const role = actor.role.replace(' - Guest Star', '')
return {
value: actor.name,
role,
guest
}
})
}
2023-10-15 14:08:23 +03:00
function parseItems(content) {
2025-01-27 04:50:14 +03:00
try {
const json = JSON.parse(content)
if (!json.length) return []
return json[0]
} catch {
return []
}
2023-10-15 14:08:23 +03:00
}
2025-02-01 00:56:43 +03:00
function wait(ms) {
2025-02-01 01:08:23 +03:00
if (process.env.NODE_ENV === 'test') return
2025-02-01 00:56:43 +03:00
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}