From: Jan-Pascal van Best <janpascal@vanbest.org>
Date: Mon, 26 Mar 2012 15:18:34 +0000 (+0200)
Subject: Factoring out EPGSource class
X-Git-Tag: 0.9.0~21
X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=ff5759d3c6dd4d54e8763fb7a37a9eee77c8ff49;p=tv_grab_nl_java

Factoring out EPGSource class
---

diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
new file mode 100644
index 0000000..8996bec
--- /dev/null
+++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
@@ -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
index 0000000..1697f45
--- /dev/null
+++ b/src/main/java/org/vanbest/xmltv/EPGSource.java
@@ -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
diff --git a/src/main/java/org/vanbest/xmltv/Main.java b/src/main/java/org/vanbest/xmltv/Main.java
index 750f42a..e94bf4d 100644
--- a/src/main/java/org/vanbest/xmltv/Main.java
+++ b/src/main/java/org/vanbest/xmltv/Main.java
@@ -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) {
diff --git a/src/main/java/org/vanbest/xmltv/RTL.java b/src/main/java/org/vanbest/xmltv/RTL.java
index daa901a..e6241a3 100644
--- a/src/main/java/org/vanbest/xmltv/RTL.java
+++ b/src/main/java/org/vanbest/xmltv/RTL.java
@@ -1,6 +1,7 @@
 package org.vanbest.xmltv;
 
 import java.io.BufferedReader;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.MalformedURLException;
@@ -10,6 +11,7 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Set;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -21,23 +23,36 @@ import net.sf.json.JSON;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 
+import org.vanbest.xmltv.EPGSource.Stats;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
-public class RTL {
+public class RTL implements EPGSource {
 
 	static final String programme_url="http://www.rtl.nl/active/epg_data/dag_data/";
 	static final String detail_url="http://www.rtl.nl/active/epg_data/uitzending_data/";
 	static final String icon_url="http://www.rtl.nl/service/gids/components/vaste_componenten/";
 	
-	int fetchErrors = 0;
+	Stats stats = new Stats();
 
-	public List<Channel> getChannels() throws Exception {
+	public List<Channel> getChannels() {
 		List<Channel> result = new ArrayList<Channel>(10);
 
-		URL url = new URL(programme_url+"1");
-		Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
+		URL url = null;
+		try {
+			url = new URL(programme_url+"1");
+		} catch (MalformedURLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		Document xml = null;
+		try {
+			xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
+		} catch (SAXException | IOException | ParserConfigurationException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 		Element root = xml.getDocumentElement();
 		String json = root.getTextContent();
 		JSONObject o = JSONObject.fromObject( json );
@@ -61,7 +76,7 @@ public class RTL {
 			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();  
@@ -133,6 +148,32 @@ public class RTL {
 	}
 
 
+	@Override
+	public void close() throws FileNotFoundException, IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public Set<Programme> getProgrammes(Channel channel, int day,
+			boolean fetchDetails) throws Exception {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Set<Programme> getProgrammes(List<Channel> channels, int day,
+			boolean fetchDetails) throws Exception {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Stats getStats() {
+		// TODO Auto-generated method stub
+		return stats;
+	}
+
 	/**
 	 * @param args
 	 */
@@ -157,4 +198,5 @@ public class RTL {
 		}
 	}
 
+
 }
diff --git a/src/main/java/org/vanbest/xmltv/TvGids.java b/src/main/java/org/vanbest/xmltv/TvGids.java
index ee79ed8..d7899df 100644
--- a/src/main/java/org/vanbest/xmltv/TvGids.java
+++ b/src/main/java/org/vanbest/xmltv/TvGids.java
@@ -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++;
 		}
 	}
 }