Skip to content

Commit

Permalink
17_channel_fix_loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald1973 committed Aug 29, 2021
2 parents c4aca21 + ebb2c8f commit 53d5af5
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 284 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.smilesmile1973.jptv.converter;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -10,7 +13,7 @@
import com.google.common.base.Strings;
import com.smilesmile1973.jptv.pojo.Channel;

public class ChannelConverter implements Converter<String[], Channel> {
public class ChannelConverter implements Converter<List<String>, Channel> {

private static ChannelConverter instance;

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

private static final String GR_TVLOGO = "tvLogo";

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

private static final String GR_TVGNAME = "tvgName";

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

private static final String GR_GROUPTITLE = "groupTitle";

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

private static final String GR_GROUPTITLE2 = "groupTitle2";

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

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

@Override
public String[] toSource(Channel target) {
public List<String> toSource(Channel target) {
String[] results = new String[2];
StringBuilder sb = new StringBuilder();
sb.append("#EXTINF:").append(target.getLength()).append(" tvg-id=\"").append(target.getTvgId())
Expand All @@ -68,7 +75,7 @@ public String[] toSource(Channel target) {
.append(target.getGroupTitle2());
results[0] = sb.toString();
results[1] = target.getChannelURL();
return results;
return Arrays.asList(results);
}

/**
Expand All @@ -83,25 +90,36 @@ public String[] toSource(Channel target) {
*
*/
@Override
public Channel toTarget(String[] source) {
public Channel toTarget(List<String> sources) {
Channel result = new Channel();
String info = source[0];
String url = source[1];
String tmp = "";
tmp = fetch(info, PA_LENGTH, GR_LENGTH);
if (!Strings.isNullOrEmpty(tmp)) {
result.setLength(Long.parseLong(tmp));
Iterator<String> i = sources.iterator();
while (i.hasNext()) {
String string = i.next();
if (string.startsWith("#EXTINF")) {
String tmp = "";
tmp = fetch(string, PA_LENGTH, GR_LENGTH);
if (!Strings.isNullOrEmpty(tmp)) {
result.setLength(Long.parseLong(tmp));
}
result.setTvgId(fetch(string, PA_TVGID, GR_TVGID));
tmp = fetch(string, PA_TVGNAME, GR_TVGNAME);
if (StringUtils.isEmpty(tmp)) {
LOG.debug("No name : {}", string);
}
result.setTvgName(tmp);
result.setTvLogo(fetch(string, PA_TVLOGO, GR_TVLOGO));
result.setGroupTitle(fetch(string, PA_GROUPTITLE, GR_GROUPTITLE));
result.setGroupTitle2(fetch(string, PA_GROUPTITLE2, GR_GROUPTITLE2));
} else if (string.startsWith("http")) {
result.setChannelURL(string.trim());
} else if (string.startsWith("#EXTVLCOPT:")) {
String tmp = string.substring(11);
LOG.info(tmp);
result.setOption(tmp);
} else {
LOG.info("Not yet parsed : {}", string);
}
}
result.setTvgId(fetch(info, PA_TVGID, GR_TVGID));
tmp = fetch(info, PA_TVGNAME, GR_TVGNAME);
if (StringUtils.isEmpty(tmp)) {
LOG.debug("No name : {}", info);
}
result.setTvgName(tmp);
result.setTvLogo(fetch(info, PA_TVLOGO, GR_TVLOGO));
result.setGroupTitle(fetch(info, PA_GROUPTITLE, GR_GROUPTITLE));
result.setGroupTitle2(fetch(info, PA_GROUPTITLE2, GR_GROUPTITLE2));
result.setChannelURL(url.trim());
return result;
}
}
124 changes: 52 additions & 72 deletions src/main/java/com/smilesmile1973/jptv/pojo/Channel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.smilesmile1973.jptv.pojo;

import java.util.Objects;

public class Channel {

private String groupTitle;
Expand All @@ -9,31 +11,43 @@ public class Channel {
private String tvLogo;
private String groupTitle2;
private String channelURL;
private String option;

public String getChannelURL() {
return channelURL;
}

public void setChannelURL(String channelURL) {
this.channelURL = channelURL;
}

public String getTvLogo() {
return tvLogo;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Channel other = (Channel) obj;
return Objects.equals(channelURL, other.channelURL) && Objects.equals(groupTitle, other.groupTitle)
&& Objects.equals(groupTitle2, other.groupTitle2) && length == other.length
&& Objects.equals(option, other.option) && Objects.equals(tvLogo, other.tvLogo)
&& Objects.equals(tvgId, other.tvgId) && Objects.equals(tvgName, other.tvgName);
}

public void setTvLogo(String tvLogo) {
this.tvLogo = tvLogo;
public String getChannelURL() {
return channelURL;
}

public String getGroupTitle() {
return groupTitle;
}

public String getGroupTitle2() {
return groupTitle2;
}

public long getLength() {
return length;
}

public String getOption() {
return option;
}

public String getTvgId() {
return tvgId;
}
Expand All @@ -42,70 +56,33 @@ public String getTvgName() {
return tvgName;
}

public String getTvLogo() {
return tvLogo;
}

@Override
public int hashCode() {
return Objects.hash(channelURL, groupTitle, groupTitle2, length, option, tvLogo, tvgId, tvgName);
}

public void setChannelURL(String channelURL) {
this.channelURL = channelURL;
}

public void setGroupTitle(String groupTitle) {
this.groupTitle = groupTitle;
}

public void setLength(long setLength) {
this.length = setLength;
public void setGroupTitle2(String groupTitle2) {
this.groupTitle2 = groupTitle2;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((channelURL == null) ? 0 : channelURL.hashCode());
result = prime * result + ((groupTitle == null) ? 0 : groupTitle.hashCode());
result = prime * result + ((groupTitle2 == null) ? 0 : groupTitle2.hashCode());
result = prime * result + (int) (length ^ (length >>> 32));
result = prime * result + ((tvLogo == null) ? 0 : tvLogo.hashCode());
result = prime * result + ((tvgId == null) ? 0 : tvgId.hashCode());
result = prime * result + ((tvgName == null) ? 0 : tvgName.hashCode());
return result;
public void setLength(long setLength) {
this.length = setLength;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Channel other = (Channel) obj;
if (channelURL == null) {
if (other.channelURL != null)
return false;
} else if (!channelURL.equals(other.channelURL))
return false;
if (groupTitle == null) {
if (other.groupTitle != null)
return false;
} else if (!groupTitle.equals(other.groupTitle))
return false;
if (groupTitle2 == null) {
if (other.groupTitle2 != null)
return false;
} else if (!groupTitle2.equals(other.groupTitle2))
return false;
if (length != other.length)
return false;
if (tvLogo == null) {
if (other.tvLogo != null)
return false;
} else if (!tvLogo.equals(other.tvLogo))
return false;
if (tvgId == null) {
if (other.tvgId != null)
return false;
} else if (!tvgId.equals(other.tvgId))
return false;
if (tvgName == null) {
if (other.tvgName != null)
return false;
} else if (!tvgName.equals(other.tvgName))
return false;
return true;
public void setOption(String option) {
this.option = option;
}

public void setTvgId(String tvgId) {
Expand All @@ -116,11 +93,14 @@ public void setTvgName(String tvgName) {
this.tvgName = tvgName;
}

public void setGroupTitle2(String groupTitle2) {
this.groupTitle2 = groupTitle2;
public void setTvLogo(String tvLogo) {
this.tvLogo = tvLogo;
}

public String getGroupTitle2() {
return groupTitle2;
@Override
public String toString() {
return "Channel [groupTitle=" + groupTitle + ", length=" + length + ", tvgId=" + tvgId + ", tvgName=" + tvgName
+ ", tvLogo=" + tvLogo + ", groupTitle2=" + groupTitle2 + ", channelURL=" + channelURL + ", option="
+ option + "]";
}
}
26 changes: 17 additions & 9 deletions src/main/java/com/smilesmile1973/jptv/service/M3UService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -40,16 +41,23 @@ private M3UService() {
public Map<String, List<Channel>> buildChannels(String url) throws Exception {
channels.clear();
List<String> strings = fetchWebSite(url);
String[] sources = new String[2];
if (strings != null && !strings.isEmpty() && strings.get(0).equals("#EXTM3U")) {
for (int i = 1; i < strings.size()-2; i = i + 2) {
sources[0] = strings.get(i);
sources[1] = strings.get(i + 1);
Iterator<String> i = strings.iterator();
List<String> sources = new ArrayList<>();
while (i.hasNext()) {
String string = i.next();
if (string.startsWith("#EXTM3U")) {
LOG.info("Header #EXTM3U");
}
if (!string.startsWith("http")) {
sources.add(string);
} else {
sources.add(string);
Channel channel = ChannelConverter.getInstance().toTarget(sources);
if (channels.get(channel.getGroupTitle()) == null) {
channels.put(channel.getGroupTitle(), new ArrayList<Channel>());
channels.put(channel.getGroupTitle(), new ArrayList<>());
}
channels.get(channel.getGroupTitle()).add(channel);
sources = new ArrayList<>();
}
}
return channels;
Expand Down Expand Up @@ -87,9 +95,9 @@ public Channel getFirst() {
}

public List<Channel> sortGroup(String group) {
List<Channel> channels = this.channels.get(group);
channels.sort(Comparator.comparing(Channel::getTvgName));
return channels;
List<Channel> tmpChannels = this.channels.get(group);
tmpChannels.sort(Comparator.comparing(Channel::getTvgName));
return tmpChannels;
}

}
19 changes: 13 additions & 6 deletions src/main/java/com/smilesmile1973/jptv/view/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -83,7 +84,12 @@ public void buildCenter(ChannelListCreatedEvent event) {
splitPane.setDividerPosition(0, 0);
root.setCenter(splitPane);
splitPane.setOnMouseMoved(eventMouse -> hideOrShowChannelList(splitPane, eventMouse));
embeddedMediaPlayer.media().play(M3UService.getInstance().getFirst().getChannelURL());
Channel channel = M3UService.getInstance().getFirst();
if (StringUtils.isBlank(channel.getOption())) {
embeddedMediaPlayer.media().play(channel.getChannelURL());
} else {
embeddedMediaPlayer.media().play(channel.getChannelURL(), channel.getOption());
}
}
}

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

@Subscribe
public void changeChannel(EventChannel eventChannel) {
String channelUrl = eventChannel.getChannel().getChannelURL();
LOG.debug("Change channel to {}:", channelUrl);
Channel channel = eventChannel.getChannel();
LOG.debug("Change channel to {}:", channel.getChannelURL());
PixelBufferInstance.getInstance().setDisplayed(false);
boolean result = embeddedMediaPlayer.media().play(channelUrl);
if (!result) {
LOG.error("The url {} can not be played.", channelUrl);
if (StringUtils.isBlank(channel.getOption())) {
embeddedMediaPlayer.media().play(channel.getChannelURL());
} else {
embeddedMediaPlayer.media().play(channel.getChannelURL(), channel.getOption());
}
}

Expand Down
Loading

0 comments on commit 53d5af5

Please sign in to comment.