From: Jan-Pascal van Best Date: Tue, 24 Sep 2013 20:04:12 +0000 (+0200) Subject: Updated config file format: Horizon now uses friendly channel id instead of 64 bit... X-Git-Tag: 1.4.0~3 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=17182621c35163b3bc6b7adefde30146cba78fab;p=tv_grab_nl_java Updated config file format: Horizon now uses friendly channel id instead of 64 bit numbers --- diff --git a/src/main/java/org/vanbest/xmltv/Channel.java b/src/main/java/org/vanbest/xmltv/Channel.java index 68e600a..6e68b8e 100644 --- a/src/main/java/org/vanbest/xmltv/Channel.java +++ b/src/main/java/org/vanbest/xmltv/Channel.java @@ -1,9 +1,11 @@ package org.vanbest.xmltv; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import org.apache.log4j.Logger; public class Channel { String id; @@ -12,6 +14,7 @@ public class Channel { List urls; protected boolean enabled = true; int source; + static Logger logger = Logger.getLogger(Channel.class); protected Channel(int source, String id) { this.id = id; @@ -26,17 +29,26 @@ public class Channel { } static Channel getChannel(int source, String id, String name) { - Channel c = new Channel(source, id); + Channel c; + if(EPGSourceFactory.getChannelSourceName(source).equals(Horizon.NAME)) { + long horizonId=Long.parseLong(id); + c = new Horizon.HorizonChannel(source, id, horizonId); + } else { + c = new Channel(source, id); + } c.names.add(name); return c; } - static Channel getChannel(int source, String id, String name, String iconUrl) { - Channel c = new Channel(source, id); - c.names.add(name); - if (iconUrl != null) { - c.icons.add(new Icon(iconUrl)); + static Channel getChannel(int source, String id, String name, String extraConfig) { + Channel c; + if(EPGSourceFactory.getChannelSourceName(source).equals(Horizon.NAME)) { + long horizonId=Long.parseLong(extraConfig); + c = new Horizon.HorizonChannel(source, id, horizonId); + } else { + c = new Channel(source, id); } + c.names.add(name); return c; } @@ -80,6 +92,89 @@ public class Channel { this.enabled = b; } + public String extraConfig() { + return ""; + } + + // must start with channel: + public void writeConfig(PrintWriter out) { + // FIXME: handle multiple channels names, icons and urls + out.print("channel: " + getSourceName() + ": " + + id + ": " + + (enabled ? "enabled" : "disabled") + ": " + + Config.escape(defaultName()) + ": " + + Config.escape(extraConfig())); + if (!icons.isEmpty()) { + out.print(" : " + Config.escape(icons.get(0).url)); + } + out.println(); + } + + public static Channel parseConfig(int fileformat, List parts) { + Channel c = null; + switch (fileformat) { + case 0: + c = Channel.getChannel(EPGSourceFactory.newInstance() + .getChannelSourceId("tvgids.nl"), parts.get(1), + parts.get(2)); + if (parts.size() > 3) { + c.addIcon(parts.get(3)); + } + break; + case 1: + c = Channel.getChannel(EPGSourceFactory.newInstance() + .getChannelSourceId("tvgids.nl"), parts.get(1), + parts.get(3)); + if (parts.size() > 4) { + c.addIcon(parts.get(4)); + } + String value = parts.get(2); + if (value.equals("enabled")) { + c.setEnabled(true); + } else if (value.equals("disabled")) { + c.setEnabled(false); + } else { + logger.error("Error in config file, unknown channel status \"" + + parts.get(2) + + "\", should be enabled or disabled"); + } + break; + case 2: + case 3: + case 4: + case 5: + int source; + if (fileformat == 2) { + source = Integer.parseInt(parts.get(1)); + } else { + source = EPGSourceFactory.newInstance() + .getChannelSourceId(parts.get(1)); + } + if(fileformat<5) { + c = Channel.getChannel(source, parts.get(2), + parts.get(4)); + } else { + c = Channel.getChannel(source, parts.get(2), + parts.get(4), parts.get(5)); + } + int iconPart = (fileformat<5?5:6); + if (parts.size() > iconPart) { + c.addIcon(parts.get(iconPart)); + } + value = parts.get(3); + if (value.equals("enabled")) { + c.setEnabled(true); + } else if (value.equals("disabled")) { + c.setEnabled(false); + } else { + logger.error("Error in config file, unknown channel status \"" + + parts.get(3) + + "\", should be enabled or disabled"); + } + } + return c; + } + public String toString() { return "Channel " + source + "::" + id + " (" + defaultName() + ")"; } diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java index 1bbd9eb..0ecb9a3 100644 --- a/src/main/java/org/vanbest/xmltv/Config.java +++ b/src/main/java/org/vanbest/xmltv/Config.java @@ -36,7 +36,7 @@ import org.apache.log4j.Logger; public class Config { // constants - private final static int CURRENT_FILE_FORMAT = 4; + private final static int CURRENT_FILE_FORMAT = 5; // in config file public int niceMilliseconds; @@ -164,14 +164,7 @@ public class Config { out.println("fetch-channel-logos: " + (fetchLogos?"yes":"no")); out.println("nice-time-milliseconds: " + niceMilliseconds); for (Channel c : channels) { - // FIXME: handle multiple channels names, icons and urls - out.print("channel: " + c.getSourceName() + ": " + c.id + ": " - + (c.enabled ? "enabled" : "disabled") + ": " - + escape(c.defaultName())); - if (!c.icons.isEmpty()) { - out.print(" : " + escape(c.icons.get(0).url)); - } - out.println(); + c.writeConfig(out); } for (Map.Entry entry : cattrans.entrySet()) { out.println("category: " + escape(entry.getKey()) + ": " @@ -244,62 +237,9 @@ public class Config { List parts = splitLine(s); String key = parts.get(0).toLowerCase(); if (key.equals("channel")) { - Channel c = null; // System.out.println("Adding channel " + parts + // " in file format " + fileformat); - switch (fileformat) { - case 0: - c = Channel.getChannel(EPGSourceFactory.newInstance() - .getChannelSourceId("tvgids.nl"), parts.get(1), - parts.get(2)); - if (parts.size() > 3) { - c.addIcon(parts.get(3)); - } - break; - case 1: - c = Channel.getChannel(EPGSourceFactory.newInstance() - .getChannelSourceId("tvgids.nl"), parts.get(1), - parts.get(3)); - if (parts.size() > 4) { - c.addIcon(parts.get(4)); - } - String value = parts.get(2); - if (value.equals("enabled")) { - c.setEnabled(true); - } else if (value.equals("disabled")) { - c.setEnabled(false); - } else { - logger.error("Error in config file, unknown channel status \"" - + parts.get(2) - + "\", should be enabled or disabled"); - } - break; - case 2: - case 3: - case 4: - int source; - if (fileformat == 2) { - source = Integer.parseInt(parts.get(1)); - } else { - source = EPGSourceFactory.newInstance() - .getChannelSourceId(parts.get(1)); - } - c = Channel.getChannel(source, parts.get(2), - parts.get(4)); - if (parts.size() > 5) { - c.addIcon(parts.get(5)); - } - value = parts.get(3); - if (value.equals("enabled")) { - c.setEnabled(true); - } else if (value.equals("disabled")) { - c.setEnabled(false); - } else { - logger.error("Error in config file, unknown channel status \"" - + parts.get(3) - + "\", should be enabled or disabled"); - } - } + Channel c = Channel.parseConfig(fileformat, parts); result.channels.add(c); } else if (key.equals("category")) { result.cattrans.put(parts.get(1), parts.get(2)); diff --git a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java index c07f035..e90d076 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java +++ b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java @@ -81,11 +81,13 @@ public class EPGSourceFactory { return createEPGSource(sourceId, config); } - public String getChannelSourceName(int id) { + public static String getChannelSourceName(int id) { + init(); return names.get(id); } - public int getChannelSourceId(String name) { + public static int getChannelSourceId(String name) { + init(); return ids.get(name); } diff --git a/src/main/java/org/vanbest/xmltv/Horizon.java b/src/main/java/org/vanbest/xmltv/Horizon.java index 149d406..7a92e7c 100644 --- a/src/main/java/org/vanbest/xmltv/Horizon.java +++ b/src/main/java/org/vanbest/xmltv/Horizon.java @@ -45,9 +45,31 @@ import net.sf.json.JSONObject; public class Horizon extends AbstractEPGSource implements EPGSource { + static class HorizonChannel extends Channel { + long horizonId; + protected HorizonChannel(int source, String id, long horizonId) { + super(source,id); + this.horizonId = horizonId; + } + protected HorizonChannel(int source, long horizonId, String name) { + super(source,sanitise(name)); + this.horizonId = horizonId; + } + public String getXmltvChannelId() { + return sanitise(defaultName()) + "." + getSourceName(); + } + public static String sanitise(String name) { + return name.replaceAll("[^a-zA-Z0-9]",""); + } + @Override + public String extraConfig() { + return Long.toString(horizonId); + } + + } + static String channels_url = "https://www.horizon.tv/oesp/api/NL/nld/web/channels/"; static String listings_url = "https://www.horizon.tv/oesp/api/NL/nld/web/listings"; - // ?byStationId=28070126&sort=startTime&range=1-100&byStartTime=1362000000000~1362100000000"; public static String NAME = "horizon.tv"; @@ -61,11 +83,12 @@ public class Horizon extends AbstractEPGSource implements EPGSource { return NAME; } - public static URL programmeUrl(Channel channel, int day) + public static URL programmeUrl(Channel c, int day) throws Exception { + HorizonChannel channel = (HorizonChannel) c; StringBuilder s = new StringBuilder(listings_url); s.append("?byStationId="); - s.append(channel.id); + s.append(channel.horizonId); s.append("&sort=startTime&range=1-100"); Calendar startTime=Calendar.getInstance(); startTime.set(Calendar.HOUR_OF_DAY, 0); @@ -120,7 +143,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource { JSONObject firstSchedule = stationSchedules.getJSONObject(0); JSONObject station = firstSchedule.getJSONObject("station"); logger.debug("firstschedule: " + firstSchedule.toString()); - long id = station.getLong("id"); + long horizonId = station.getLong("id"); String name = station.getString("title"); // Use the largest available station logo as icon JSONArray images = station.getJSONArray("images"); @@ -134,8 +157,10 @@ public class Horizon extends AbstractEPGSource implements EPGSource { maxSize = image.getInt("width"); } } - Channel c = Channel.getChannel(getId(), Long.toString(id), name, - icon); + String id = HorizonChannel.sanitise(name); + Channel c = Channel.getChannel(getId(), id, name, Long.toString(horizonId)); + //Channel c = new HorizonChannel(getId(), horizonId, name); + c.addIcon(icon); result.add(c); } diff --git a/src/main/java/org/vanbest/xmltv/RTL.java b/src/main/java/org/vanbest/xmltv/RTL.java index ed1e09b..b892436 100644 --- a/src/main/java/org/vanbest/xmltv/RTL.java +++ b/src/main/java/org/vanbest/xmltv/RTL.java @@ -167,7 +167,8 @@ public class RTL extends AbstractEPGSource implements EPGSource { String id = genericChannelId(k.toString()); String name = (String) j.get(0); String icon = icon_url + id + ".gif"; - Channel c = Channel.getChannel(getId(), id, name, icon); + Channel c = Channel.getChannel(getId(), id, name); + c.addIcon(icon); result.add(c); } diff --git a/src/main/java/org/vanbest/xmltv/TvGids.java b/src/main/java/org/vanbest/xmltv/TvGids.java index 0c7a6a5..92307da 100644 --- a/src/main/java/org/vanbest/xmltv/TvGids.java +++ b/src/main/java/org/vanbest/xmltv/TvGids.java @@ -142,8 +142,8 @@ public class TvGids extends AbstractEPGSource implements EPGSource { .unescapeHtml(zender.getString("name")); String icon = "http://tvgidsassets.nl/img/channels/53x27/" + id + ".png"; - Channel c = Channel.getChannel(getId(), Integer.toString(id), name, - icon); + Channel c = Channel.getChannel(getId(), Integer.toString(id), name); + c.addIcon(icon); result.add(c); }