mirror of
https://github.com/4gray/iptvnator.git
synced 2026-10-09 01:16:15 -08:00
95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { v4 as uuidv4 } from 'uuid';
|
|
import { ParsedPlaylist } from '../src/typings';
|
|
import { Channel } from './channel.interface';
|
|
import { GLOBAL_FAVORITES_PLAYLIST_ID } from './constants';
|
|
import { Playlist } from './playlist.interface';
|
|
|
|
/**
|
|
* Aggregates favorite channels as objects from all available playlists
|
|
* @param playlists all available playlists
|
|
* @returns an array with favorite channels from all playlists
|
|
*/
|
|
export function aggregateFavoriteChannels(playlists: Playlist[]): Channel[] {
|
|
const favorites = [];
|
|
playlists.forEach((playlist) => {
|
|
if (playlist.favorites?.length > 0) {
|
|
playlist.playlist.items.forEach((channel) => {
|
|
if (
|
|
playlist.favorites.includes(channel.id) ||
|
|
playlist.favorites.includes(channel.url)
|
|
) {
|
|
favorites.push(channel);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
return favorites;
|
|
}
|
|
|
|
/**
|
|
* Creates a simplified playlist object which is used for global favorites
|
|
* @param channels channels list
|
|
* @returns simplified playlist object
|
|
*/
|
|
export function createFavoritesPlaylist(
|
|
channels: Channel[]
|
|
): Partial<Playlist> {
|
|
return {
|
|
_id: GLOBAL_FAVORITES_PLAYLIST_ID,
|
|
count: channels.length,
|
|
playlist: {
|
|
items: channels,
|
|
},
|
|
filename: 'Global favorites',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Returns last segment (part after last slash "/") of the given URL
|
|
* @param value URL as string
|
|
*/
|
|
export const getFilenameFromUrl = (value: string): string => {
|
|
if (value && value.length > 1) {
|
|
return value.substring(value.lastIndexOf('/') + 1);
|
|
}
|
|
return 'Untitled playlist';
|
|
};
|
|
|
|
/**
|
|
* Creates a playlist object
|
|
* @param name name of the playlist
|
|
* @param playlist playlist to save
|
|
* @param urlOrPath absolute fs path or url of the playlist
|
|
* @param uploadType upload type - by file or via an url
|
|
*/
|
|
export const createPlaylistObject = (
|
|
name: string,
|
|
playlist: ParsedPlaylist,
|
|
urlOrPath?: string,
|
|
uploadType?: 'URL' | 'FILE' | 'TEXT'
|
|
): Playlist => {
|
|
return {
|
|
_id: uuidv4(),
|
|
filename: name,
|
|
title: name,
|
|
count: playlist.items.length,
|
|
playlist: {
|
|
...playlist,
|
|
items: playlist.items.map((item) => ({
|
|
id: uuidv4(),
|
|
...item,
|
|
})),
|
|
},
|
|
importDate: new Date().toISOString(),
|
|
lastUsage: new Date().toISOString(),
|
|
favorites: [],
|
|
autoRefresh: false,
|
|
...(uploadType === 'URL' ? { url: urlOrPath } : {}),
|
|
...(uploadType === 'FILE' ? { filePath: urlOrPath } : {}),
|
|
};
|
|
};
|
|
|
|
export const getExtensionFromUrl = (url: string) => {
|
|
return url.split(/[#?]/)[0].split('.').pop().trim();
|
|
};
|