M3U Parser Java

 

public class M3UPrser {


public static void main(String[] args) {

//

//final String regex = "(?:^|\\n)#EXTINF:(?:-?\\d+(\\.\\d+)?),(.*?)[\\r\\n]+(.*)";
//final String regex = "(?:^|\\n)#EXTINF:" + "-?(\\d+)(\\.\\d+)?" + ",(.*?)[\\r\\n]+(.*)";
final String regex = "(?:^|\\n)#EXTINF:" + "(.*?)" + ",(.*?)[\\r\\n]+(.*)";

// final String reg2 =
// "(?:\\s+tvg-id=\"([^\"]*)\")?" +
// "(?:\\s+tvg-name=\"([^\"]*)\")?" +
// "(?:\\s+tvg-logo=\"([^\"]*)\")?" +
// "(?:\\s+group-title=\"([^\"]*)\")?";


// Регулярные выражения для каждого атрибута
final String idRegex = "tvg-id=\"([^\"]*)\"";
final String nameRegex = "tvg-name=\"([^\"]*)\"";
final String logoRegex = "tvg-logo=\"([^\"]*)\"";
final String groupRegex = "group-title=\"([^\"]*)\"";

// Создаем паттерны
final Pattern idPattern = Pattern.compile(idRegex);
final Pattern namePattern = Pattern.compile(nameRegex);
final Pattern logoPattern = Pattern.compile(logoRegex);
final Pattern groupPattern = Pattern.compile(groupRegex);

final String string = "\n"
+ "#EXTM3U\n"
+ "#EXTINF:O,Telegool TV Bein Sport 1 \n"
+ "http://stream99.astalavistababy.xyz/priv/b1111/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Bein Sport 2\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/bein2222/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Bein Sport 3\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/bein3333/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Bein Sport 4\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/bein4444/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Beinmax 1\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/beinmax1111/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Beinmax 2\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/beinmax2222/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Tivibu Sport 1\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/tivibu11/playlist.m3u8\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/tivibu22/playlist.m3u8\n"
+ "#EXTINF:0,Telegool TV Tivibu Sport 3\n"
+ "http://stream99.astalavistababy.xyz/selcuksportsfutbol/tivibu33/playlist.m3u8\n\n\n\n\n"
+ "#EXTM3U\n"
+ "#EXTINF:-1,-------------- | SPOR NOX | --------------\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/268.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 Hd\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/7.m3u8\n"
+ "#EXTINF:-1,beIN Sport 1 Hd [ 720p ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/16271.m3u8\n"
+ "#EXTINF:-1,BeIN Sports 1 Sd [ 60 Fps ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/16273.m3u8\n"
+ "#EXTINF:-1,beIN Sport 1 Hd [ dt ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/20279.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 Hd 1080 Hevc\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/8.m3u8\n"
+ "#EXTINF:-1,BeIN Sports 1 Hd 1080i 265.hevc\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/16272.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 Hevc [ 1920 x 1080 ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/16274.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 Hd 1080i\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/11.m3u8\n"
+ "#EXTINF:-1,BeIN Sports 1 Hd 265.hevc [ 60 Fps ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/14.m3u8\n"
+ "#EXTINF:-1,Bein Sport Sd 1 Mobile\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/15.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 Hd [ yedek ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/16.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 [ 360P ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/24.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 [ 480P ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/23.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 [ 576P ]\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/22.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 4k\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/9.m3u8\n"
+ "#EXTINF:-1,Bein Sport 1 4k 1080 Hevc\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/10.m3u8\n"
+ "#EXTINF:-1,Bein Sport 2 Sd\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/17.m3u8\n"
+ "#EXTINF:-1,Bein Sport 2 Mobile\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/18.m3u8\n"
+ "#EXTINF:-1,Bein Sport 2 Hd\n"
+ "http://nox101.com:8080/live/orkun2020/orkun2021/19.m3u8" +
"\n#EXTINF:-1 tvg-id=\"TamadonTV.af\",Tamadon TV (720p)\n" +
"#EXTINF:-1 tvg-name=\"Panorama TV \" tvg-logo=\"https://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Panorama_logo.svg/512px-Panorama_logo.svg.png\" tvg-id=\"PanoramaTV.al\" group-title=\"Albania\",Panorama TV\n" +
"#EXTINF:-1 tvg-name=\"Totò – Pluto TV\" tvg-logo=\"https://i.imgur.com/wLDteJZ.png\" group-title=\"VOD Italy\",Totò – Pluto TV\n" +
"#EXTINF:-1 tvg-name=\"Euronews Albania \" tvg-logo=\"https://i.imgur.com/Skf6vdi.png\" tvg-id=\"EuronewsAlbania.al\" group-title=\"Albania\",Euronews Albania \n" +
"#EXTINF:-1 tvg-id=\"1AlmereTV.nl\" tvg-logo=\"https://i.imgur.com/XfkbTrU.png\" group-title=\"General\",1Almere TV (720p)" +
"\n";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
//System.out.println(string);
int k = 0;
while (matcher.find()) {
++k;
System.out.println("=====================================");
// System.out.println("Full match: " + matcher.group(0));
//
// for (int i = 1; i <= matcher.groupCount(); i++) {
// System.out.println("Group " + i + ": " + matcher.group(i));
// }
//System.out.println(matcher.group());
System.out.println(matcher.group(1));
String line = matcher.group(1);
if (line != null) {
String tvgId = extractValue(line, idPattern);
String tvgName = extractValue(line, namePattern);
String tvgLogo = extractValue(line, logoPattern);
String groupTitle = extractValue(line, groupPattern);

System.out.println("tvg-id: " + (tvgId != null ? tvgId : "Not present"));
System.out.println("tvg-name: " + (tvgName != null ? tvgName : "Not present"));
System.out.println("tvg-logo: " + (tvgLogo != null ? tvgLogo : "Not present"));
System.out.println("group-title: " + (groupTitle != null ? groupTitle : "Not present"));
System.out.println("-----------------------------------");

}
}
System.out.println(k);
}

private static String extractValue(String line, Pattern pattern) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}

Комментариев нет:

Отправить комментарий