From: Jan-Pascal van Best Date: Thu, 16 Jan 2014 20:58:51 +0000 (+0100) Subject: Refactor: use source name as key for EPGSource X-Git-Tag: v1.6.0~1 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=fdb0406180e7f47d24883b642c48e5dfb96289ed;p=tv_grab_nl_java Refactor: use source name as key for EPGSource --- diff --git a/pom.xml b/pom.xml index 4ca9bde..956f852 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,11 @@ httpclient 4.3.1 + + org.reflections + reflections + 0.9.9-RC1 + diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java index 804e2a6..a0d418f 100644 --- a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java +++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java @@ -29,7 +29,6 @@ import org.apache.log4j.Logger; public abstract class AbstractEPGSource implements EPGSource { - private int sourceId; protected Config config; protected ProgrammeCache cache; protected Stats stats = new Stats(); @@ -37,20 +36,11 @@ public abstract class AbstractEPGSource implements EPGSource { public static final int MAX_FETCH_TRIES = 5; - public AbstractEPGSource(int sourceId, Config config) { + public AbstractEPGSource(Config config) { this.config = config; - this.sourceId = sourceId; cache = new ProgrammeCache(config); } - public int getId() { - return sourceId; - } - - public void setId(int id) { - sourceId = id; - } - public List getProgrammes(Channel channel, int day) throws Exception { ArrayList list = new ArrayList(2); @@ -103,7 +93,7 @@ public abstract class AbstractEPGSource implements EPGSource { } public void clearCache() { - cache.clear(sourceId); + cache.clear(getName()); } String kijkwijzerCategorie(char c) { diff --git a/src/main/java/org/vanbest/xmltv/Channel.java b/src/main/java/org/vanbest/xmltv/Channel.java index 211cc16..d824e83 100644 --- a/src/main/java/org/vanbest/xmltv/Channel.java +++ b/src/main/java/org/vanbest/xmltv/Channel.java @@ -1,217 +1,211 @@ -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; // Internal id, used by EPG provider - String xmltv; // xmltv id such as '1.tvgids.nl' or 'Nederland1.horizon.tv' - List names; // at least one name is obligatory - List icons; - List urls; - protected boolean enabled = true; - int source; - static Logger logger = Logger.getLogger(Channel.class); - - protected Channel(int source, String id, String xmltv) { - this.id = id; - this.source = source; - this.xmltv = xmltv; - names = new ArrayList(); - icons = new ArrayList(); - urls = new ArrayList(); - } - - public String defaultName() { - return names.get(0); - } - - static Channel getChannel(int source, String id, String xmltv, String name) { - Channel c = new Channel(source, id, xmltv); - c.names.add(name); - return c; - } - - // Use default xmltvid with id+"."+sourceName - static Channel getChannel(int source, String id, String name) { - String xmltv = id + "." + EPGSourceFactory.getChannelSourceName(source); - Channel c = new Channel(source, id, xmltv); - c.names.add(name); - return c; - } -/* - 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; - } -*/ - public String getXmltvChannelId() { - return xmltv; // id + "." + getSourceName(); - } - - public String getSourceName() { - return EPGSourceFactory.getChannelSourceName(source); - } - - public void serialize(XMLStreamWriter writer, boolean writeLogos) throws XMLStreamException { - writer.writeStartElement("channel"); - writer.writeAttribute("id", getXmltvChannelId()); - for (String name : names) { - writer.writeStartElement("display-name"); - writer.writeAttribute("lang", "nl"); - writer.writeCharacters(name); - writer.writeEndElement(); - } - if (writeLogos) { - for (Icon i : icons) { - i.serialize(writer); - } - } - for (String url : urls) { - writer.writeStartElement("url"); - writer.writeCharacters(url); - writer.writeEndElement(); - } - writer.writeEndElement(); - writer.writeCharacters(System.getProperty("line.separator")); - } - - // Convenience method - public void addIcon(String url) { - icons.add(new Icon(url)); - } - - public void setEnabled(boolean b) { - 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() + ": " - + Config.escape(xmltv) + ": " - + 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)); - } - String id = parts.get(2); - String enabled = parts.get(3); - String name = parts.get(4); - if(fileformat<5) { - c = Channel.getChannel(source, id, name); - } else { - String extra = parts.get(5); - if(extra.isEmpty()) { - c = Channel.getChannel(source, id, name); - } else { - // Horizon channel - String xmltv=id+"."+EPGSourceFactory.getChannelSourceName(source); - String horizonId=extra; - c = Channel.getChannel(source, horizonId, xmltv, name); - } - } - int iconPart = (fileformat<5?5:6); - if (parts.size() > iconPart) { - c.addIcon(parts.get(iconPart)); - } - if (enabled.equals("enabled")) { - c.setEnabled(true); - } else if (enabled.equals("disabled")) { - c.setEnabled(false); - } else { - logger.error("Error in config file, unknown channel status \"" - + enabled - + "\", should be enabled or disabled"); - } - break; - } - case 6: { - int source = EPGSourceFactory.getChannelSourceId(parts.get(1)); - String xmltv = parts.get(2); - String id = parts.get(3); - String enabled = parts.get(4); - String name = parts.get(5); - String extra = parts.get(6); - c = Channel.getChannel(source, id, xmltv, name); - if (parts.size() > 7) { - c.addIcon(parts.get(7)); - } - if (enabled.equals("enabled")) { - c.setEnabled(true); - } else if (enabled.equals("disabled")) { - c.setEnabled(false); - } else { - logger.error("Error in config file, unknown channel status \"" - + enabled - + "\", should be enabled or disabled"); - } - } - } - return c; - } - - public String toString() { - return "Channel " + source + "::" + id + " (" + defaultName() + ")"; - } -} +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; // Internal id, used by EPG provider + String xmltv; // xmltv id such as '1.tvgids.nl' or 'Nederland1.horizon.tv' + List names; // at least one name is obligatory + List icons; + List urls; + protected boolean enabled = true; + String source; + static Logger logger = Logger.getLogger(Channel.class); + + protected Channel(String source, String id, String xmltv) { + this.id = id; + this.source = source; + this.xmltv = xmltv; + names = new ArrayList(); + icons = new ArrayList(); + urls = new ArrayList(); + } + + public String defaultName() { + return names.get(0); + } + + static Channel getChannel(String source, String id, String xmltv, String name) { + Channel c = new Channel(source, id, xmltv); + c.names.add(name); + return c; + } + + // Use default xmltvid with id+"."+sourceName + static Channel getChannel(String source, String id, String name) { + String xmltv = id + "." + source; + Channel c = new Channel(source, id, xmltv); + c.names.add(name); + return c; + } +/* + 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; + } +*/ + public String getXmltvChannelId() { + return xmltv; // id + "." + getSourceName(); + } + + public String getSourceName() { + return source; + } + + public void serialize(XMLStreamWriter writer, boolean writeLogos) throws XMLStreamException { + writer.writeStartElement("channel"); + writer.writeAttribute("id", getXmltvChannelId()); + for (String name : names) { + writer.writeStartElement("display-name"); + writer.writeAttribute("lang", "nl"); + writer.writeCharacters(name); + writer.writeEndElement(); + } + if (writeLogos) { + for (Icon i : icons) { + i.serialize(writer); + } + } + for (String url : urls) { + writer.writeStartElement("url"); + writer.writeCharacters(url); + writer.writeEndElement(); + } + writer.writeEndElement(); + writer.writeCharacters(System.getProperty("line.separator")); + } + + // Convenience method + public void addIcon(String url) { + icons.add(new Icon(url)); + } + + public void setEnabled(boolean b) { + 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() + ": " + + Config.escape(xmltv) + ": " + + 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("tvgids.nl", parts.get(1), + parts.get(2)); + if (parts.size() > 3) { + c.addIcon(parts.get(3)); + } + break; + case 1: + c = Channel.getChannel("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: + System.err.println("This config file format is no longer supported..."); + break; + case 3: + case 4: + case 5: { + String source = parts.get(1); + String id = parts.get(2); + String enabled = parts.get(3); + String name = parts.get(4); + if(fileformat<5) { + c = Channel.getChannel(source, id, name); + } else { + String extra = parts.get(5); + if(extra.isEmpty()) { + c = Channel.getChannel(source, id, name); + } else { + // Horizon channel + String xmltv=id+"."+source; + String horizonId=extra; + c = Channel.getChannel(source, horizonId, xmltv, name); + } + } + int iconPart = (fileformat<5?5:6); + if (parts.size() > iconPart) { + c.addIcon(parts.get(iconPart)); + } + if (enabled.equals("enabled")) { + c.setEnabled(true); + } else if (enabled.equals("disabled")) { + c.setEnabled(false); + } else { + logger.error("Error in config file, unknown channel status \"" + + enabled + + "\", should be enabled or disabled"); + } + break; + } + case 6: { + String source = parts.get(1); + String xmltv = parts.get(2); + String id = parts.get(3); + String enabled = parts.get(4); + String name = parts.get(5); + String extra = parts.get(6); + c = Channel.getChannel(source, id, xmltv, name); + if (parts.size() > 7) { + c.addIcon(parts.get(7)); + } + if (enabled.equals("enabled")) { + c.setEnabled(true); + } else if (enabled.equals("disabled")) { + c.setEnabled(false); + } else { + logger.error("Error in config file, unknown channel status \"" + + enabled + + "\", should be enabled or disabled"); + } + } + } + return c; + } + + public String toString() { + return "Channel " + source + "::" + id + " (" + defaultName() + ")"; + } +} diff --git a/src/main/java/org/vanbest/xmltv/EPGSource.java b/src/main/java/org/vanbest/xmltv/EPGSource.java index 8c3a632..9f7be2f 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSource.java +++ b/src/main/java/org/vanbest/xmltv/EPGSource.java @@ -11,15 +11,12 @@ public interface EPGSource { int cacheMisses = 0; } - public int getId(); - - public void setId(int id); + // All implementing class must declare a + // public final static String NAME = "...."; + // (e.g tvgids.nl or rtl.nl) public String getName(); - // All implementing class must declare a - // public static final String NAME (e.g tvgids.nl or rtl.nl) - public List getChannels(); // Convenience method diff --git a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java index e90d076..ac0903d 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java +++ b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java @@ -4,98 +4,81 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.NoSuchFieldException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Properties; +import org.reflections.Reflections; +import org.reflections.util.ClasspathHelper; +import org.reflections.scanners.SubTypesScanner; + import org.apache.log4j.Logger; public class EPGSourceFactory { - private static Map ids = new HashMap(); - private static Map> classes = new HashMap>(); - private static Map names = new HashMap(); - private static boolean initialised = false; - private static List sources = new ArrayList(); - static Logger logger = Logger.getLogger(EPGSourceFactory.class); + private static Map> sources = new HashMap>(); + + private static boolean initialised = false; + static Logger logger = Logger.getLogger(EPGSourceFactory.class); - static void init() { - if (initialised) - return; - Properties configProp = new Properties(); - ClassLoader loader = ClassLoader.getSystemClassLoader(); - InputStream in = loader - .getResourceAsStream("tv_grab_nl_java.properties"); - try { - configProp.load(in); - } catch (IOException e) { - logger.warn("Error reading application properties resource", e); - } - for (int source = 1;; source++) { - String name = configProp - .getProperty("org.vanbest.xmltv.epgsource.impl." + source); - if (name == null) - break; - try { - Class clazz = (Class) loader - .loadClass(name); - classes.put(source, clazz); - // System.out.println("clazz: " + clazz.toString()); - Field NAME = clazz.getField("NAME"); - // System.out.println("NAME: " + NAME.toString()); - String sourceName = (String) NAME.get(null); - names.put(source, sourceName); - ids.put(sourceName, source); - sources.add(source); - } catch (Exception e) { - logger.error("Error reading EPG Source class " + name, e); - } - } - initialised = true; - } + static void init() { + if (initialised) return; - private EPGSourceFactory() { - init(); - } + Reflections reflections = new Reflections( + ClasspathHelper.forPackage("org.vanbest.xmltv"), new SubTypesScanner()); + Set> implementingTypes = + reflections.getSubTypesOf(EPGSource.class); - public static EPGSourceFactory newInstance() { - return new EPGSourceFactory(); - } + for(Class clazz: implementingTypes) { + try { + Field NAME; + try { + NAME = clazz.getField("NAME"); + } catch (NoSuchFieldException e2) { + continue; + } + logger.debug("EPG source classname: " + NAME.toString()); + String sourceName = (String) NAME.get(null); + sources.put(sourceName, clazz); + } catch (Exception e) { + logger.error("Error reading EPG Source class " + clazz, e); + } + } + initialised = true; + } - public EPGSource createEPGSource(int source, Config config) { - Constructor constructor; - try { - constructor = classes.get(source).getConstructor(Integer.TYPE, - Config.class); - return constructor.newInstance(source, config); - } catch (Exception e) { - logger.error( - "Error instantiating EPG source " + classes.get(source), e); - } - return null; - } + private EPGSourceFactory() { + init(); + } - public EPGSource createEPGSource(String source, Config config) { - int sourceId = getChannelSourceId(source); - return createEPGSource(sourceId, config); - } + public static EPGSourceFactory newInstance() { + return new EPGSourceFactory(); + } - public static String getChannelSourceName(int id) { - init(); - return names.get(id); - } + public EPGSource createEPGSource(Class source, Config config) { + Constructor constructor; + try { + constructor = source.getConstructor(Config.class); + return constructor.newInstance(config); + } catch (Exception e) { + logger.error("Error instantiating EPG source " + source, e); + } + return null; + } - public static int getChannelSourceId(String name) { - init(); - return ids.get(name); - } + public EPGSource createEPGSource(String source, Config config) { + return createEPGSource(sources.get(source), config); + } - public int[] getAll() { - int[] result = new int[sources.size()]; - for (int i = 0; i < result.length; i++) { - result[i] = sources.get(i); - } - return result; - } + public List getAll(Config config) { + ArrayList result = new ArrayList(); + for(Class clazz: sources.values()) { + result.add(createEPGSource(clazz, config)); + } + return result; + } } diff --git a/src/main/java/org/vanbest/xmltv/Horizon.java b/src/main/java/org/vanbest/xmltv/Horizon.java index a1c2b80..e38d96d 100644 --- a/src/main/java/org/vanbest/xmltv/Horizon.java +++ b/src/main/java/org/vanbest/xmltv/Horizon.java @@ -50,12 +50,12 @@ public class Horizon extends AbstractEPGSource implements EPGSource { private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_HORIZON = 7; - public static String NAME = "horizon.tv"; + public final static String NAME="horizon.tv"; static Logger logger = Logger.getLogger(Horizon.class); - public Horizon(int sourceId, Config config) { - super(sourceId, config); + public Horizon(Config config) { + super(config); } public String getName() { @@ -136,7 +136,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource { } } String xmltv = name.replaceAll("[^a-zA-Z0-9]", "")+"."+getName(); - Channel c = Channel.getChannel(getId(), Long.toString(horizonId), xmltv, name); + Channel c = Channel.getChannel(getName(), Long.toString(horizonId), xmltv, name); //Channel c = new HorizonChannel(getId(), horizonId, name); c.addIcon(icon); result.add(c); @@ -214,7 +214,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource { private Programme programmeFromJSON(JSONObject json, boolean fetchDetails) throws Exception { String id = json.getString("id"); - Programme result = cache.get(getId(), id); + Programme result = cache.get(getName(), id); boolean cached = (result != null); boolean doNotCache = false; if (result == null) { @@ -308,7 +308,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource { // TODO other fields if (!cached && !doNotCache) { - cache.put(getId(), id, result); + cache.put(getName(), id, result); } logger.debug(result); return result; @@ -318,7 +318,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource { */ public static void main(String[] args) { Config config = Config.getDefaultConfig(); - Horizon horizon = new Horizon(3, config); + Horizon horizon = new Horizon(config); horizon.clearCache(); try { List channels = horizon.getChannels(); diff --git a/src/main/java/org/vanbest/xmltv/Main.java b/src/main/java/org/vanbest/xmltv/Main.java index 39b6ade..e98be90 100644 --- a/src/main/java/org/vanbest/xmltv/Main.java +++ b/src/main/java/org/vanbest/xmltv/Main.java @@ -97,7 +97,7 @@ public class Main { cache.clear(); cache.close(); } - Map guides = new HashMap(); + Map guides = new HashMap(); EPGSourceFactory factory = EPGSourceFactory.newInstance(); // EPGSource gids = new TvGids(config); // if (clearCache) gids.clearCache(); @@ -148,13 +148,13 @@ public class Main { writer.flush(); writer.close(); - for (int source : guides.keySet()) { + for (String source : guides.keySet()) { guides.get(source).close(); } if (!config.quiet) { EPGSource.Stats stats = new EPGSource.Stats(); - for (int source : guides.keySet()) { + for (String source : guides.keySet()) { EPGSource.Stats part = guides.get(source).getStats(); stats.cacheHits += part.cacheHits; stats.cacheMisses += part.cacheMisses; @@ -201,7 +201,7 @@ public class Main { Set oldChannels = new HashSet(); Set oldChannelNames = new HashSet(); - Set oldGuides = new HashSet(); + Set oldGuides = new HashSet(); for (Channel c : config.channels) { if (c.enabled) { oldChannels.add(c.source + "::" + c.id); @@ -211,18 +211,16 @@ public class Main { } EPGSourceFactory factory = EPGSourceFactory.newInstance(); - int[] sources = factory.getAll(); + List allSources = factory.getAll(config); - System.out - .println("Please select the TV programme information sources to use"); + System.out.println("Please select the TV programme information sources to use"); List guides = new ArrayList(); - for (int source : sources) { - boolean selected = oldGuides.contains(source); - EPGSource guide = factory.createEPGSource(source, config); - System.out.print(" Use \"" + guide.getName() + for(EPGSource source: allSources) { + boolean selected = oldGuides.contains(source.getName()); + System.out.print(" Use \"" + source.getName() + "\" (Y/N, default=" + (selected ? "Y" : "N") + "):"); if(config.configYes) { - guides.add(guide); + guides.add(source); System.out.println("Y"); continue; } @@ -231,10 +229,10 @@ public class Main { String s = reader.readLine().toLowerCase(); if (s.isEmpty()) { if (selected) - guides.add(guide); + guides.add(source); break; } else if (s.startsWith("y")) { - guides.add(guide); + guides.add(source); break; } else if (s.startsWith("n")) { break; diff --git a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java index 2857855..47bae75 100644 --- a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java +++ b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java @@ -52,10 +52,11 @@ public class ProgrammeCache { private PreparedStatement clearSourceStatement; /** The Constant SCHEMA_VERSION. */ - private final static Integer SCHEMA_VERSION = 2; + private final static Integer SCHEMA_VERSION = 3; - /** The Constant SCHEMA_KEY. */ - private final static String SCHEMA_KEY = "TV_GRAB_NL_JAVA_SCHEMA_VERSION"; + /** The Constant SCHEMA_KEY_ID. */ + private final static String SCHEMA_KEY_SOURCE = "TV_GRAB_NL_JAVA"; + private final static String SCHEMA_KEY_ID = "SCHEMA_VERSION"; /** The logger. */ static Logger logger = Logger.getLogger(ProgrammeCache.class); @@ -90,8 +91,8 @@ public class ProgrammeCache { try { PreparedStatement stat = db .prepareStatement("SELECT programme FROM cache WHERE source=? AND id=?"); - stat.setInt(1, 1); - stat.setString(2, SCHEMA_KEY); + stat.setString(1, SCHEMA_KEY_SOURCE); + stat.setString(2, SCHEMA_KEY_ID); ResultSet result = stat.executeQuery(); if (!result.next()) { logger.debug("No schema version found in database"); @@ -125,18 +126,18 @@ public class ProgrammeCache { logger.info("Unknown cache schema, removing and recreating cache"); try { Statement stat = db.createStatement(); - // System.out.println("Dropping old table"); + logger.trace("Dropping old table"); stat.execute("DROP TABLE IF EXISTS cache"); - // System.out.println("Creating new table"); - stat.execute("CREATE CACHED TABLE IF NOT EXISTS cache (source INTEGER, id VARCHAR(128), date DATE, programme OTHER, PRIMARY KEY (source,id))"); + logger.trace("Creating new table"); + stat.execute("CREATE CACHED TABLE IF NOT EXISTS cache (source CHAR(20), id VARCHAR(128), date DATE, programme OTHER, PRIMARY KEY (source,id))"); stat.close(); - // System.out.println("Writing new schema version to database"); + logger.trace("Writing new schema version to database"); PreparedStatement stat2 = db .prepareStatement("INSERT INTO cache VALUES (?,?,?,?)"); - stat2.setInt(1, 1); - stat2.setString(2, SCHEMA_KEY); + stat2.setString(1, SCHEMA_KEY_SOURCE); + stat2.setString(2, SCHEMA_KEY_ID); stat2.setDate(3, new java.sql.Date(new java.util.Date(2100, 11, 31).getTime())); stat2.setObject(4, SCHEMA_VERSION); @@ -179,11 +180,11 @@ public class ProgrammeCache { * @param id the id * @return the programme */ - public Programme get(int source, String id) { + public Programme get(String source, String id) { if (db == null) return null; try { - getStatement.setInt(1, source); + getStatement.setString(1, source); getStatement.setString(2, id); ResultSet r = getStatement.executeQuery(); if (!r.next()) @@ -211,9 +212,9 @@ public class ProgrammeCache { * @param source the source * @param id the id */ - private void removeCacheEntry(int source, String id) { + private void removeCacheEntry(String source, String id) { try { - removeStatement.setInt(1, source); + removeStatement.setString(1, source); removeStatement.setString(2, id); removeStatement.execute(); } catch (SQLException e) { @@ -230,14 +231,13 @@ public class ProgrammeCache { * @param id the id * @param prog the prog */ - public void put(int source, String id, Programme prog) { + public void put(String source, String id, Programme prog) { if (db == null) return; try { - putStatement.setInt(1, source); + putStatement.setString(1, source); putStatement.setString(2, id); - putStatement - .setDate(3, new java.sql.Date(prog.startTime.getTime())); + putStatement.setDate(3, new java.sql.Date(prog.startTime.getTime())); putStatement.setObject(4, prog); // System.out.println(putStatement.toString()); int count = putStatement.executeUpdate(); @@ -294,11 +294,11 @@ public class ProgrammeCache { * * @param source the source */ - public void clear(int source) { + public void clear(String source) { if (db == null) return; try { - clearSourceStatement.setInt(1, source); + clearSourceStatement.setString(1, source); int count = clearSourceStatement.executeUpdate(); if (!config.quiet && count > 0) { logger.info("Cleared " + count + " entries from cache"); diff --git a/src/main/java/org/vanbest/xmltv/RTL.java b/src/main/java/org/vanbest/xmltv/RTL.java index d49055f..0a84a4a 100644 --- a/src/main/java/org/vanbest/xmltv/RTL.java +++ b/src/main/java/org/vanbest/xmltv/RTL.java @@ -49,7 +49,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { private static final String icon_url = "http://www.rtl.nl/service/gids/components/vaste_componenten/"; private static final int MAX_PROGRAMMES_PER_DAY = 9999; - public static final String NAME = "rtl.nl"; + public static final String NAME="rtl.nl"; static Logger logger = Logger.getLogger(RTL.class); static boolean debug = false; @@ -65,8 +65,8 @@ public class RTL extends AbstractEPGSource implements EPGSource { } } - public RTL(int sourceId, Config config) { - super(sourceId, config); + public RTL(Config config) { + super(config); } public String getName() { @@ -94,7 +94,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { JSONObject schedule = schedules.getJSONObject(i); String channel = schedule.getString("station"); if(!channels.containsKey(channel)) { - Channel c = Channel.getChannel(getId(), channel, channel); + Channel c = Channel.getChannel(getName(), channel, channel); // TODO: channel icon channels.put(channel, c); } @@ -246,7 +246,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { List result = new LinkedList(); Map channelMap = new HashMap(); for (Channel c : channels) { - if (c.enabled && c.source == getId()) + if (c.enabled && c.source.equals(getName())) channelMap.put(c.id, c); } URL url = programmeUrl(0, day); @@ -284,7 +284,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { Config config = Config.getDefaultConfig(); config.niceMilliseconds = 50; - RTL rtl = new RTL(2, config); + RTL rtl = new RTL(config); if (debug) { rtl.cache.clear(); logger.info("Writing CSV to rtl.csv"); diff --git a/src/main/java/org/vanbest/xmltv/TvGids.java b/src/main/java/org/vanbest/xmltv/TvGids.java index 92307da..b8d5f71 100644 --- a/src/main/java/org/vanbest/xmltv/TvGids.java +++ b/src/main/java/org/vanbest/xmltv/TvGids.java @@ -50,17 +50,16 @@ public class TvGids extends AbstractEPGSource implements EPGSource { private static final int MAX_PROGRAMMES_PER_DAY = 9999; private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_TVGIDS = 3; - - public static String NAME = "tvgids.nl"; + public static final String NAME="tvgids.nl"; static Logger logger = Logger.getLogger(TvGids.class); - public TvGids(int sourceId, Config config) { - super(sourceId, config); + public TvGids(Config config) { + super(config); } public String getName() { - return NAME; + return NAME; } public static URL programmeUrl(List channels, int day) @@ -142,7 +141,7 @@ 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); + Channel c = Channel.getChannel(getName(), Integer.toString(id), name); c.addIcon(icon); result.add(c); } @@ -220,7 +219,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { private Programme programmeFromJSON(JSONObject programme, boolean fetchDetails) throws Exception { String id = programme.getString("db_id"); - Programme result = cache.get(getId(), id); + Programme result = cache.get(getName(), id); boolean cached = (result != null); if (result == null) { stats.cacheMisses++; @@ -265,7 +264,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { } if (!cached) { // FIXME where to do this? - cache.put(getId(), id, result); + cache.put(getName(), id, result); } logger.debug(result); return result; @@ -431,7 +430,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { */ public static void main(String[] args) { Config config = Config.getDefaultConfig(); - TvGids gids = new TvGids(1, config); + TvGids gids = new TvGids(config); try { List channels = gids.getChannels(); System.out.println("Channels: " + channels); diff --git a/src/main/java/org/vanbest/xmltv/ZiggoGids.java b/src/main/java/org/vanbest/xmltv/ZiggoGids.java index 65ace76..0b5f7ae 100644 --- a/src/main/java/org/vanbest/xmltv/ZiggoGids.java +++ b/src/main/java/org/vanbest/xmltv/ZiggoGids.java @@ -74,13 +74,12 @@ public class ZiggoGids extends AbstractEPGSource implements EPGSource { private static final int MAX_PROGRAMMES_PER_DAY = 9999; private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_ZIGGOGIDS = 3; private static final int MAX_CHANNELS_PER_REQUEST = 25; - - public static String NAME = "ziggogids.nl"; + public final static String NAME="ziggogids.nl"; static Logger logger = Logger.getLogger(ZiggoGids.class); - public ZiggoGids(int sourceId, Config config) { - super(sourceId, config); + public ZiggoGids(Config config) { + super(config); } public String getName() { @@ -201,7 +200,7 @@ public class ZiggoGids extends AbstractEPGSource implements EPGSource { String index = e.select("input").first().attr("value"); String name = e.select("label").first().text(); logger.debug(" "+index+": \""+name+"\""); - Channel c = Channel.getChannel(getId(), index, name); + Channel c = Channel.getChannel(getName(), index, name); try { String icon = fetchIconUrl(httpclient, index); logger.debug(" "+icon); @@ -272,7 +271,7 @@ public class ZiggoGids extends AbstractEPGSource implements EPGSource { long start = Long.parseLong(item.attr("pr-start")); // unix time String id = Long.toString(start)+"_"+progid; - Programme p = cache.get(getId(), id); + Programme p = cache.get(getName(), id); boolean cached = (p != null); if (p == null) { stats.cacheMisses++; @@ -292,7 +291,7 @@ public class ZiggoGids extends AbstractEPGSource implements EPGSource { } if (!cached) { // FIXME where to do this? - cache.put(getId(), id, p); + cache.put(getName(), id, p); } return p; } @@ -328,7 +327,7 @@ public class ZiggoGids extends AbstractEPGSource implements EPGSource { public static void main(String[] args) { Config config = Config.getDefaultConfig(); logger.setLevel(Level.TRACE); - ZiggoGids gids = new ZiggoGids(4, config); + ZiggoGids gids = new ZiggoGids(config); try { List channels = gids.getChannels(); System.out.println("Channels: " + channels); diff --git a/src/test/java/org/vanbest/xmltv/HorizonTest.java b/src/test/java/org/vanbest/xmltv/HorizonTest.java index 3553eed..cb7df42 100644 --- a/src/test/java/org/vanbest/xmltv/HorizonTest.java +++ b/src/test/java/org/vanbest/xmltv/HorizonTest.java @@ -12,13 +12,12 @@ import org.junit.Test; public class HorizonTest { private static Horizon horizon; - private final static int HORIZON_SOURCE_ID = 3; private static List channels = null; @BeforeClass public static void setUp() throws Exception { Config config = Config.getDefaultConfig(); - horizon = new Horizon(HORIZON_SOURCE_ID, config); + horizon = new Horizon(config); horizon.clearCache(); } @@ -49,7 +48,7 @@ public class HorizonTest { c.icons.isEmpty()); } assertEquals("All channels should have RTL.nl source id", - HORIZON_SOURCE_ID, c.source); + horizon.getName(), c.source); } if (!foundRTL4) { fail("Channel RTL4 not found, should be there"); diff --git a/src/test/java/org/vanbest/xmltv/RTLTest.java b/src/test/java/org/vanbest/xmltv/RTLTest.java index b79764d..de9f12b 100644 --- a/src/test/java/org/vanbest/xmltv/RTLTest.java +++ b/src/test/java/org/vanbest/xmltv/RTLTest.java @@ -13,13 +13,12 @@ import org.junit.Test; public class RTLTest { private static RTL rtl; - private final static int RTL_SOURCE_ID = 2; private static List channels = null; @BeforeClass public static void setUp() throws Exception { Config config = Config.getDefaultConfig(); - rtl = new RTL(RTL_SOURCE_ID, config); + rtl = new RTL(config); rtl.clearCache(); } @@ -49,7 +48,7 @@ public class RTLTest { //assertFalse("RTL4 channel should have at least one icon", c.icons.isEmpty()); } assertEquals("All channels should have RTL.nl source id", - RTL_SOURCE_ID, c.source); + rtl.getName(), c.source); } if (!foundRTL4) { fail("Channel RTL4 not found, should be there");