--- /dev/null
+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
--- /dev/null
+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
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);
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) {
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
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
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
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
}\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
}\r
}\r
\r
+\r
}\r
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
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;
}, 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;
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);
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();
p.details = cache.getDetails(p.db_id);
if ( p.details == null ) {
- cacheMisses++;
+ stats.cacheMisses++;
URL url = JSONDetailUrl(p.db_id);
JSONObject json = fetchJSON(url);
p.details.fixup(p, config.quiet);
cache.add(p.db_id, p.details);
} else {
- cacheHits++;
+ stats.cacheHits++;
}
}
}