Files
IPTV-Restream/backend/services/PlaylistService.js
2024-12-20 00:49:41 +01:00

63 lines
1.8 KiB
JavaScript

const m3uParser = require('iptv-playlist-parser');
const ChannelService = require('./ChannelService');
class PlaylistService {
async addPlaylist(playlistUrl, restream, headersJson) {
const response = await fetch(playlistUrl);
const content = await response.text();
const parsedPlaylist = m3uParser.parse(content);
// list of added channels
const channels = parsedPlaylist.items.map(channel => {
//TODO: add channel.http if not '' to headers
try {
return ChannelService.addChannel({
name: channel.name,
url: channel.url,
avatar: channel.tvg.logo,
restream: restream,
headersJson: headersJson,
group: channel.group.title,
playlist: playlistUrl
});
} catch (error) {
console.error(error);
return null;
}
})
.filter(result => result !== null);
return channels;
}
updatePlaylist(playlistUrl, updatedAttributes) {
const channels = ChannelService
.getChannels()
.filter(channel => channel.playlist === playlistUrl);
for(var channel of channels) {
channel = ChannelService.updateChannel(channel.id, updatedAttributes);
}
return channels;
}
deletePlaylist(playlistUrl) {
const channels = ChannelService
.getChannels()
.filter(channel => channel.playlist === playlistUrl);
for(const channel of channels) {
ChannelService.deleteChannel(channel.id);
}
return channels;
}
}
module.exports = new PlaylistService();