Skip to content

Commit 53d5af5

Browse files
committed
17_channel_fix_loading
2 parents c4aca21 + ebb2c8f commit 53d5af5

File tree

8 files changed

+124
-284
lines changed

8 files changed

+124
-284
lines changed

src/main/java/com/smilesmile1973/jptv/converter/ChannelConverter.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.smilesmile1973.jptv.converter;
22

3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.List;
36
import java.util.regex.Matcher;
47
import java.util.regex.Pattern;
58

@@ -10,7 +13,7 @@
1013
import com.google.common.base.Strings;
1114
import com.smilesmile1973.jptv.pojo.Channel;
1215

13-
public class ChannelConverter implements Converter<String[], Channel> {
16+
public class ChannelConverter implements Converter<List<String>, Channel> {
1417

1518
private static ChannelConverter instance;
1619

@@ -26,19 +29,23 @@ public class ChannelConverter implements Converter<String[], Channel> {
2629

2730
private static final String GR_TVLOGO = "tvLogo";
2831

29-
private static final Pattern PA_TVLOGO = Pattern.compile("tvg-logo=\"(?<tvLogo>[^\\\"]*)", Pattern.CASE_INSENSITIVE);
32+
private static final Pattern PA_TVLOGO = Pattern.compile("tvg-logo=\"(?<tvLogo>[^\\\"]*)",
33+
Pattern.CASE_INSENSITIVE);
3034

3135
private static final String GR_TVGNAME = "tvgName";
3236

33-
private static final Pattern PA_TVGNAME = Pattern.compile("tvg-name=\"(?<tvgName>[^\\\"]*)", Pattern.CASE_INSENSITIVE);
37+
private static final Pattern PA_TVGNAME = Pattern.compile("tvg-name=\"(?<tvgName>[^\\\"]*)",
38+
Pattern.CASE_INSENSITIVE);
3439

3540
private static final String GR_GROUPTITLE = "groupTitle";
3641

37-
private static final Pattern PA_GROUPTITLE = Pattern.compile("group-title=\"(?<groupTitle>[^\\\"]*)\"", Pattern.CASE_INSENSITIVE);
42+
private static final Pattern PA_GROUPTITLE = Pattern.compile("group-title=\"(?<groupTitle>[^\\\"]*)\"",
43+
Pattern.CASE_INSENSITIVE);
3844

3945
private static final String GR_GROUPTITLE2 = "groupTitle2";
4046

41-
private static final Pattern PA_GROUPTITLE2 = Pattern.compile("group-title=\".*\",(?<groupTitle2>.*)", Pattern.CASE_INSENSITIVE);
47+
private static final Pattern PA_GROUPTITLE2 = Pattern.compile("group-title=\".*\",(?<groupTitle2>.*)",
48+
Pattern.CASE_INSENSITIVE);
4249

4350
public static final ChannelConverter getInstance() {
4451
if (instance == null) {
@@ -59,7 +66,7 @@ private String fetch(String source, Pattern pattern, String groupName) {
5966
}
6067

6168
@Override
62-
public String[] toSource(Channel target) {
69+
public List<String> toSource(Channel target) {
6370
String[] results = new String[2];
6471
StringBuilder sb = new StringBuilder();
6572
sb.append("#EXTINF:").append(target.getLength()).append(" tvg-id=\"").append(target.getTvgId())
@@ -68,7 +75,7 @@ public String[] toSource(Channel target) {
6875
.append(target.getGroupTitle2());
6976
results[0] = sb.toString();
7077
results[1] = target.getChannelURL();
71-
return results;
78+
return Arrays.asList(results);
7279
}
7380

7481
/**
@@ -83,25 +90,36 @@ public String[] toSource(Channel target) {
8390
*
8491
*/
8592
@Override
86-
public Channel toTarget(String[] source) {
93+
public Channel toTarget(List<String> sources) {
8794
Channel result = new Channel();
88-
String info = source[0];
89-
String url = source[1];
90-
String tmp = "";
91-
tmp = fetch(info, PA_LENGTH, GR_LENGTH);
92-
if (!Strings.isNullOrEmpty(tmp)) {
93-
result.setLength(Long.parseLong(tmp));
95+
Iterator<String> i = sources.iterator();
96+
while (i.hasNext()) {
97+
String string = i.next();
98+
if (string.startsWith("#EXTINF")) {
99+
String tmp = "";
100+
tmp = fetch(string, PA_LENGTH, GR_LENGTH);
101+
if (!Strings.isNullOrEmpty(tmp)) {
102+
result.setLength(Long.parseLong(tmp));
103+
}
104+
result.setTvgId(fetch(string, PA_TVGID, GR_TVGID));
105+
tmp = fetch(string, PA_TVGNAME, GR_TVGNAME);
106+
if (StringUtils.isEmpty(tmp)) {
107+
LOG.debug("No name : {}", string);
108+
}
109+
result.setTvgName(tmp);
110+
result.setTvLogo(fetch(string, PA_TVLOGO, GR_TVLOGO));
111+
result.setGroupTitle(fetch(string, PA_GROUPTITLE, GR_GROUPTITLE));
112+
result.setGroupTitle2(fetch(string, PA_GROUPTITLE2, GR_GROUPTITLE2));
113+
} else if (string.startsWith("http")) {
114+
result.setChannelURL(string.trim());
115+
} else if (string.startsWith("#EXTVLCOPT:")) {
116+
String tmp = string.substring(11);
117+
LOG.info(tmp);
118+
result.setOption(tmp);
119+
} else {
120+
LOG.info("Not yet parsed : {}", string);
121+
}
94122
}
95-
result.setTvgId(fetch(info, PA_TVGID, GR_TVGID));
96-
tmp = fetch(info, PA_TVGNAME, GR_TVGNAME);
97-
if (StringUtils.isEmpty(tmp)) {
98-
LOG.debug("No name : {}", info);
99-
}
100-
result.setTvgName(tmp);
101-
result.setTvLogo(fetch(info, PA_TVLOGO, GR_TVLOGO));
102-
result.setGroupTitle(fetch(info, PA_GROUPTITLE, GR_GROUPTITLE));
103-
result.setGroupTitle2(fetch(info, PA_GROUPTITLE2, GR_GROUPTITLE2));
104-
result.setChannelURL(url.trim());
105123
return result;
106124
}
107125
}
Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.smilesmile1973.jptv.pojo;
22

3+
import java.util.Objects;
4+
35
public class Channel {
46

57
private String groupTitle;
@@ -9,31 +11,43 @@ public class Channel {
911
private String tvLogo;
1012
private String groupTitle2;
1113
private String channelURL;
14+
private String option;
1215

13-
public String getChannelURL() {
14-
return channelURL;
15-
}
16-
17-
public void setChannelURL(String channelURL) {
18-
this.channelURL = channelURL;
19-
}
20-
21-
public String getTvLogo() {
22-
return tvLogo;
16+
@Override
17+
public boolean equals(Object obj) {
18+
if (this == obj)
19+
return true;
20+
if (obj == null)
21+
return false;
22+
if (getClass() != obj.getClass())
23+
return false;
24+
Channel other = (Channel) obj;
25+
return Objects.equals(channelURL, other.channelURL) && Objects.equals(groupTitle, other.groupTitle)
26+
&& Objects.equals(groupTitle2, other.groupTitle2) && length == other.length
27+
&& Objects.equals(option, other.option) && Objects.equals(tvLogo, other.tvLogo)
28+
&& Objects.equals(tvgId, other.tvgId) && Objects.equals(tvgName, other.tvgName);
2329
}
2430

25-
public void setTvLogo(String tvLogo) {
26-
this.tvLogo = tvLogo;
31+
public String getChannelURL() {
32+
return channelURL;
2733
}
2834

2935
public String getGroupTitle() {
3036
return groupTitle;
3137
}
3238

39+
public String getGroupTitle2() {
40+
return groupTitle2;
41+
}
42+
3343
public long getLength() {
3444
return length;
3545
}
3646

47+
public String getOption() {
48+
return option;
49+
}
50+
3751
public String getTvgId() {
3852
return tvgId;
3953
}
@@ -42,70 +56,33 @@ public String getTvgName() {
4256
return tvgName;
4357
}
4458

59+
public String getTvLogo() {
60+
return tvLogo;
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(channelURL, groupTitle, groupTitle2, length, option, tvLogo, tvgId, tvgName);
66+
}
67+
68+
public void setChannelURL(String channelURL) {
69+
this.channelURL = channelURL;
70+
}
71+
4572
public void setGroupTitle(String groupTitle) {
4673
this.groupTitle = groupTitle;
4774
}
4875

49-
public void setLength(long setLength) {
50-
this.length = setLength;
76+
public void setGroupTitle2(String groupTitle2) {
77+
this.groupTitle2 = groupTitle2;
5178
}
5279

53-
@Override
54-
public int hashCode() {
55-
final int prime = 31;
56-
int result = 1;
57-
result = prime * result + ((channelURL == null) ? 0 : channelURL.hashCode());
58-
result = prime * result + ((groupTitle == null) ? 0 : groupTitle.hashCode());
59-
result = prime * result + ((groupTitle2 == null) ? 0 : groupTitle2.hashCode());
60-
result = prime * result + (int) (length ^ (length >>> 32));
61-
result = prime * result + ((tvLogo == null) ? 0 : tvLogo.hashCode());
62-
result = prime * result + ((tvgId == null) ? 0 : tvgId.hashCode());
63-
result = prime * result + ((tvgName == null) ? 0 : tvgName.hashCode());
64-
return result;
80+
public void setLength(long setLength) {
81+
this.length = setLength;
6582
}
6683

67-
@Override
68-
public boolean equals(Object obj) {
69-
if (this == obj)
70-
return true;
71-
if (obj == null)
72-
return false;
73-
if (getClass() != obj.getClass())
74-
return false;
75-
Channel other = (Channel) obj;
76-
if (channelURL == null) {
77-
if (other.channelURL != null)
78-
return false;
79-
} else if (!channelURL.equals(other.channelURL))
80-
return false;
81-
if (groupTitle == null) {
82-
if (other.groupTitle != null)
83-
return false;
84-
} else if (!groupTitle.equals(other.groupTitle))
85-
return false;
86-
if (groupTitle2 == null) {
87-
if (other.groupTitle2 != null)
88-
return false;
89-
} else if (!groupTitle2.equals(other.groupTitle2))
90-
return false;
91-
if (length != other.length)
92-
return false;
93-
if (tvLogo == null) {
94-
if (other.tvLogo != null)
95-
return false;
96-
} else if (!tvLogo.equals(other.tvLogo))
97-
return false;
98-
if (tvgId == null) {
99-
if (other.tvgId != null)
100-
return false;
101-
} else if (!tvgId.equals(other.tvgId))
102-
return false;
103-
if (tvgName == null) {
104-
if (other.tvgName != null)
105-
return false;
106-
} else if (!tvgName.equals(other.tvgName))
107-
return false;
108-
return true;
84+
public void setOption(String option) {
85+
this.option = option;
10986
}
11087

11188
public void setTvgId(String tvgId) {
@@ -116,11 +93,14 @@ public void setTvgName(String tvgName) {
11693
this.tvgName = tvgName;
11794
}
11895

119-
public void setGroupTitle2(String groupTitle2) {
120-
this.groupTitle2 = groupTitle2;
96+
public void setTvLogo(String tvLogo) {
97+
this.tvLogo = tvLogo;
12198
}
12299

123-
public String getGroupTitle2() {
124-
return groupTitle2;
100+
@Override
101+
public String toString() {
102+
return "Channel [groupTitle=" + groupTitle + ", length=" + length + ", tvgId=" + tvgId + ", tvgName=" + tvgName
103+
+ ", tvLogo=" + tvLogo + ", groupTitle2=" + groupTitle2 + ", channelURL=" + channelURL + ", option="
104+
+ option + "]";
125105
}
126106
}

src/main/java/com/smilesmile1973/jptv/service/M3UService.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.URL;
99
import java.util.ArrayList;
1010
import java.util.Comparator;
11+
import java.util.Iterator;
1112
import java.util.List;
1213
import java.util.Map;
1314
import java.util.TreeMap;
@@ -40,16 +41,23 @@ private M3UService() {
4041
public Map<String, List<Channel>> buildChannels(String url) throws Exception {
4142
channels.clear();
4243
List<String> strings = fetchWebSite(url);
43-
String[] sources = new String[2];
44-
if (strings != null && !strings.isEmpty() && strings.get(0).equals("#EXTM3U")) {
45-
for (int i = 1; i < strings.size()-2; i = i + 2) {
46-
sources[0] = strings.get(i);
47-
sources[1] = strings.get(i + 1);
44+
Iterator<String> i = strings.iterator();
45+
List<String> sources = new ArrayList<>();
46+
while (i.hasNext()) {
47+
String string = i.next();
48+
if (string.startsWith("#EXTM3U")) {
49+
LOG.info("Header #EXTM3U");
50+
}
51+
if (!string.startsWith("http")) {
52+
sources.add(string);
53+
} else {
54+
sources.add(string);
4855
Channel channel = ChannelConverter.getInstance().toTarget(sources);
4956
if (channels.get(channel.getGroupTitle()) == null) {
50-
channels.put(channel.getGroupTitle(), new ArrayList<Channel>());
57+
channels.put(channel.getGroupTitle(), new ArrayList<>());
5158
}
5259
channels.get(channel.getGroupTitle()).add(channel);
60+
sources = new ArrayList<>();
5361
}
5462
}
5563
return channels;
@@ -87,9 +95,9 @@ public Channel getFirst() {
8795
}
8896

8997
public List<Channel> sortGroup(String group) {
90-
List<Channel> channels = this.channels.get(group);
91-
channels.sort(Comparator.comparing(Channel::getTvgName));
92-
return channels;
98+
List<Channel> tmpChannels = this.channels.get(group);
99+
tmpChannels.sort(Comparator.comparing(Channel::getTvgName));
100+
return tmpChannels;
93101
}
94102

95103
}

src/main/java/com/smilesmile1973/jptv/view/Main.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Set;
88

9+
import org.apache.commons.lang3.StringUtils;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -83,7 +84,12 @@ public void buildCenter(ChannelListCreatedEvent event) {
8384
splitPane.setDividerPosition(0, 0);
8485
root.setCenter(splitPane);
8586
splitPane.setOnMouseMoved(eventMouse -> hideOrShowChannelList(splitPane, eventMouse));
86-
embeddedMediaPlayer.media().play(M3UService.getInstance().getFirst().getChannelURL());
87+
Channel channel = M3UService.getInstance().getFirst();
88+
if (StringUtils.isBlank(channel.getOption())) {
89+
embeddedMediaPlayer.media().play(channel.getChannelURL());
90+
} else {
91+
embeddedMediaPlayer.media().play(channel.getChannelURL(), channel.getOption());
92+
}
8793
}
8894
}
8995

@@ -160,12 +166,13 @@ private Node buildTopPane(Window owner) {
160166

161167
@Subscribe
162168
public void changeChannel(EventChannel eventChannel) {
163-
String channelUrl = eventChannel.getChannel().getChannelURL();
164-
LOG.debug("Change channel to {}:", channelUrl);
169+
Channel channel = eventChannel.getChannel();
170+
LOG.debug("Change channel to {}:", channel.getChannelURL());
165171
PixelBufferInstance.getInstance().setDisplayed(false);
166-
boolean result = embeddedMediaPlayer.media().play(channelUrl);
167-
if (!result) {
168-
LOG.error("The url {} can not be played.", channelUrl);
172+
if (StringUtils.isBlank(channel.getOption())) {
173+
embeddedMediaPlayer.media().play(channel.getChannelURL());
174+
} else {
175+
embeddedMediaPlayer.media().play(channel.getChannelURL(), channel.getOption());
169176
}
170177
}
171178

0 commit comments

Comments
 (0)