aboutsummaryrefslogtreecommitdiff
path: root/src/lib/m3u.ts
blob: e5138e02e4e22f3d5e7605bb16fcafb8b13bc586 (plain) (blame)
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
export interface Channel {
	groupTitle: string;
	tvgId: string;
	tvgLogo: string;
	name: string;
	url: string;
}

export const parseM3U = (m3uContent: string): Channel[] => {
	const lines = m3uContent.split('\n');
	const channels: Channel[] = [];
	let currentChannel: Partial<Channel> = {};

	for (const line of lines)
		if (line.startsWith('#EXTINF')) {
			const groupTitleMatch = line.match(/group-title="([^"]*)"/);
			const tvgIdMatch = line.match(/tvg-id="([^"]*)"/);
			const tvgLogoMatch = line.match(/tvg-logo="([^"]*)"/);
			const nameMatch = line.match(/,([^,]*)$/);

			currentChannel = {
				groupTitle: groupTitleMatch ? groupTitleMatch[1].trim() : '',
				tvgId: tvgIdMatch ? tvgIdMatch[1].trim() : '',
				tvgLogo: tvgLogoMatch ? tvgLogoMatch[1].trim() : '',
				name: nameMatch ? nameMatch[1].trim() : ''
			};
		} else if (line && !line.startsWith('#')) {
			currentChannel.url = line.trim();

			channels.push(currentChannel as Channel);

			currentChannel = {};
		}

	return channels;
};