-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
301 lines (264 loc) · 10.4 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const express = require('express');
const { addonBuilder, serveHTTP } = require('stremio-addon-sdk');
const axios = require('axios');
const NodeCache = require('node-cache');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { HttpProxyAgent } = require('http-proxy-agent');
// Constants
const IPTV_CHANNELS_URL = 'https://iptv-org.github.io/api/channels.json';
const IPTV_STREAMS_URL = 'https://iptv-org.github.io/api/streams.json';
const PORT = process.env.PORT || 3000;
const FETCH_INTERVAL = parseInt(process.env.FETCH_INTERVAL) || 86400000; // Fetch interval in milliseconds, default 1 day
const PROXY_URL = process.env.PROXY_URL || ''; // Proxy URL for verification
const FETCH_TIMEOUT = parseInt(process.env.FETCH_TIMEOUT) || 10000; // Fetch timeout in milliseconds, default 10 seconds
// Configuration for channel filtering.
const config = {
includeLanguages: process.env.INCLUDE_LANGUAGES ? process.env.INCLUDE_LANGUAGES.split(',') : [],
includeCountries: process.env.INCLUDE_COUNTRIES ? process.env.INCLUDE_COUNTRIES.split(',') : ['GR'],
excludeLanguages: process.env.EXCLUDE_LANGUAGES ? process.env.EXCLUDE_LANGUAGES.split(',') : [],
excludeCountries: process.env.EXCLUDE_COUNTRIES ? process.env.EXCLUDE_COUNTRIES.split(',') : [],
excludeCategories: process.env.EXCLUDE_CATEGORIES ? process.env.EXCLUDE_CATEGORIES.split(',') : [],
};
// Express app setup
const app = express();
app.use(express.json());
// Cache setup
const cache = new NodeCache({ stdTTL: 0 }); // Set stdTTL to 0 for infinite TTL
// Addon Manifest
const manifest = {
id: 'org.iptv',
name: 'IPTV Addon',
version: '0.0.2',
description: `Watch live TV from ${config.includeCountries.join(', ')}`,
resources: ['catalog', 'meta', 'stream'],
types: ['tv'],
catalogs: config.includeCountries.map(country => ({
type: 'tv',
id: `iptv-channels-${country}`,
name: `IPTV - ${country}`,
extra: [
{
name: 'genre',
isRequired: false,
"options": [
"animation",
"business",
"classic",
"comedy",
"cooking",
"culture",
"documentary",
"education",
"entertainment",
"family",
"kids",
"legislative",
"lifestyle",
"movies",
"music",
"general",
"religious",
"news",
"outdoor",
"relax",
"series",
"science",
"shop",
"sports",
"travel",
"weather",
"xxx",
"auto"
]
}
],
})),
idPrefixes: ['iptv-'],
behaviorHints: { configurable: false, configurationRequired: false },
logo: "https://dl.strem.io/addon-logo.png",
icon: "https://dl.strem.io/addon-logo.png",
background: "https://dl.strem.io/addon-background.jpg",
};
const addon = new addonBuilder(manifest);
// Helper Functions
// Convert channel to Stremio accepted Meta object
const toMeta = (channel) => ({
id: `iptv-${channel.id}`,
name: channel.name,
type: 'tv',
genres: [...(channel.categories || []), channel.country].filter(Boolean),
poster: channel.logo,
posterShape: 'square',
background: channel.logo || null,
logo: channel.logo || null,
});
// Fetch and filter channels
const getChannels = async () => {
console.log("Downloading channels");
try {
const channelsResponse = await axios.get(IPTV_CHANNELS_URL, { timeout: FETCH_TIMEOUT });
console.log("Finished downloading channels");
return channelsResponse.data;
} catch (error) {
console.error('Error fetching channels:', error);
// If we fail to get new channels, serve from cache
if (cache.has('channels')) {
console.log('Serving channels from cache');
return cache.get('channels');
}
return null;
}
};
// Fetch Stream Info for the Channel
const getStreamInfo = async () => {
if (!cache.has('streams')) {
console.log("Downloading streams data");
try {
const streamsResponse = await axios.get(IPTV_STREAMS_URL, { timeout: FETCH_TIMEOUT });
cache.set('streams', streamsResponse.data);
} catch (error) {
console.error('Error fetching streams:', error);
return [];
}
}
return cache.get('streams');
};
// Verify stream URL
const verifyStreamURL = async (url, userAgent, httpReferrer) => {
const cachedResult = cache.get(url);
if (cachedResult !== undefined) {
return cachedResult;
}
const effectiveUserAgent = userAgent || 'Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 DMOST/2.0.0 (; LGE; webOSTV; WEBOS6.3.2 03.34.95; W6_lm21a;)';
const effectiveReferer = httpReferrer || '';
if (effectiveUserAgent !== 'Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 DMOST/2.0.0 (; LGE; webOSTV; WEBOS6.3.2 03.34.95; W6_lm21a;)') {
console.log(`Using User-Agent: ${effectiveUserAgent}`);
}
if (httpReferrer) {
console.log(`Using Referer: ${effectiveReferer}`);
}
let axiosConfig = {
timeout: FETCH_TIMEOUT,
headers: {
'User-Agent': effectiveUserAgent,
'Accept': '*/*',
'Referer': effectiveReferer
}
};
if (PROXY_URL) {
if (PROXY_URL.startsWith('socks')) {
axiosConfig.httpsAgent = new SocksProxyAgent(PROXY_URL);
} else {
axiosConfig.httpsAgent = new HttpProxyAgent(PROXY_URL);
}
}
try {
const response = await axios.head(url, axiosConfig);
const result = response.status === 200;
cache.set(url, result);
return result;
} catch (error) {
console.log(`Stream URL verification failed for ${url}:`, error.message);
cache.set(url, false);
return false;
}
};
// Get all channel information
const getAllInfo = async () => {
if (cache.has('channelsInfo')) {
return cache.get('channelsInfo');
}
const streams = await getStreamInfo();
const channels = await getChannels();
if (!channels) {
console.log('Failed to fetch channels, using cached data if available');
return cache.get('channelsInfo') || [];
}
const streamMap = new Map(streams.map(stream => [stream.channel, stream]));
const filteredChannels = channels.filter((channel) => {
if (config.includeCountries.length > 0 && !config.includeCountries.includes(channel.country)) return false;
if (config.excludeCountries.length > 0 && config.excludeCountries.includes(channel.country)) return false;
if (config.includeLanguages.length > 0 && !channel.languages.some(lang => config.includeLanguages.includes(lang))) return false;
if (config.excludeLanguages.length > 0 && channel.languages.some(lang => config.excludeLanguages.includes(lang))) return false;
if (config.excludeCategories.some(cat => channel.categories.includes(cat))) return false;
return streamMap.has(channel.id);
});
const channelsWithDetails = await Promise.all(filteredChannels.map(async (channel) => {
const streamInfo = streamMap.get(channel.id);
if (streamInfo && await verifyStreamURL(streamInfo.url, streamInfo.user_agent, streamInfo.http_referrer)) {
const meta = toMeta(channel);
meta.streamInfo = {
url: streamInfo.url,
title: 'Live Stream',
httpReferrer: streamInfo.http_referrer
};
return meta;
}
return null;
}));
const filteredChannelsInfo = channelsWithDetails.filter(Boolean);
cache.set('channelsInfo', filteredChannelsInfo);
return filteredChannelsInfo;
};
// Addon Handlers
// Catalog Handler
addon.defineCatalogHandler(async ({ type, id, extra }) => {
if (type === 'tv' && id.startsWith('iptv-channels-')) {
const country = id.split('-')[2];
const allChannels = await getAllInfo();
let filteredChannels = allChannels.filter(channel => channel.genres.includes(country));
if (extra && extra.genre) {
const genres = Array.isArray(extra.genre) ? extra.genre : [extra.genre];
filteredChannels = filteredChannels.filter(channel =>
genres.some(genre => channel.genres.includes(genre))
);
}
console.log(`Serving catalog for ${country} with ${filteredChannels.length} channels`);
return { metas: filteredChannels };
}
return { metas: [] };
});
// Meta Handler
addon.defineMetaHandler(async ({ type, id }) => {
if (type === 'tv' && id.startsWith('iptv-')) {
const channels = await getAllInfo();
const channel = channels.find((meta) => meta.id === id);
if (channel) {
return { meta: channel };
}
}
return { meta: {} };
});
// Stream Handler
addon.defineStreamHandler(async ({ type, id }) => {
if (type === 'tv' && id.startsWith('iptv-')) {
const channels = await getAllInfo();
const channel = channels.find((meta) => meta.id === id);
if (channel?.streamInfo) {
console.log("Serving stream id: ", channel.id);
return { streams: [channel.streamInfo] };
} else {
console.log('No matching stream found for channelID:', id);
}
}
return { streams: [] };
});
// Server setup
app.get('/manifest.json', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.json(manifest);
});
serveHTTP(addon.getInterface(), { server: app, path: '/manifest.json', port: PORT });
// Cache management
const fetchAndCacheInfo = async () => {
try {
const metas = await getAllInfo();
console.log(`${metas.length} channel(s) information cached successfully`);
} catch (error) {
console.error('Error caching channel information:', error);
}
};
// Initial fetch
fetchAndCacheInfo();
// Schedule fetch based on FETCH_INTERVAL
setInterval(fetchAndCacheInfo, FETCH_INTERVAL);