forked from mrcanelas/pure-tv-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.js
More file actions
139 lines (117 loc) · 3.78 KB
/
Copy pathaddon.js
File metadata and controls
139 lines (117 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { addonBuilder } = require("stremio-addon-sdk")
const manifest = require("./src/manifest")
const { decodeConfig, isEncodedConfig } = require('./src/configCodec')
const {
loadPlaylist,
loadXmltv,
m3uChannelsToMetas,
buildChannelIndex,
xmltvToVideosForChannel,
} = require('./src/iptv')
const ADDON_PREFIX = 'pure:'
const cache = new Map()
const CACHE_MS = 2 * 60 * 1000
async function getState(cfg) {
const now = Date.now()
const hit = cache.get(cfg)
if (hit && now - hit.at < CACHE_MS) return hit.state
if (!isEncodedConfig(cfg)) throw new Error('Config inválida (esperado formato inline c_...)')
const cfgData = decodeConfig(cfg)
const playlist = await loadPlaylist(cfgData)
const xmltv = await loadXmltv(cfgData)
const index = buildChannelIndex(ADDON_PREFIX, playlist)
const state = { cfgData, playlist, xmltv, index }
cache.set(cfg, { at: now, state })
return state
}
const builder = new addonBuilder(manifest)
const CATALOG_PAGE_SIZE = 20
builder.defineCatalogHandler(({type, id, extra}) => {
return (async () => {
if (!id.startsWith(ADDON_PREFIX)) return { metas: [] }
const cfg = extra && extra.__cfg
if (!cfg) return { metas: [] }
const skip = Math.max(0, parseInt(extra && extra.skip, 10) || 0)
const { playlist } = await getState(cfg)
const allMetas = m3uChannelsToMetas(ADDON_PREFIX, playlist.channels || [])
const metas = allMetas.slice(skip, skip + CATALOG_PAGE_SIZE)
return {
metas,
// cache curto para o EPG/plalist mudar sem travar o usuário
cacheMaxAge: 300, // 5 min
staleRevalidate: 1800, // 30 min
staleError: 604800, // 7 dias
}
})()
})
builder.defineMetaHandler(({type, id, extra}) => {
return (async () => {
if (!id.startsWith(ADDON_PREFIX)) return { meta: null }
const cfg = extra && extra.__cfg
if (!cfg) return { meta: null }
const { xmltv, index } = await getState(cfg)
const ch = index.idToChannel.get(id)
if (!ch) return { meta: null }
const displayName = ch.tvgName || ch.name || ch.tvgId || 'Canal'
const poster = `https://da5f663b4690-proxyimage.baby-beamup.club/proxy-image/?url=${ch.tvgLogo || (ch.extras && (ch.extras['tvg-logo'] || ch.extras['logo'])) || undefined}`
const candidates = new Set()
for (const v of [
ch.tvgId,
ch.tvgName,
ch.name,
ch.extras && ch.extras['tvg-id'],
ch.extras && ch.extras['tvg-name'],
]) {
if (typeof v === 'string' && v.trim()) candidates.add(v.trim())
}
if (Array.isArray(xmltv.channels) && candidates.size) {
for (const c of xmltv.channels) {
const dns = c['display-name']
const arr = !dns ? [] : (Array.isArray(dns) ? dns : [dns])
const names = arr.map((dn) => ((dn && (dn.value || dn._)) || '').trim()).filter(Boolean)
if (!names.length) continue
const hit = names.some((n) => candidates.has(n))
if (hit && c.id) candidates.add(String(c.id).trim())
}
}
const videos = xmltvToVideosForChannel(xmltv, id, Array.from(candidates))
return {
meta: {
id,
type: 'tv',
name: displayName,
logo: poster,
poster,
posterShape: 'landscape',
videos,
},
cacheMaxAge: 900, // 15 min
staleRevalidate: 3600, // 1h
staleError: 604800, // 7 dias
}
})()
})
builder.defineStreamHandler(({type, id, extra}) => {
return (async () => {
if (!id.startsWith(ADDON_PREFIX)) return { streams: [] }
const cfg = extra && extra.__cfg
if (!cfg) return { streams: [] }
const { index } = await getState(cfg)
const ch = index.idToChannel.get(`${ADDON_PREFIX}${id.split(':')[1]}`)
if (!ch || !ch.url) return { streams: [] }
const name = ch.tvgName || ch.name || 'Live'
return {
streams: [
{
name: 'PureTV',
title: name,
url: ch.url,
},
],
cacheMaxAge: 3600, // 1h
staleRevalidate: 7200, // 2h
staleError: 604800, // 7 dias
}
})()
})
module.exports = builder.getInterface()