]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Refactor: use source name as key for EPGSource
authorJan-Pascal van Best <janpascal@vanbest.org>
Thu, 16 Jan 2014 20:58:51 +0000 (21:58 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Thu, 16 Jan 2014 20:58:51 +0000 (21:58 +0100)
13 files changed:
pom.xml
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Channel.java
src/main/java/org/vanbest/xmltv/EPGSource.java
src/main/java/org/vanbest/xmltv/EPGSourceFactory.java
src/main/java/org/vanbest/xmltv/Horizon.java
src/main/java/org/vanbest/xmltv/Main.java
src/main/java/org/vanbest/xmltv/ProgrammeCache.java
src/main/java/org/vanbest/xmltv/RTL.java
src/main/java/org/vanbest/xmltv/TvGids.java
src/main/java/org/vanbest/xmltv/ZiggoGids.java
src/test/java/org/vanbest/xmltv/HorizonTest.java
src/test/java/org/vanbest/xmltv/RTLTest.java

diff --git a/pom.xml b/pom.xml
index 4ca9bde41d839ff3033bb71393583e803efecb12..956f852d72e622924dd891b61a72ea7ff73889fc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                   <artifactId>httpclient</artifactId>
                   <version>4.3.1</version>
                 </dependency>
+                 <dependency>
+                     <groupId>org.reflections</groupId>
+                     <artifactId>reflections</artifactId>
+                     <version>0.9.9-RC1</version>
+                 </dependency>
        </dependencies>
        <build>
                <plugins>
index 804e2a6424b81f765f304d3aae02892dd636fc28..a0d418f64f0c1848ab5aa8071c48135d6427458b 100644 (file)
@@ -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<Programme> getProgrammes(Channel channel, int day)
                        throws Exception {
                ArrayList<Channel> list = new ArrayList<Channel>(2);
@@ -103,7 +93,7 @@ public abstract class AbstractEPGSource implements EPGSource {
        }
 
        public void clearCache() {
-               cache.clear(sourceId);
+               cache.clear(getName());
        }
 
         String kijkwijzerCategorie(char c) {
index 211cc16fb3d5692197269b8b877d7036335e9713..d824e83fe0ee97792fb57afd71d1e6f8a37bda29 100644 (file)
-package org.vanbest.xmltv;\r
-\r
-import java.io.PrintWriter;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import javax.xml.stream.XMLStreamException;\r
-import javax.xml.stream.XMLStreamWriter;\r
-import org.apache.log4j.Logger;\r
-\r
-public class Channel {\r
-       String id; // Internal id, used by EPG provider\r
-        String xmltv; // xmltv id such as '1.tvgids.nl' or 'Nederland1.horizon.tv'\r
-       List<String> names; // at least one name is obligatory\r
-       List<Icon> icons;\r
-       List<String> urls;\r
-       protected boolean enabled = true;\r
-       int source;\r
-       static Logger logger = Logger.getLogger(Channel.class);\r
-\r
-       protected Channel(int source, String id, String xmltv) {\r
-               this.id = id;\r
-               this.source = source;\r
-                this.xmltv = xmltv;\r
-               names = new ArrayList<String>();\r
-               icons = new ArrayList<Icon>();\r
-               urls = new ArrayList<String>();\r
-       }\r
-\r
-       public String defaultName() {\r
-               return names.get(0);\r
-       }\r
-\r
-       static Channel getChannel(int source, String id, String xmltv, String name) {\r
-                Channel c = new Channel(source, id, xmltv);\r
-               c.names.add(name);\r
-               return c;\r
-       }\r
-\r
-        // Use default xmltvid with id+"."+sourceName\r
-       static Channel getChannel(int source, String id, String name) {\r
-                String xmltv = id + "." + EPGSourceFactory.getChannelSourceName(source);\r
-                Channel c = new Channel(source, id, xmltv);\r
-               c.names.add(name);\r
-               return c;\r
-       }\r
-/*\r
-       static Channel getChannel(int source, String id, String name, String extraConfig) {\r
-                Channel c;\r
-                if(EPGSourceFactory.getChannelSourceName(source).equals(Horizon.NAME)) {\r
-                        long horizonId=Long.parseLong(extraConfig);\r
-                       c = new Horizon.HorizonChannel(source, id, horizonId);\r
-                } else {\r
-                       c = new Channel(source, id);\r
-                }\r
-               c.names.add(name);\r
-               return c;\r
-       }\r
-*/\r
-       public String getXmltvChannelId() {\r
-               return xmltv; // id + "." + getSourceName();\r
-       }\r
-\r
-       public String getSourceName() {\r
-               return EPGSourceFactory.getChannelSourceName(source);\r
-       }\r
-\r
-       public void serialize(XMLStreamWriter writer, boolean writeLogos) throws XMLStreamException {\r
-               writer.writeStartElement("channel");\r
-               writer.writeAttribute("id", getXmltvChannelId());\r
-               for (String name : names) {\r
-                       writer.writeStartElement("display-name");\r
-                       writer.writeAttribute("lang", "nl");\r
-                       writer.writeCharacters(name);\r
-                       writer.writeEndElement();\r
-               }\r
-                if (writeLogos) {\r
-                    for (Icon i : icons) {\r
-                            i.serialize(writer);\r
-                    }\r
-                }\r
-               for (String url : urls) {\r
-                       writer.writeStartElement("url");\r
-                       writer.writeCharacters(url);\r
-                       writer.writeEndElement();\r
-               }\r
-               writer.writeEndElement();\r
-               writer.writeCharacters(System.getProperty("line.separator"));\r
-       }\r
-\r
-       // Convenience method\r
-       public void addIcon(String url) {\r
-               icons.add(new Icon(url));\r
-       }\r
-\r
-       public void setEnabled(boolean b) {\r
-               this.enabled = b;\r
-       }\r
-\r
-        public String extraConfig() {\r
-                return "";\r
-        }\r
-\r
-        // must start with channel: <sourcename>\r
-        public void writeConfig(PrintWriter out) {\r
-               // FIXME: handle multiple channels names, icons and urls\r
-                out.print("channel: " + getSourceName() + ": "\r
-                                + Config.escape(xmltv) + ": "\r
-                                + id + ": "\r
-                                + (enabled ? "enabled" : "disabled") + ": "\r
-                                + Config.escape(defaultName()) + ": "\r
-                                + Config.escape(extraConfig()));\r
-                if (!icons.isEmpty()) {\r
-                        out.print(" : " + Config.escape(icons.get(0).url));\r
-                }\r
-                out.println();\r
-        }\r
-\r
-        public static Channel parseConfig(int fileformat, List<String> parts) {\r
-                Channel c = null;\r
-                switch (fileformat) {\r
-                case 0:\r
-                        c = Channel.getChannel(EPGSourceFactory.newInstance()\r
-                                        .getChannelSourceId("tvgids.nl"), parts.get(1),\r
-                                        parts.get(2));\r
-                        if (parts.size() > 3) {\r
-                                c.addIcon(parts.get(3));\r
-                        }\r
-                        break;\r
-                case 1:\r
-                        c = Channel.getChannel(EPGSourceFactory.newInstance()\r
-                                        .getChannelSourceId("tvgids.nl"), parts.get(1),\r
-                                        parts.get(3));\r
-                        if (parts.size() > 4) {\r
-                                c.addIcon(parts.get(4));\r
-                        }\r
-                        String value = parts.get(2);\r
-                        if (value.equals("enabled")) {\r
-                                c.setEnabled(true);\r
-                        } else if (value.equals("disabled")) {\r
-                                c.setEnabled(false);\r
-                        } else {\r
-                                logger.error("Error in config file, unknown channel status \""\r
-                                                + parts.get(2)\r
-                                                + "\", should be enabled or disabled");\r
-                        }\r
-                        break;\r
-                case 2:\r
-                case 3:\r
-                case 4:\r
-                case 5: {\r
-                        int source;\r
-                        if (fileformat == 2) {\r
-                                source = Integer.parseInt(parts.get(1));\r
-                        } else {\r
-                                source = EPGSourceFactory.newInstance()\r
-                                                .getChannelSourceId(parts.get(1));\r
-                        }\r
-                        String id = parts.get(2);\r
-                        String enabled = parts.get(3);\r
-                        String name = parts.get(4);\r
-                        if(fileformat<5) {\r
-                                c = Channel.getChannel(source, id, name);\r
-                        } else {\r
-                                String extra = parts.get(5);\r
-                                if(extra.isEmpty()) {\r
-                                    c = Channel.getChannel(source, id, name);\r
-                                } else {\r
-                                    // Horizon channel\r
-                                    String xmltv=id+"."+EPGSourceFactory.getChannelSourceName(source);\r
-                                    String horizonId=extra;\r
-                                    c = Channel.getChannel(source, horizonId, xmltv, name);\r
-                                }\r
-                        }\r
-                        int iconPart = (fileformat<5?5:6);\r
-                        if (parts.size() > iconPart) {\r
-                                c.addIcon(parts.get(iconPart));\r
-                        }\r
-                        if (enabled.equals("enabled")) {\r
-                                c.setEnabled(true);\r
-                        } else if (enabled.equals("disabled")) {\r
-                                c.setEnabled(false);\r
-                        } else {\r
-                                logger.error("Error in config file, unknown channel status \""\r
-                                                + enabled \r
-                                                + "\", should be enabled or disabled");\r
-                        }\r
-                        break;\r
-                }\r
-                case 6: {\r
-                        int source = EPGSourceFactory.getChannelSourceId(parts.get(1));\r
-                        String xmltv = parts.get(2);\r
-                        String id = parts.get(3);\r
-                        String enabled = parts.get(4);\r
-                        String name = parts.get(5);\r
-                        String extra = parts.get(6);\r
-                        c = Channel.getChannel(source, id, xmltv, name);\r
-                        if (parts.size() > 7) {\r
-                                c.addIcon(parts.get(7));\r
-                        }\r
-                        if (enabled.equals("enabled")) {\r
-                                c.setEnabled(true);\r
-                        } else if (enabled.equals("disabled")) {\r
-                                c.setEnabled(false);\r
-                        } else {\r
-                                logger.error("Error in config file, unknown channel status \""\r
-                                                + enabled\r
-                                                + "\", should be enabled or disabled");\r
-                        }\r
-                }\r
-                }\r
-                return c;\r
-        }\r
-\r
-       public String toString() {\r
-               return "Channel " + source + "::" + id + " (" + defaultName() + ")";\r
-       }\r
-}\r
+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<String> names; // at least one name is obligatory
+       List<Icon> icons;
+       List<String> 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<String>();
+               icons = new ArrayList<Icon>();
+               urls = new ArrayList<String>();
+       }
+
+       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: <sourcename>
+        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<String> 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() + ")";
+       }
+}
index 8c3a6326dca7cec7097b9311cdd0e2c804894a2f..9f7be2f718844dd84f8ae6eae6654d7ed018de80 100644 (file)
@@ -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<Channel> getChannels();
 
        // Convenience method
index e90d076530c1736d8b505bdc37e43f04b83bac57..ac0903dcdf32344001618a77c5f53c4e4ad56139 100644 (file)
@@ -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<String, Integer> ids = new HashMap<String, Integer>();
-       private static Map<Integer, Class<EPGSource>> classes = new HashMap<Integer, Class<EPGSource>>();
-       private static Map<Integer, String> names = new HashMap<Integer, String>();
-       private static boolean initialised = false;
-       private static List<Integer> sources = new ArrayList<Integer>();
-       static Logger logger = Logger.getLogger(EPGSourceFactory.class);
+    private static Map<String, Class<? extends EPGSource>> sources = new HashMap<String, Class<? extends EPGSource>>();
+
+    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<EPGSource> clazz = (Class<EPGSource>) 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<Class<? extends EPGSource>> implementingTypes =
+            reflections.getSubTypesOf(EPGSource.class);
 
-       public static EPGSourceFactory newInstance() {
-               return new EPGSourceFactory();
-       }
+        for(Class<? extends EPGSource> 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<EPGSource> 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<? extends EPGSource> source, Config config) {
+        Constructor<? extends EPGSource> 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<EPGSource> getAll(Config config) {
+        ArrayList<EPGSource> result = new ArrayList<EPGSource>();
+        for(Class<? extends EPGSource> clazz: sources.values()) {
+            result.add(createEPGSource(clazz, config));
+        }
+        return result;
+    }
 }
index a1c2b80550025f970938de4cb07525f39db4f756..e38d96d9b119c69f660729548e8db83bac608a05 100644 (file)
@@ -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<Channel> channels = horizon.getChannels();
index 39b6adee315922b89bfae1d886ee18bf37a98874..e98be9025b31cc481f7ae30cf09e0045a04d9233 100644 (file)
@@ -97,7 +97,7 @@ public class Main {
                        cache.clear();
                        cache.close();
                }
-               Map<Integer, EPGSource> guides = new HashMap<Integer, EPGSource>();
+               Map<String, EPGSource> guides = new HashMap<String, EPGSource>();
                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<String> oldChannels = new HashSet<String>();
                Set<String> oldChannelNames = new HashSet<String>();
-               Set<Integer> oldGuides = new HashSet<Integer>();
+               Set<String> oldGuides = new HashSet<String>();
                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<? extends EPGSource> 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<EPGSource> guides = new ArrayList<EPGSource>();
-               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;
index 28578550fa87265615f97b87f4bb916e2f60dcc6..47bae7551d757618be7994753e8a6c2423e8773d 100644 (file)
@@ -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");
index d49055f7c08deacdb81d5246f9e0dcd5e27168f1..0a84a4a390c94e73e2df2bb9e8d7f0422ce087c7 100644 (file)
@@ -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<Programme> result = new LinkedList<Programme>();
         Map<String, Channel> channelMap = new HashMap<String, Channel>();
         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");
index 92307dac2e597dfc4792d8dcc99fcf3a17278800..b8d5f7107aea24c3eafd45a2dfbf9eca5f71d094 100644 (file)
@@ -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<Channel> 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<Channel> channels = gids.getChannels();
                        System.out.println("Channels: " + channels);
index 65ace76eb4ab58f5f277ca2d3566e8757feeefa7..0b5f7aec871fcbaad06e854012923c1993864fec 100644 (file)
@@ -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<Channel> channels = gids.getChannels();
                        System.out.println("Channels: " + channels);
index 3553eede06963a0e6423bd18a9524b02df096105..cb7df42396c6c91d7cd330f9c225676ff5a05019 100644 (file)
@@ -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<Channel> 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");
index b79764daf154abf9d254518b52aa159b1d97ce29..de9f12b79e68c009bb1be0c809feb57d47c8f20c 100644 (file)
@@ -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<Channel> 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");