]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Factoring out EPGSource class
authorJan-Pascal van Best <janpascal@vanbest.org>
Mon, 26 Mar 2012 15:18:34 +0000 (17:18 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Mon, 26 Mar 2012 15:18:34 +0000 (17:18 +0200)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java [new file with mode: 0644]
src/main/java/org/vanbest/xmltv/EPGSource.java [new file with mode: 0644]
src/main/java/org/vanbest/xmltv/Main.java
src/main/java/org/vanbest/xmltv/RTL.java
src/main/java/org/vanbest/xmltv/TvGids.java

diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
new file mode 100644 (file)
index 0000000..8996bec
--- /dev/null
@@ -0,0 +1,38 @@
+package org.vanbest.xmltv;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Set;
+
+import org.vanbest.xmltv.EPGSource.Stats;
+
+public abstract class AbstractEPGSource implements EPGSource {
+
+       protected Config config;
+       protected ProgrammeCache cache;
+       protected Stats stats = new Stats();
+
+       public AbstractEPGSource(Config config) {
+               this.config = config;
+               cache = new ProgrammeCache(config.cacheFile);
+       }
+
+       public Set<Programme> getProgrammes(Channel channel, int day, boolean fetchDetails)
+                       throws Exception {
+                               ArrayList<Channel> list = new ArrayList<Channel>(2);
+                               list.add(channel);
+                               return getProgrammes(list, day, fetchDetails);
+                       }
+
+       @Override
+       public Stats getStats() {
+               return stats;
+       }
+
+       @Override
+       public void close() throws FileNotFoundException, IOException {
+               cache.close();
+       }
+
+}
\ No newline at end of file
diff --git a/src/main/java/org/vanbest/xmltv/EPGSource.java b/src/main/java/org/vanbest/xmltv/EPGSource.java
new file mode 100644 (file)
index 0000000..1697f45
--- /dev/null
@@ -0,0 +1,29 @@
+package org.vanbest.xmltv;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+
+public interface EPGSource {
+       
+       public class Stats {
+               int fetchErrors = 0;
+               int cacheHits = 0;
+               int cacheMisses = 0;
+       }
+
+       public abstract void close() throws FileNotFoundException, IOException;
+
+       public abstract List<Channel> getChannels();
+
+       // Convenience method
+       public abstract Set<Programme> getProgrammes(Channel channel, int day,
+                       boolean fetchDetails) throws Exception;
+
+       public abstract Set<Programme> getProgrammes(List<Channel> channels,
+                       int day, boolean fetchDetails) throws Exception;
+       
+       public abstract Stats getStats();
+
+}
\ No newline at end of file
index 750f42a6df0a0e4bc08c5a583f0b03f8f5e24239..e94bf4defc18c2ce83b11340cdf759944b9d8ce5 100644 (file)
@@ -73,7 +73,7 @@ public class Main {
                XmlTvWriter writer = new XmlTvWriter(outputWriter, config);
                writer.writeChannels(config.channels);
 
-               TvGids gids = new TvGids(config);
+               EPGSource gids = new TvGids(config);
 
                for (int day=offset; day<offset+days; day++) {
                        if (!config.quiet) System.out.print("Fetching information for day " + day);
@@ -100,14 +100,15 @@ public class Main {
 
                writer.close();
                if (!config.quiet) {
-                       System.out.println("Number of programmes from cache: " + gids.cacheHits);
-                       System.out.println("Number of programmes fetched: " + gids.cacheMisses);
-                       System.out.println("Number of fetch errors: " + gids.fetchErrors);
+                       EPGSource.Stats stats = gids.getStats();
+                       System.out.println("Number of programmes from cache: " + stats.cacheHits);
+                       System.out.println("Number of programmes fetched: " + stats.cacheMisses);
+                       System.out.println("Number of fetch errors: " + stats.fetchErrors);
                }
        }
        
        public void configure() throws IOException {
-               TvGids gids = new TvGids(config);
+               EPGSource gids = new TvGids(config);
                
                Set<String> oldChannels = new HashSet<String>();
                for (Channel c: config.channels) {
index daa901a0497e5028478cf68ea0ef1d9044c9ec92..e6241a3b57922bceb896332c8ee951d6bf838ac1 100644 (file)
@@ -1,6 +1,7 @@
 package org.vanbest.xmltv;\r
 \r
 import java.io.BufferedReader;\r
+import java.io.FileNotFoundException;\r
 import java.io.IOException;\r
 import java.io.InputStreamReader;\r
 import java.net.MalformedURLException;\r
@@ -10,6 +11,7 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
+import java.util.Set;\r
 \r
 import javax.xml.parsers.DocumentBuilderFactory;\r
 import javax.xml.parsers.ParserConfigurationException;\r
@@ -21,23 +23,36 @@ import net.sf.json.JSON;
 import net.sf.json.JSONArray;\r
 import net.sf.json.JSONObject;\r
 \r
+import org.vanbest.xmltv.EPGSource.Stats;\r
 import org.w3c.dom.Document;\r
 import org.w3c.dom.Element;\r
 import org.xml.sax.SAXException;\r
 \r
-public class RTL {\r
+public class RTL implements EPGSource {\r
 \r
        static final String programme_url="http://www.rtl.nl/active/epg_data/dag_data/";\r
        static final String detail_url="http://www.rtl.nl/active/epg_data/uitzending_data/";\r
        static final String icon_url="http://www.rtl.nl/service/gids/components/vaste_componenten/";\r
        \r
-       int fetchErrors = 0;\r
+       Stats stats = new Stats();\r
 \r
-       public List<Channel> getChannels() throws Exception {\r
+       public List<Channel> getChannels() {\r
                List<Channel> result = new ArrayList<Channel>(10);\r
 \r
-               URL url = new URL(programme_url+"1");\r
-               Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());\r
+               URL url = null;\r
+               try {\r
+                       url = new URL(programme_url+"1");\r
+               } catch (MalformedURLException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               }\r
+               Document xml = null;\r
+               try {\r
+                       xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());\r
+               } catch (SAXException | IOException | ParserConfigurationException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               }\r
                Element root = xml.getDocumentElement();\r
                String json = root.getTextContent();\r
                JSONObject o = JSONObject.fromObject( json );\r
@@ -61,7 +76,7 @@ public class RTL {
                        String s;\r
                        while ((s = reader.readLine()) != null) buf.append(s);\r
                } catch (IOException e) {\r
-                       fetchErrors++;\r
+                       stats.fetchErrors++;\r
                        throw new Exception("Error getting program data from url " + url, e);\r
                }\r
                return buf.toString();  \r
@@ -133,6 +148,32 @@ public class RTL {
        }\r
 \r
 \r
+       @Override\r
+       public void close() throws FileNotFoundException, IOException {\r
+               // TODO Auto-generated method stub\r
+               \r
+       }\r
+\r
+       @Override\r
+       public Set<Programme> getProgrammes(Channel channel, int day,\r
+                       boolean fetchDetails) throws Exception {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public Set<Programme> getProgrammes(List<Channel> channels, int day,\r
+                       boolean fetchDetails) throws Exception {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public Stats getStats() {\r
+               // TODO Auto-generated method stub\r
+               return stats;\r
+       }\r
+\r
        /**\r
         * @param args\r
         */\r
@@ -157,4 +198,5 @@ public class RTL {
                }\r
        }\r
 \r
+\r
 }\r
index ee79ed869cc8a2f472bd05fa29fb2524969c366a..d7899dfa26e4e2317eb25b0ddaa9c80b01f7a8ad 100644 (file)
@@ -18,7 +18,6 @@ package org.vanbest.xmltv;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.MalformedURLException;
@@ -42,23 +41,18 @@ import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 import net.sf.json.util.JSONUtils;
 
-public class TvGids {
+public class TvGids extends AbstractEPGSource implements EPGSource {
 
        static String channels_url="http://www.tvgids.nl/json/lists/channels.php";
        static String programme_base_url="http://www.tvgids.nl/json/lists/programs.php";
        static String detail_base_url = "http://www.tvgids.nl/json/lists/program.php";
        static String html_detail_base_url = "http://www.tvgids.nl/programma/";
 
-       Config config;
-       ProgrammeCache cache;
        static boolean initialised = false;
-       int fetchErrors = 0;
-       int cacheHits = 0;
-       int cacheMisses = 0;
        
        public TvGids(Config config) {
+               super(config);
                this.config = config;
-               cache = new ProgrammeCache(config.cacheFile);
                if ( ! initialised ) {
                        init();
                        initialised = true;
@@ -83,10 +77,10 @@ public class TvGids {
                }, true);
        }
        
-       public void close() throws FileNotFoundException, IOException {
-               cache.close();
-       }
-
+       /* (non-Javadoc)
+        * @see org.vanbest.xmltv.EPGSource#getChannels()
+        */
+       @Override
        public List<Channel> getChannels() {
                List<Channel> result = new ArrayList<Channel>(10);
                URL url = null;
@@ -165,13 +159,10 @@ public class TvGids {
                return new URL(s.toString());
        }
        
-       // Convenience method
-       public Set<Programme> getProgrammes(Channel channel, int day, boolean fetchDetails) throws Exception {
-               ArrayList<Channel> list = new ArrayList<Channel>(2);
-               list.add(channel);
-               return getProgrammes(list, day, fetchDetails);
-       }
-               
+       /* (non-Javadoc)
+        * @see org.vanbest.xmltv.EPGSource#getProgrammes(java.util.List, int, boolean)
+        */
+       @Override
        public Set<Programme> getProgrammes(List<Channel> channels, int day, boolean fetchDetails) throws Exception {
                Set<Programme> result = new HashSet<Programme>();
                URL url = programmeUrl(channels, day);
@@ -223,7 +214,7 @@ public class TvGids {
                        String s;
                        while ((s = reader.readLine()) != null) buf.append(s);
                } catch (IOException e) {
-                       fetchErrors++;
+                       stats.fetchErrors++;
                        throw new Exception("Error getting program data from url " + url, e);
                }
                return buf.toString();  
@@ -243,7 +234,7 @@ public class TvGids {
 
                p.details = cache.getDetails(p.db_id);
                if ( p.details == null ) {
-                       cacheMisses++;
+                       stats.cacheMisses++;
                        
                        URL url = JSONDetailUrl(p.db_id);
                        JSONObject json = fetchJSON(url);
@@ -306,7 +297,7 @@ public class TvGids {
                        p.details.fixup(p, config.quiet);
                        cache.add(p.db_id, p.details);
                } else {
-                       cacheHits++;
+                       stats.cacheHits++;
                }
        }
 }