]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Configure epg sources in proerties file (experimental)
authorJan-Pascal van Best <janpascal@vanbest.org>
Wed, 4 Apr 2012 14:58:15 +0000 (16:58 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Wed, 4 Apr 2012 14:58:15 +0000 (16:58 +0200)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Channel.java
src/main/java/org/vanbest/xmltv/Config.java
src/main/java/org/vanbest/xmltv/EPGSource.java
src/main/java/org/vanbest/xmltv/EPGSourceFactory.java
src/main/java/org/vanbest/xmltv/RTL.java
src/main/java/org/vanbest/xmltv/TvGids.java
src/main/resources/tv_grab_nl_java.properties

index 6dcf0762264fa8bb7e026ba9f8b9b93de2ffc4fa..2eb200e3c4387a3738cd05c75ab19119d8d0ffd9 100644 (file)
@@ -13,6 +13,7 @@ import org.vanbest.xmltv.EPGSource.Stats;
 
 public abstract class AbstractEPGSource implements EPGSource {
 
+       private int sourceId;
        protected Config config;
        protected ProgrammeCache cache;
        protected Stats stats = new Stats();
@@ -24,6 +25,14 @@ public abstract class AbstractEPGSource implements EPGSource {
                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);
index e1725b8b2f39b02624976409d114dc87b3b495cd..5b8f84e6cfc9b6f0bfbbf3e02a00940183038b07 100644 (file)
@@ -46,7 +46,7 @@ public class Channel {
        }\r
        \r
        public String getSourceName() {\r
-               return EPGSourceFactory.getChannelSourceName(source);\r
+               return EPGSourceFactory.newInstance().getChannelSourceName(source);\r
        }\r
        \r
        public void serialize(XMLStreamWriter writer) throws XMLStreamException {\r
index 0707120987497459963f2cb7f38d8629548cb083..835627ba8926b0c04b6e1dd2b97ad5a9e6e55131 100644 (file)
@@ -64,7 +64,8 @@ public class Config {
        
        private Config() {
                Properties configProp = new Properties();
-        InputStream in = this.getClass().getResourceAsStream("/tv_grab_nl_java.properties");
+        InputStream in = ClassLoader.getSystemResourceAsStream("/tv_grab_nl_java.properties");
+        System.out.println(in);
         try {
             configProp.load(in);
         } catch (IOException e) {
@@ -214,13 +215,13 @@ public class Config {
                                        // System.out.println("Adding channel " + parts + " in file format " + fileformat);
                                        switch(fileformat) {
                                        case 0:
-                                               c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_TVGIDS, parts.get(1), parts.get(2));
+                                               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.CHANNEL_SOURCE_TVGIDS, parts.get(1), parts.get(3));
+                                               c = Channel.getChannel(EPGSourceFactory.newInstance().getChannelSourceId("tvgids.nl"), parts.get(1), parts.get(3));
                                                if (parts.size()>4) {
                                                        c.addIcon(parts.get(4));
                                                }
@@ -240,7 +241,7 @@ public class Config {
                                                if (fileformat==2) {
                                                        source = Integer.parseInt(parts.get(1));
                                                } else {
-                                                       source = EPGSourceFactory.getChannelSourceId(parts.get(1));
+                                                       source = EPGSourceFactory.newInstance().getChannelSourceId(parts.get(1));
                                                }
                                                c = Channel.getChannel(source, parts.get(2), parts.get(4));
                                                if (parts.size()>5) {
index fd5a4971a7ecf6d45a6599728e2704c8358f4a76..d3211b7a34a52e7b99308fbac5eff61b413ea821 100644 (file)
@@ -13,6 +13,7 @@ public interface EPGSource {
        }
 
        public int getId();
+       public void setId(int id);
     public String getName();
 
        public List<Channel> getChannels();
index 9634b1729faca28d9945b321cf9b217ccc217eac..7a78bc87a44ffc41ce466e426c9c07a330c1d133 100644 (file)
@@ -1,18 +1,58 @@
 package org.vanbest.xmltv;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 public class EPGSourceFactory {
-       
-       public final static int CHANNEL_SOURCE_TVGIDS=1;
-       public final static int CHANNEL_SOURCE_RTL=2;
-       
-       private final static int[] CHANNEL_IDS={CHANNEL_SOURCE_TVGIDS, CHANNEL_SOURCE_RTL};
-       private final static String[] CHANNEL_SOURCE_NAMES={"tvgids.nl", "rtl.nl"};
-       private static Map<String,Integer> channelSourceNameMap = new HashMap<String,Integer>();
-       
+       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;
+
+       static void init() {
+               if(initialised) return;
+               Properties configProp = new Properties();
+        ClassLoader loader = ClassLoader.getSystemClassLoader();
+        InputStream in = loader.getResourceAsStream("/org/vanbest/xmltv/tv_grab_nl_java.properties");
+        try {
+            configProp.load(in);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        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);
+                               Method getName=clazz.getMethod("getName");
+                               String sourceName=(String)getName.invoke(null);
+                               names.put(source,sourceName);
+                               ids.put(sourceName,source);
+                       } catch (ClassNotFoundException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       } catch (ReflectiveOperationException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       } catch (SecurityException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
+        }
+        initialised=true;
+       }
+
        private EPGSourceFactory() {
+               init();
        }
 
        public static EPGSourceFactory newInstance() {
@@ -20,37 +60,52 @@ public class EPGSourceFactory {
        }
        
        public EPGSource createEPGSource(int source, Config config) {
-               switch(source) {
-               case EPGSourceFactory.CHANNEL_SOURCE_RTL:
-                       return new RTL(config, false);
-               case EPGSourceFactory.CHANNEL_SOURCE_TVGIDS:
-                       return new TvGids(config);
-               default:
-                       return null;
+               Constructor<EPGSource> constructor;
+               try {
+                       constructor = classes.get(source).getConstructor(Config.class);
+                       return constructor.newInstance(source, config);
+               } catch (NoSuchMethodException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (SecurityException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (InstantiationException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalAccessException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalArgumentException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (InvocationTargetException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
                }
+               return null;
        }
        
        public EPGSource createEPGSource(String source, Config config) {
-               int sourceId = EPGSourceFactory.getChannelSourceId(source);
+               int sourceId = getChannelSourceId(source);
                return createEPGSource(sourceId, config);
        }
        
-       public static String getChannelSourceName(int id) {
-               return CHANNEL_SOURCE_NAMES[id-1];
+       public String getChannelSourceName(int id) {
+               return names.get(id);
        }
        
-       public static int getChannelSourceId(String name) {
-               if (channelSourceNameMap.isEmpty()) {
-                       int i=1;
-                       for (String s: EPGSourceFactory.CHANNEL_SOURCE_NAMES) {
-                               EPGSourceFactory.channelSourceNameMap.put(s,  i);
-                               i++;
-                       }
-               }
-               return EPGSourceFactory.channelSourceNameMap.get(name);
+       public int getChannelSourceId(String name) {
+               return ids.get(name);
        }
-       
+
        public int[] getAll() {
-               return CHANNEL_IDS;
+               Integer[] list = (Integer[]) classes.keySet().toArray();
+               Arrays.sort(list);
+               int[] result = new int[list.length];
+               for(int i=0; i<result.length; i++) {
+                       result[i]=list[i];
+               }
+               return result;
        }
 }
index 0a05eca35e818ad4553f68724849f7d81260fa5b..038f9bcc5e14cad94dba6627074f8328773868a3 100644 (file)
@@ -59,16 +59,12 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
        private static final String programme_url="http://www.rtl.nl/active/epg_data/dag_data/";\r
        private static final String detail_url="http://www.rtl.nl/active/epg_data/uitzending_data/";\r
        private static final String icon_url="http://www.rtl.nl/service/gids/components/vaste_componenten/";\r
-       private static final String xmltv_channel_suffix = ".rtl.nl";\r
        private static final int MAX_PROGRAMMES_PER_DAY = 9999;\r
        \r
-       private Connection db;\r
-       \r
        String[] xmlKeys = {"zendernr", "pgmsoort", "genre", "bijvnwlanden", "ondertiteling", "begintijd", "titel", \r
                        "site_path", "wwwadres", "presentatie", "omroep", "eindtijd", "inhoud", "tt_inhoud", "alginhoud", "afl_titel", "kijkwijzer" };\r
                \r
-       Map<String,Integer> xmlKeyMap = new HashMap<String,Integer>();\r
-       private ProgrammeCache cache;\r
+       //Map<String,Integer> xmlKeyMap = new HashMap<String,Integer>();\r
        \r
        class RTLException extends Exception {\r
                public RTLException(String s) {\r
@@ -76,53 +72,14 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                }\r
        }\r
        \r
-       public RTL(Config config, boolean useDB) {\r
+       public RTL(Config config) {\r
                super(config);\r
-               cache = new ProgrammeCache(config);\r
-               try {\r
-                       if (useDB) {\r
-                               Properties dbProp = new Properties();\r
-                       try {\r
-                               InputStream in = new FileInputStream("tv_grab_nl_java.db.properties");\r
-                           dbProp.load(in);\r
-                       } catch (IOException e) {\r
-                           e.printStackTrace();\r
-                       }\r
-                       db = DriverManager.getConnection(dbProp.getProperty("db_url"), dbProp.getProperty("db_user"), dbProp.getProperty("db_passwd"));\r
-                               Statement stat = db.createStatement();\r
-                               StringBuilder s = new StringBuilder();\r
-                               s.append("CREATE TABLE IF NOT EXISTS prog (id VARCHAR(32) primary key, ");\r
-                               int i=0;\r
-                               for( String key: xmlKeys) {\r
-                                       if(i>0) s.append(", ");\r
-                                       xmlKeyMap.put(key, i+1);\r
-                                       s.append(key);\r
-                                       s.append(" TEXT");\r
-                                       i++;\r
-                               }\r
-                               s.append(");");\r
-                               System.out.println(s);\r
-                               stat.execute(s.toString());\r
-                               stat.execute("TRUNCATE TABLE prog");\r
-                       } else {\r
-                               db = null;\r
-                       }\r
-               } catch (SQLException e) {\r
-                       // TODO Auto-generated catch block\r
-                       e.printStackTrace();\r
-                       System.exit(1);\r
-                       db = null;\r
-               }\r
        }\r
-\r
-       public int getId() {\r
-         return EPGSourceFactory.CHANNEL_SOURCE_RTL;\r
-    }\r
        \r
-    public String getName() {\r
-        return EPGSourceFactory.getChannelSourceName(getId());\r
-    }\r
-    \r
+       public String getName() {\r
+               return "rtl.nl";\r
+       }\r
+       \r
        public List<Channel> getChannels() {\r
                List<Channel> result = new ArrayList<Channel>(10);\r
 \r
@@ -149,7 +106,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                        String name = (String) j.get(0);\r
                        String icon = icon_url+id+".gif";\r
                        \r
-                       Channel c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_RTL, id, name, icon);\r
+                       Channel c = Channel.getChannel(getId(), id, name, icon);\r
                        result.add(c);\r
                }\r
 \r
@@ -204,25 +161,6 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                if (root.hasAttributes()) {\r
                        System.out.println("Unknown attributes for RTL detail root node");\r
                }\r
-               PreparedStatement stat = null;\r
-               if (db != null) {\r
-                       StringBuilder sql = new StringBuilder("INSERT INTO prog (id");\r
-                       StringBuilder sql2= new StringBuilder(") values (?");\r
-                       for(String key:xmlKeys) {\r
-                               sql.append(",");\r
-                               sql.append(key);\r
-                               sql2.append(",");\r
-                               sql2.append("?");\r
-                       }\r
-                       sql.append(sql2);\r
-                       sql.append(");");\r
-                       // System.out.println(sql.toString());\r
-                       stat = db.prepareStatement(sql.toString());\r
-                       stat.setString(1, id);\r
-                       for(String key:xmlKeys) {\r
-                               \r
-                       }\r
-               }\r
                NodeList nodes = root.getChildNodes();\r
                for( int i=0; i<nodes.getLength(); i++) {\r
                        Node n = nodes.item(i);\r
@@ -237,7 +175,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                        NodeList subnodes = n.getChildNodes();\r
                        for( int j=0; j<subnodes.getLength(); j++) {\r
                                try {\r
-                                       handleNode(prog, date, stat, subnodes.item(j));\r
+                                       handleNode(prog, date, subnodes.item(j));\r
                                } catch (RTLException e) {\r
                                        System.out.println(e.getMessage());\r
                                        Transformer t = TransformerFactory.newInstance().newTransformer();\r
@@ -246,15 +184,11 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                                        continue;\r
                                }\r
                        }\r
-                       //System.out.println(stat.toString());\r
-                       if (db != null) {\r
-                               stat.execute();\r
-                       }\r
                }\r
        }\r
 \r
        \r
-       private void handleNode(Programme prog, Date date, PreparedStatement stat, Node n) throws RTLException, DOMException, SQLException {\r
+       private void handleNode(Programme prog, Date date, Node n) throws RTLException, DOMException, SQLException {\r
                if (n.getNodeType() != Node.ELEMENT_NODE) {\r
                        throw new RTLException("Ignoring non-element node " + n.getNodeName());\r
                }\r
@@ -270,9 +204,6 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                        }\r
                }\r
                Element e = (Element)n;\r
-               if (db != null) {\r
-                       stat.setString(xmlKeyMap.get(e.getTagName())+1, e.getTextContent());\r
-               }\r
                String tag = e.getTagName();\r
                if (e.getTextContent().isEmpty()) {\r
                        return;\r
@@ -322,7 +253,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
                List<Programme> result = new LinkedList<Programme>();\r
                Map<String,Channel> channelMap = new HashMap<String,Channel>();\r
                for(Channel c: channels) {\r
-                       if (c.enabled && c.source==EPGSourceFactory.CHANNEL_SOURCE_RTL) channelMap.put(c.id, c);\r
+                       if (c.enabled && c.source==getId()) channelMap.put(c.id, c);\r
                }\r
                URL url = programmeUrl(day);\r
                //String xmltext = fetchURL(url);\r
@@ -373,8 +304,8 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
 \r
        public void close() throws FileNotFoundException, IOException {\r
                super.close();\r
-               cache.close();\r
        }\r
+\r
        private Date parseTime(Date date, String time) {\r
                Calendar result = Calendar.getInstance();\r
                result.setTime(date);\r
@@ -404,7 +335,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
         */\r
        public static void main(String[] args) {\r
                Config config = Config.getDefaultConfig();\r
-               RTL rtl = new RTL(config, false);\r
+               RTL rtl = new RTL(config);\r
                try {\r
                        List<Channel> channels = rtl.getChannels();\r
                        System.out.println("Channels: " + channels);\r
index 3668db2afb3c205964f72a22715ac317ae49ff1f..92d223992f8ca4a25c4ad69552f294b7c1806407 100644 (file)
@@ -62,14 +62,10 @@ public class TvGids extends AbstractEPGSource implements EPGSource {
                super(config);
        }
        
-       public int getId() {
-        return EPGSourceFactory.CHANNEL_SOURCE_TVGIDS;
-    }
+       public String getName() {
+               return "tvgids.nl";
+       }
        
-    public String getName() {
-       return EPGSourceFactory.getChannelSourceName(getId());
-    }
        public static URL programmeUrl(List<Channel> channels, int day) throws Exception {
                StringBuilder s = new StringBuilder(programme_base_url);
                if (channels.size() < 1) {
@@ -166,7 +162,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource {
                        int id = zender.getInt("id");
                        String name = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(zender.getString("name"));
                        String icon = "http://tvgidsassets.nl/img/channels/53x27/" + id + ".png"; 
-                       Channel c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_TVGIDS, Integer.toString(id), name, icon); 
+                       Channel c = Channel.getChannel(getId(), Integer.toString(id), name, icon); 
                        result.add(c);
                }
 
index fb70c7b5d0c185dcc792f174574c157be1e8d0cc..2efb1d944765eac47d505b7c870a008211945baa 100644 (file)
@@ -1,4 +1,6 @@
 project.version=${project.version}
 project.name=${project.name}
 project.description=${project.description}
-build.time=${build.time}
\ No newline at end of file
+build.time=${build.time}
+org.vanbest.xmltv.epgsource.impl.1=org.vanbest.xmltv.TvGids
+org.vanbest.xmltv.epgsource.impl.2=org.vanbest.xmltv.RTL
\ No newline at end of file