-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
551 lines (487 loc) · 16.6 KB
/
app.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Global variables
let currentPage = 1;
const itemsPerPage = 5;
let activeFilters = [];
// Add this helper function at the top of the file with other functions
async function checkServerAvailability() {
try {
const response = await fetch('http://localhost:5000/check_url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://www.google.com' // Test URL
})
});
return response.ok;
} catch (error) {
return false;
}
}
// Wait for DOM to be fully loaded before attaching event listeners
document.addEventListener('DOMContentLoaded', function() {
// Initialize event listeners
initializeEventListeners();
// Load channels on initial page load
loadChannels();
});
// Initialize all event listeners
function initializeEventListeners() {
// Form submission
document.getElementById('channel-form').addEventListener('submit', handleChannelSubmit);
// Playlist management
document.getElementById('save-playlist').addEventListener('click', handleSavePlaylist);
document.getElementById('load-playlist-button').addEventListener('click', handleLoadPlaylistClick);
document.getElementById('load-playlist').addEventListener('change', handleLoadPlaylist);
// Channel management
document.getElementById('check-urls').addEventListener('click', handleCheckUrls);
document.getElementById('sort-channels').addEventListener('click', handleSortChannels);
// M3U operations
document.getElementById('export-m3u').addEventListener('click', handleExportM3uClick);
document.getElementById('exportOption').addEventListener('change', handleExportOptionChange);
document.getElementById('exportConfirm').addEventListener('click', handleExportConfirm);
document.getElementById('import-m3u-button').addEventListener('click', handleImportM3uClick);
document.getElementById('import-m3u').addEventListener('change', handleImportM3u);
// Filter controls
document.getElementById('add-filter').addEventListener('click', handleAddFilter);
// Add this line
document.getElementById('add-channel-button').addEventListener('click', () => $('#channelModal').modal('show'));
document.getElementById('save-channel').addEventListener('click', handleChannelSubmit);
}
// Channel form handler
function handleChannelSubmit(e) {
if (e) e.preventDefault();
var name = document.getElementById('name').value;
var logoId = document.getElementById('logo-id').value;
var groupId = document.getElementById('group-id').value;
var epgId = document.getElementById('epg-id').value;
var providerId = document.getElementById('provider-id').value;
var streamUrls = document.getElementById('stream-urls').value.split('\n');
var channels = JSON.parse(localStorage.getItem('channels')) || [];
var existingChannelIndex = channels.findIndex(function(channel) {
return channel.name === name || channel.epgId === epgId;
});
if (existingChannelIndex !== -1) {
if (confirm('A channel with this name or EPG ID already exists. Do you want to update it?')) {
var existingStreamUrls = channels[existingChannelIndex].streamUrls;
channels[existingChannelIndex] = {
name,
logoId,
groupId,
epgId,
providerId,
streamUrls: [...new Set([...existingStreamUrls, ...streamUrls])]
};
}
} else {
channels.push({
name,
logoId,
groupId,
epgId,
providerId,
streamUrls
});
}
localStorage.setItem('channels', JSON.stringify(channels));
loadChannels();
// Add these lines at the end
$('#channelModal').modal('hide');
document.getElementById('channel-form').reset();
}
// Channel loading and display
function loadChannels() {
if (activeFilters.length > 0) {
applyFilters();
} else {
const channels = JSON.parse(localStorage.getItem('channels')) || [];
displayChannels(channels);
}
}
// New function to handle channel display
function displayChannels(channels) {
const channelsContainer = document.getElementById('channels-container');
const emptyMessage = document.getElementById('empty-message');
channelsContainer.innerHTML = '';
if (channels.length === 0) {
emptyMessage.style.display = 'block';
return;
}
emptyMessage.style.display = 'none';
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = Math.min(startIndex + itemsPerPage, channels.length);
// Add channel cards
for (let i = startIndex; i < endIndex; i++) {
let channel = channels[i];
if (!channel.streamUrls) {
channel.streamUrls = [];
}
var div = document.createElement('div');
div.className = 'card mb-3';
var cardBody = document.createElement('div');
cardBody.className = 'card-body';
cardBody.innerHTML = '<h5 class="card-title">' + channel.name + '</h5>' +
'<p class="card-text">Logo ID: ' + channel.logoId + '</p>' +
'<p class="card-text">Group ID: ' + channel.groupId + '</p>' +
'<p class="card-text">EPG ID: ' + channel.epgId + '</p>' +
'<p class="card-text">Provider ID: ' + channel.providerId + '</p>' +
'<p class="card-text">Stream URLs: ' + channel.streamUrls.join(', ') + '</p>';
var deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.className = 'btn btn-danger';
deleteButton.addEventListener('click', function() {
channels.splice(i, 1);
localStorage.setItem('channels', JSON.stringify(channels));
loadChannels();
});
var editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.className = 'btn btn-primary ml-2';
editButton.addEventListener('click', function() {
document.getElementById('name').value = channel.name;
document.getElementById('logo-id').value = channel.logoId;
document.getElementById('group-id').value = channel.groupId;
document.getElementById('epg-id').value = channel.epgId;
document.getElementById('provider-id').value = channel.providerId;
document.getElementById('stream-urls').value = channel.streamUrls.join('\n');
$('#channelModal').modal('show');
});
cardBody.appendChild(deleteButton);
cardBody.appendChild(editButton);
div.appendChild(cardBody);
channelsContainer.appendChild(div);
}
// Modify pagination to work with filtered results
const totalPages = Math.ceil(channels.length / itemsPerPage);
if (totalPages > 1) {
const pagination = document.createElement('nav');
const ul = document.createElement('ul');
ul.className = 'pagination justify-content-center';
// Previous button
const prevLi = document.createElement('li');
prevLi.className = `page-item ${currentPage === 1 ? 'disabled' : ''}`;
const prevA = document.createElement('a');
prevA.className = 'page-link';
prevA.href = '#';
prevA.textContent = 'Previous';
prevA.addEventListener('click', (e) => {
e.preventDefault();
if (currentPage > 1) {
currentPage--;
displayChannels(channels);
}
});
prevLi.appendChild(prevA);
ul.appendChild(prevLi);
// Calculate visible page range
let startPage = Math.max(1, currentPage - 2);
let endPage = Math.min(totalPages, startPage + 4);
// Adjust start if we're near the end
if (endPage - startPage < 4) {
startPage = Math.max(1, endPage - 4);
}
// First page if not in range
if (startPage > 1) {
const li = document.createElement('li');
li.className = 'page-item';
const a = document.createElement('a');
a.className = 'page-link';
a.href = '#';
a.textContent = '1';
a.addEventListener('click', (e) => {
e.preventDefault();
currentPage = 1;
displayChannels(channels);
});
li.appendChild(a);
ul.appendChild(li);
if (startPage > 2) {
const ellipsis = document.createElement('li');
ellipsis.className = 'page-item disabled';
ellipsis.innerHTML = '<span class="page-link">...</span>';
ul.appendChild(ellipsis);
}
}
// Visible pages
for (let i = startPage; i <= endPage; i++) {
const li = document.createElement('li');
li.className = `page-item ${i === currentPage ? 'active' : ''}`;
const a = document.createElement('a');
a.className = 'page-link';
a.href = '#';
a.textContent = i;
const pageNum = i; // Store the page number
a.addEventListener('click', (e) => {
e.preventDefault();
currentPage = pageNum;
displayChannels(channels);
});
li.appendChild(a);
ul.appendChild(li);
}
// Last page if not in range
if (endPage < totalPages) {
if (endPage < totalPages - 1) {
const ellipsis = document.createElement('li');
ellipsis.className = 'page-item disabled';
ellipsis.innerHTML = '<span class="page-link">...</span>';
ul.appendChild(ellipsis);
}
const li = document.createElement('li');
li.className = 'page-item';
const a = document.createElement('a');
a.className = 'page-link';
a.href = '#';
a.textContent = totalPages;
a.addEventListener('click', (e) => {
e.preventDefault();
currentPage = totalPages;
displayChannels(channels);
});
li.appendChild(a);
ul.appendChild(li);
}
// Next button
const nextLi = document.createElement('li');
nextLi.className = `page-item ${currentPage === totalPages ? 'disabled' : ''}`;
const nextA = document.createElement('a');
nextA.className = 'page-link';
nextA.href = '#';
nextA.textContent = 'Next';
nextA.addEventListener('click', (e) => {
e.preventDefault();
if (currentPage < totalPages) {
currentPage++;
displayChannels(channels);
}
});
nextLi.appendChild(nextA);
ul.appendChild(nextLi);
pagination.appendChild(ul);
channelsContainer.appendChild(pagination);
}
}
// Playlist save handler
function handleSavePlaylist() {
var channels = JSON.parse(localStorage.getItem('channels')) || [];
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(channels));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "playlist.json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
// Playlist load handlers
function handleLoadPlaylistClick() {
document.getElementById('load-playlist').click();
}
function handleLoadPlaylist(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
localStorage.setItem('channels', contents);
loadChannels();
};
reader.readAsText(file);
}
// URL checking handler
async function handleCheckUrls() {
// First check if the server is available
const isServerAvailable = await checkServerAvailability();
if (!isServerAvailable) {
alert('URL validator server is not available. Please make sure the server is running on localhost:5000');
return;
}
var channels = JSON.parse(localStorage.getItem('channels')) || [];
var promises = []; // Array to hold all fetch promises
channels.forEach(function(channel, index) {
channel.streamUrls.forEach(function(url, i) {
var promise = fetch('http://localhost:5000/check_url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: url
})
})
.then(response => response.json())
.then(data => {
if (data.status !== 200) {
// Move the URL to the end of the list
channel.streamUrls.push(channel.streamUrls.splice(i, 1)[0]);
}
})
.catch(error => {
console.error('Error checking URL:', url, error);
});
promises.push(promise);
});
});
// Wait for all fetch promises to complete
Promise.all(promises).then(function() {
localStorage.setItem('channels', JSON.stringify(channels));
loadChannels();
alert('URL check completed!');
}).catch(function(error) {
console.error('Error during URL checks:', error);
alert('An error occurred while checking URLs. Please try again.');
});
}
// Channel sorting handler
function handleSortChannels() {
var channels = JSON.parse(localStorage.getItem('channels')) || [];
channels.sort(function(a, b) {
if (a.groupId < b.groupId) {
return -1;
}
if (a.groupId > b.groupId) {
return 1;
}
// If Group IDs are equal, sort by name
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
localStorage.setItem('channels', JSON.stringify(channels));
loadChannels();
alert('Channels sorted successfully!');
}
// M3U export handlers
function handleExportM3uClick() {
$('#exportModal').modal('show');
}
function handleExportOptionChange() {
if (this.value === 'all') {
document.getElementById('exportValue').style.display = 'none';
} else {
document.getElementById('exportValue').style.display = 'block';
}
}
function handleExportConfirm() {
var channels = JSON.parse(localStorage.getItem('channels')) || [];
var exportOption = document.getElementById('exportOption').value;
var exportValue = document.getElementById('exportValue').value;
if (exportOption !== 'all') {
channels = channels.filter(function(channel) {
return channel[exportOption === 'group' ? 'groupId' : 'providerId'] === exportValue;
});
}
var m3uContent = "#EXTM3U\n";
channels.forEach(function(channel) {
m3uContent += "#EXTINF:-1 tvg-id=\"" + channel.epgId + "\" tvg-name=\"" + channel.name + "\" tvg-logo=\"" + channel.logoId + "\" group-title=\"" + channel.groupId + "\", " + channel.name + "\n";
m3uContent += channel.streamUrls[0] + "\n";
});
var dataStr = "data:audio/x-mpegurl;charset=utf-8," + encodeURIComponent(m3uContent);
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "playlist.m3u");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
$('#exportModal').modal('hide');
}
// M3U import handlers
function handleImportM3uClick() {
document.getElementById('import-m3u').click();
}
function handleImportM3u(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
var lines = contents.split('\n');
var channels = [];
var currentChannel = null;
lines.forEach(function(line) {
if (line.startsWith('#EXTINF:')) {
if (currentChannel) {
channels.push(currentChannel);
}
currentChannel = {};
var matches = line.match(/tvg-id="([^"]*)".*tvg-logo="([^"]*)".*group-title="([^"]*)",\s*(.*)/);
if (matches) {
currentChannel.epgId = matches[1] ? matches[1] : "";
currentChannel.logoId = matches[2] ? matches[2] : "";
currentChannel.groupId = matches[3] ? matches[3] : "";
// Split the remaining string on the last comma to separate the name and the rest of the string
var nameAndRest = matches[4].split(/,(.+)/);
currentChannel.name = nameAndRest[0] ? nameAndRest[0].trim() : "";
currentChannel.providerId = ""; // Provider ID is not available in the M3U file
}
} else if (line.startsWith('http')) {
currentChannel.streamUrls = [line.trim()];
}
});
if (currentChannel) {
channels.push(currentChannel);
}
localStorage.setItem('channels', JSON.stringify(channels));
loadChannels();
};
reader.readAsText(file);
}
// Filter functions
function handleAddFilter() {
const searchTerm = document.getElementById('search-input').value.trim();
const filterType = document.getElementById('filter-type').value;
if (!searchTerm) return;
const newFilter = {
id: Date.now(),
type: filterType,
term: searchTerm
};
activeFilters.push(newFilter);
document.getElementById('search-input').value = '';
updateFilterTags();
applyFilters();
}
function updateFilterTags() {
const filterContainer = document.getElementById('active-filters');
filterContainer.innerHTML = '';
activeFilters.forEach(filter => {
const tag = document.createElement('span');
tag.className = 'badge badge-primary mr-2 mb-2 p-2';
tag.style.fontSize = '0.9rem';
tag.innerHTML = `
${filter.type === 'all' ? 'All' : filter.type}: ${filter.term}
<i class="fas fa-times ml-2" style="cursor: pointer;"></i>
`;
tag.querySelector('i').addEventListener('click', () => {
activeFilters = activeFilters.filter(f => f.id !== filter.id);
updateFilterTags();
applyFilters();
});
filterContainer.appendChild(tag);
});
}
function applyFilters() {
const channels = JSON.parse(localStorage.getItem('channels')) || [];
const filteredChannels = channels.filter(channel => {
return activeFilters.every(filter => {
if (filter.type === 'all') {
return Object.values(channel).some(value =>
String(value).toLowerCase().includes(filter.term.toLowerCase())
);
}
return String(channel[filter.type])
.toLowerCase()
.includes(filter.term.toLowerCase());
});
});
currentPage = 1; // Reset to first page when filters change
displayChannels(filteredChannels);
}