From: Jan-Pascal van Best Date: Mon, 2 Apr 2012 15:27:39 +0000 (+0200) Subject: Change to generic tvgids epg source X-Git-Tag: 0.9.0~1 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=bc85bf0339aaea1a2d74168b7cc1a75869fe8808;p=tv_grab_nl_java Change to generic tvgids epg source - Refactor EPGSource to be generic (not TvGids-specific) - Add timestamp to stdout and xmltv output - Use generic TvGids instead of legacy classes - new config file format 4 - store hsqldb database location in config file - add --clear-cache option - allow nice-time-milliseconds to be set using --configure --- diff --git a/pom.xml b/pom.xml index ddb98c1..f13bcdd 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,29 @@ + + org.apache.maven.plugins + maven-antrun-plugin + + + generate-resources + + run + + + + + + + + + + + + + + @@ -76,7 +98,9 @@ true - + + ${basedir}/target/filter.properties + diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java index ef40f5b..424ed0b 100644 --- a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java +++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; +import java.util.List; import java.util.Set; import org.vanbest.xmltv.EPGSource.Stats; @@ -13,17 +14,17 @@ import org.vanbest.xmltv.EPGSource.Stats; public abstract class AbstractEPGSource implements EPGSource { protected Config config; - protected TvGidsProgrammeCache cache; + protected ProgrammeCache cache; protected Stats stats = new Stats(); public static final int MAX_FETCH_TRIES=5; public AbstractEPGSource(Config config) { this.config = config; - cache = new TvGidsProgrammeCache(config.cacheFile); + cache = new ProgrammeCache(config); } - public Set getProgrammes(Channel channel, int day, boolean fetchDetails) + public List getProgrammes(Channel channel, int day, boolean fetchDetails) throws Exception { ArrayList list = new ArrayList(2); list.add(channel); @@ -64,6 +65,10 @@ public abstract class AbstractEPGSource implements EPGSource { } return buf.toString(); } - - + + public void clearCache() { + ProgrammeCache cache = new ProgrammeCache(config); + cache.clear(); + cache.close(); + } } \ No newline at end of file diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java index 3a9544d..c5bfcc5 100644 --- a/src/main/java/org/vanbest/xmltv/Config.java +++ b/src/main/java/org/vanbest/xmltv/Config.java @@ -36,25 +36,30 @@ import java.util.Properties; import org.apache.commons.io.FileUtils; public class Config { + //constants + public static final int LOG_INFO = 0x0001; + public static final int LOG_JSON = 0x0100; + private final static int LOG_PROGRAMME_INFO = 0x0200; + private final static int CURRENT_FILE_FORMAT=4; + public final static int LOG_DEFAULT = LOG_INFO; + + // in config file public int niceMilliseconds; public List channels; public Map cattrans; - protected File cacheFile; public String cacheDbHandle; public String cacheDbUser; public String cacheDbPassword; - boolean quiet = false; - public int logLevel = LOG_DEFAULT; - public static final int LOG_INFO = 0x0001; - public static final int LOG_JSON = 0x0100; - private static final int LOG_PROGRAMME_INFO = 0x0200; + // not stored (yet) + public boolean joinKijkwijzerRatings = true; - public static int LOG_DEFAULT = LOG_INFO; + // command-line options + boolean quiet = false; + public int logLevel = LOG_DEFAULT; - private final static int CURRENT_FILE_FORMAT=3; - String project_version; + String build_time; private Config() { Properties configProp = new Properties(); @@ -65,15 +70,16 @@ public class Config { e.printStackTrace(); } project_version=configProp.getProperty("project.version"); + build_time=configProp.getProperty("build.time"); } public static Config getDefaultConfig() { Config result = new Config(); result.channels = new ArrayList(); result.cattrans = getDefaultCattrans(); - result.cacheFile = defaultCacheFile(); result.niceMilliseconds = 500; - result.cacheDbHandle = "jdbc:hsqldb:file:cachedb"; // FIXME in userdir + String cachefile = FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv", "tv_grab_nl_java.cache").getPath(); + result.setCacheFile(cachefile); result.cacheDbUser = "SA"; result.cacheDbPassword = ""; return result; @@ -127,7 +133,9 @@ public class Config { FileUtils.forceMkdir(configFile.getParentFile()); PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( configFile ))); out.println("config-file-format: " + CURRENT_FILE_FORMAT); - out.println("cache-file: " + escape(cacheFile.getPath())); + out.println("cache-db-handle: " + escape(cacheDbHandle)); + out.println("cache-db-user: " + escape(cacheDbUser)); + out.println("cache-db-password: " + escape(cacheDbPassword)); out.println("nice-time-milliseconds: " + niceMilliseconds); for(Channel c: channels) { // FIXME: handle multiple channels names, icons and urls @@ -226,6 +234,7 @@ public class Config { break; case 2: case 3: + case 4: int source; if (fileformat==2) { source = Integer.parseInt(parts.get(1)); @@ -260,7 +269,20 @@ public class Config { fileformat = CURRENT_FILE_FORMAT; } } else if (key.equals("cache-file")) { - result.cacheFile = new File(parts.get(1)); + if (fileformat<4) { + String cacheFile = parts.get(1); + result.cacheDbHandle = "jdbc:hsqldb:file:"+cacheFile; + result.cacheDbUser = "SA"; + result.cacheDbPassword = ""; + } else { + System.out.println("Illegal key cache-file in config file!"); + } + } else if (key.equals("cache-db-handle")) { + result.cacheDbHandle = parts.get(1); + } else if (key.equals("cache-db-user")) { + result.cacheDbUser = parts.get(1); + } else if (key.equals("cache-db-password")) { + result.cacheDbPassword = parts.get(1); } else if (key.equals("nice-time-milliseconds")) { result.niceMilliseconds = Integer.parseInt(parts.get(1)); } else { @@ -283,5 +305,9 @@ public class Config { return (logLevel & LOG_PROGRAMME_INFO) != 0; } + public void setCacheFile(String cacheFile) { + cacheDbHandle = "jdbc:hsqldb:file:"+cacheFile; + } + } diff --git a/src/main/java/org/vanbest/xmltv/EPGSource.java b/src/main/java/org/vanbest/xmltv/EPGSource.java index 30443e1..aa6a697 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSource.java +++ b/src/main/java/org/vanbest/xmltv/EPGSource.java @@ -18,12 +18,13 @@ public interface EPGSource { public abstract List getChannels(); // Convenience method - public abstract Set getProgrammes(Channel channel, int day, + public abstract List getProgrammes(Channel channel, int day, boolean fetchDetails) throws Exception; - public abstract Set getProgrammes(List channels, + public abstract List getProgrammes(List channels, int day, boolean fetchDetails) throws Exception; public abstract Stats getStats(); - + + public void clearCache(); } \ 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 dfdc4d6..be8ea28 100644 --- a/src/main/java/org/vanbest/xmltv/Main.java +++ b/src/main/java/org/vanbest/xmltv/Main.java @@ -28,10 +28,14 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import javax.xml.stream.FactoryConfigurationError; +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; @@ -48,6 +52,7 @@ public class Main { private PrintStream outputWriter; private int days = 5; private int offset = 0; + private boolean clearCache = false; /** * @param args */ @@ -56,37 +61,55 @@ public class Main { this.configFile = defaultConfigFile(); this.outputWriter = System.out; } - + + public void showHeader() { + System.out.println("tv_grab_nl_java version "+config.project_version + " (built "+config.build_time+")"); + System.out.println("Copyright (C) 2012 Jan-Pascal van Best "); + System.out.println("tv_grab_nl_java comes with ABSOLUTELY NO WARRANTY. It is free software, and you are welcome to redistribute it"); + System.out.println("under certain conditions; `tv_grab_nl_java --license' for details."); + } public void run() throws FactoryConfigurationError, Exception { if (!config.quiet) { - System.out.println("tv_grab_nl_java version "+config.project_version + ", Copyright (C) 2012 Jan-Pascal van Best "); - System.out.println("tv_grab_nl_java comes with ABSOLUTELY NO WARRANTY. It is free software, and you are welcome to redistribute it"); - System.out.println("under certain conditions; `tv_grab_nl_java --license' for details."); - + showHeader(); System.out.println("Fetching programme data for " + this.days + " starting from day " + this.offset); int enabledCount = 0; for(Channel c: config.channels) { if (c.enabled) enabledCount++; } System.out.println("... from " + enabledCount + " channels"); - System.out.println("... using cache file " + config.cacheFile.getCanonicalPath()); + System.out.println("... using cache at " + config.cacheDbHandle); } - XmlTvWriter writer = new XmlTvWriter(outputWriter, config); - writer.writeChannels(config.channels); + EPGSource gids = new TvGids(config); + if (clearCache) gids.clearCache(); + + XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputWriter); + writer.writeStartDocument(); + writer.writeCharacters("\n"); + writer.writeDTD(""); + writer.writeCharacters("\n"); + writer.writeStartElement("tv"); + writer.writeAttribute("generator-info-url","http://github.com/janpascal/tv_grab_nl_java"); + writer.writeAttribute("source-info-url", "http://tvgids.nl/"); + writer.writeAttribute("source-info-name", "TvGids.nl"); + writer.writeAttribute("generator-info-name", "tv_grab_nl_java release "+config.project_version + ", built " + config.build_time); - EPGSource gids = new TvGidsLegacy(config); + for(Channel c: config.channels) if (c.enabled) c.serialize(writer); for (int day=offset; day programmes = new HashSet(); for(Channel c: config.channels) { if (!c.enabled) continue; if (!config.quiet) System.out.print("."); - Set p = gids.getProgrammes(c, day, true); - writer.writePrograms(p); + List programmes = gids.getProgrammes(c, day, true); + for (Programme p: programmes) p.serialize(writer); writer.flush(); } if (!config.quiet) System.out.println(); } + + writer.writeEndElement(); + writer.writeEndDocument(); + writer.flush(); + writer.close(); try { gids.close(); @@ -98,7 +121,6 @@ public class Main { e.printStackTrace(); } - writer.close(); if (!config.quiet) { EPGSource.Stats stats = gids.getStats(); System.out.println("Number of programmes from cache: " + stats.cacheHits); @@ -108,7 +130,9 @@ public class Main { } public void configure() throws IOException { - EPGSource gids = new TvGidsLegacy(config); + showHeader(); + + EPGSource gids = new TvGids(config); Set oldChannels = new HashSet(); for (Channel c: config.channels) { @@ -119,6 +143,27 @@ public class Main { List channels = gids.getChannels(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + + System.out.print("Delay between each request to the server (in milliseconds, default="+config.niceMilliseconds+"):"); + while(true) { + String s = reader.readLine().toLowerCase(); + if (s.isEmpty()) { + break; + } + try { + config.niceMilliseconds = Integer.parseInt(s); + break; + } catch (NumberFormatException e) { + System.out.println("\""+s+"\" is not a valid number, please try again"); + continue; + } + } + + // TODO configure cache location + // public String cacheDbHandle; + // public String cacheDbUser; + // public String cacheDbPassword; + boolean all = false; boolean none = false; boolean keep = false; @@ -243,6 +288,10 @@ public class Main { .hasArg() .withDescription("Cache file location") .create()) + .addOption(OptionBuilder + .withLongOpt("clear-cache") + .withDescription("Clear cache, remove all cached info") + .create()) .addOption(OptionBuilder .withLongOpt("help") .withDescription("Show this help") @@ -266,9 +315,10 @@ public class Main { e.printStackTrace(); } - if(line.hasOption("license")) { - showLicense(); - System.exit(0); + if(line.hasOption("license")) { + showHeader(); + showLicense(); + System.exit(0); } if(line.hasOption("config-file")) { configFile = new File(line.getOptionValue("config-file")); @@ -278,7 +328,8 @@ public class Main { config.quiet = true; } if (line.hasOption("description")) { - System.out.println("tv_grab_nl_java version " + config.project_version); + showHeader(); + System.out.println(); System.out.println("tv_grab_nl_java is a parser for Dutch TV listings using the tvgids.nl JSON interface"); System.exit(0); } @@ -295,7 +346,10 @@ public class Main { config.logLevel = Integer.parseInt(line.getOptionValue("log-level")); } if (line.hasOption("cache")) { - config.cacheFile = new File(line.getOptionValue("cache")); + config.setCacheFile(line.getOptionValue("cache")); + } + if (line.hasOption("clear-cache")) { + clearCache = true; } if (line.hasOption("days")) { this.days = Integer.parseInt(line.getOptionValue("days")); diff --git a/src/main/java/org/vanbest/xmltv/Programme.java b/src/main/java/org/vanbest/xmltv/Programme.java index f7cd8d4..8c1d787 100644 --- a/src/main/java/org/vanbest/xmltv/Programme.java +++ b/src/main/java/org/vanbest/xmltv/Programme.java @@ -206,6 +206,7 @@ public class Programme implements Serializable { previouslyShown.channel = channel; } public boolean hasCategory(String category) { + if(categories==null) return false; for(Title t: categories) { if (t.title.toLowerCase().equals(category)) return true; } @@ -216,6 +217,7 @@ public class Programme implements Serializable { Rating r = new Rating(); r.system = system; r.value = value; + ratings.add(r); } diff --git a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java index dda3dde..cdfdbff 100644 --- a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java +++ b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java @@ -44,6 +44,7 @@ public class ProgrammeCache { private PreparedStatement getStatement; private PreparedStatement putStatement; private PreparedStatement removeStatement; + private PreparedStatement clearStatement; public ProgrammeCache(Config config) { this.config = config; @@ -56,6 +57,7 @@ public class ProgrammeCache { getStatement = db.prepareStatement("SELECT programme FROM cache WHERE id=?"); putStatement = db.prepareStatement("INSERT INTO cache VALUES (?,?,?)"); removeStatement = db.prepareStatement("DELETE FROM cache WHERE id=?"); + clearStatement = db.prepareStatement("DELETE FROM cache"); } catch (SQLException e) { db = null; if (!config.quiet) { @@ -117,7 +119,7 @@ public class ProgrammeCache { try { stat = db.createStatement(); int count = stat.executeUpdate("DELETE FROM cache WHERE date0) { System.out.println("Purged " + count + " old entries from cache"); } stat.close(); @@ -127,7 +129,19 @@ public class ProgrammeCache { } } - public void close() throws FileNotFoundException, IOException { + public void clear() { + try { + int count = clearStatement.executeUpdate(); + if (!config.quiet && count>0) { + System.out.println("Cleared " + count + " entries from cache"); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void close() { cleanup(); if (db != null) { try { diff --git a/src/main/java/org/vanbest/xmltv/RTL.java b/src/main/java/org/vanbest/xmltv/RTL.java index 86d8495..0881bcf 100644 --- a/src/main/java/org/vanbest/xmltv/RTL.java +++ b/src/main/java/org/vanbest/xmltv/RTL.java @@ -294,7 +294,8 @@ public class RTL extends AbstractEPGSource implements EPGSource { //prog.endTime = parseTime(date, root.) } - public List getProgrammes1(List channels, int day, + @Override + public List getProgrammes(List channels, int day, boolean fetchDetails) throws Exception { List result = new LinkedList(); Map channelMap = new HashMap(); @@ -393,8 +394,8 @@ public class RTL extends AbstractEPGSource implements EPGSource { writer.writeStartElement("tv"); for(Channel c: channels) {c.serialize(writer);} writer.flush(); - List programmes = rtl.getProgrammes1(channels.subList(6, 9), 0, true); - //List programmes = rtl.getProgrammes1(channels, 0, true); + List programmes = rtl.getProgrammes(channels.subList(6, 9), 0, true); + //List programmes = rtl.getProgrammes(channels, 0, true); for(Programme p: programmes) {p.serialize(writer);} writer.writeEndElement(); writer.writeEndDocument(); @@ -412,12 +413,4 @@ public class RTL extends AbstractEPGSource implements EPGSource { } } - @Override - public Set getProgrammes(List channels, int day, - boolean fetchDetails) throws Exception { - // TODO Refactor EPGSource to return Programme instead of TvGidsProgramme - return null; - } - - } diff --git a/src/main/java/org/vanbest/xmltv/TvGids.java b/src/main/java/org/vanbest/xmltv/TvGids.java index bd4d142..70fcf1b 100644 --- a/src/main/java/org/vanbest/xmltv/TvGids.java +++ b/src/main/java/org/vanbest/xmltv/TvGids.java @@ -39,6 +39,7 @@ import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang.StringUtils; import net.sf.ezmorph.MorpherRegistry; import net.sf.ezmorph.ObjectMorpher; @@ -55,37 +56,10 @@ public class TvGids extends AbstractEPGSource implements EPGSource { static String detail_base_url = "http://www.tvgids.nl/json/lists/program.php"; static String html_detail_base_url = "http://www.tvgids.nl/programma/"; - static boolean initialised = false; - private static final int MAX_PROGRAMMES_PER_DAY = 9999; - private ProgrammeCache cache; - public TvGids(Config config) { super(config); - cache = new ProgrammeCache(config); - if ( ! initialised ) { - init(); - initialised = true; - } - } - - public static void init() { - String[] formats = {"yyyy-MM-dd HH:mm:ss"}; - MorpherRegistry registry = JSONUtils.getMorpherRegistry(); - registry.registerMorpher( new DateMorpher(formats, new Locale("nl"))); - registry.registerMorpher( new ObjectMorpher() { - public Object morph(Object value) { - String s = (String) value; - return org.apache.commons.lang.StringEscapeUtils.unescapeHtml(s); - } - public Class morphsTo() { - return String.class; - } - public boolean supports(Class clazz) { - return clazz == String.class; - } - }, true); } public static URL programmeUrl(List channels, int day) throws Exception { @@ -181,8 +155,6 @@ public class TvGids extends AbstractEPGSource implements EPGSource { JSONObject zender = jsonArray.getJSONObject(i); //System.out.println( "id: " + zender.getString("id")); //System.out.println( "name: " + zender.getString("name")); - //TvGidsChannel c = new TvGidsChannel(zender.getInt("id"), zender.getString("name"), zender.getString("name_short")); - //c.setIconUrl("http://tvgidsassets.nl/img/channels/53x27/" + c.id + ".png"); 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"; @@ -202,8 +174,8 @@ public class TvGids extends AbstractEPGSource implements EPGSource { /* (non-Javadoc) * @see org.vanbest.xmltv.EPGSource#getProgrammes(java.util.List, int, boolean) */ - //@Override - public List getProgrammes1(List channels, int day, boolean fetchDetails) throws Exception { + @Override + public List getProgrammes(List channels, int day, boolean fetchDetails) throws Exception { List result = new ArrayList(); URL url = programmeUrl(channels, day); @@ -259,31 +231,38 @@ public class TvGids extends AbstractEPGSource implements EPGSource { if (result == null) { stats.cacheMisses++; result = new Programme(); + // Do this here, because we can only add to these fields. Pity if they're updated + result.addTitle(programme.getString("titel")); + String genre = programme.getString("genre"); + if (genre != null && !genre.isEmpty()) result.addCategory(config.translateCategory(genre)); + String kijkwijzer = programme.getString("kijkwijzer"); + if (kijkwijzer!=null && !kijkwijzer.isEmpty()) { + List list = parseKijkwijzer(kijkwijzer); + if (config.joinKijkwijzerRatings) { + // mythtv doesn't understand multiple tags + result.addRating("kijkwijzer", StringUtils.join(list, ",")); + } else { + for(String rating: list) { + result.addRating("kijkwijzer", rating); + } + } + // TODO add icon from HTML detail page + } } else { + //System.out.println("From cache: " + programme.getString("titel")); stats.cacheHits++; } - System.out.println(" titel:" + programme.getString("titel")); + //System.out.println(" titel:" + programme.getString("titel")); //System.out.println("datum_start:" + programme.getString("datum_start")); //System.out.println(" datum_end:" + programme.getString("datum_end")); //System.out.println(" genre:" + programme.getString("genre")); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("nl")); result.startTime = df.parse(programme.getString("datum_start")); result.endTime = df.parse(programme.getString("datum_end")); - result.addTitle(programme.getString("titel")); - String genre = programme.getString("genre"); - if (genre != null && !genre.isEmpty()) result.addCategory(config.translateCategory(genre)); - String kijkwijzer = programme.getString("kijkwijzer"); - if (kijkwijzer!=null && !kijkwijzer.isEmpty()) { - List list = parseKijkwijzer(kijkwijzer); - for(String s: list) { - result.addRating("kijkwijzer", s); - // TODO add icon from HTML detail page - } - - } // TODO other fields if (fetchDetails && !cached) { + // TODO also read details if those have not been cached fillDetails(id, result); } if (!cached) { @@ -346,6 +325,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { replaceAll("", " "). replaceAll("", " "). trim(); + if (value.isEmpty()) continue ; result.addDescription(value); } else if (key.equals("presentatie")) { String[] parts = value.split(","); @@ -450,15 +430,6 @@ public class TvGids extends AbstractEPGSource implements EPGSource { // result.details.fixup(result, config.quiet); } - @Override - public Set getProgrammes(List channels, int day, - boolean fetchDetails) throws Exception { - // TODO Auto-generated method stub - // dummy, wait for superclass and interface to be generalised - return null; - } - - /** * @param args */ @@ -478,7 +449,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { List my_channels = channels.subList(0,15); for(Channel c: my_channels) {c.serialize(writer);} writer.flush(); - List programmes = gids.getProgrammes1(my_channels, 2, true); + List programmes = gids.getProgrammes(my_channels, 2, true); for(Programme p: programmes) {p.serialize(writer);} writer.writeEndElement(); writer.writeEndDocument(); diff --git a/src/main/java/org/vanbest/xmltv/TvGidsLegacy.java b/src/main/java/org/vanbest/xmltv/TvGidsLegacy.java index f419daa..231fa5d 100644 --- a/src/main/java/org/vanbest/xmltv/TvGidsLegacy.java +++ b/src/main/java/org/vanbest/xmltv/TvGidsLegacy.java @@ -34,6 +34,9 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.vanbest.xmltv.EPGSource.Stats; + + import net.sf.ezmorph.MorpherRegistry; import net.sf.ezmorph.ObjectMorpher; import net.sf.ezmorph.object.DateMorpher; @@ -42,18 +45,24 @@ import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.util.JSONUtils; -public class TvGidsLegacy extends AbstractEPGSource implements EPGSource { +public class TvGidsLegacy { 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/"; + public static final int MAX_FETCH_TRIES=5; + static boolean initialised = false; - + private Config config; + protected Stats stats = new Stats(); + private TvGidsProgrammeCache cache; + public TvGidsLegacy(Config config) { - super(config); this.config = config; + //this.cache = new TvGidsProgrammeCache(config.cacheFile); + this.cache = new TvGidsProgrammeCache(new File("tv_grab_nl_java.cache")); if ( ! initialised ) { init(); initialised = true; @@ -81,7 +90,6 @@ public class TvGidsLegacy extends AbstractEPGSource implements EPGSource { /* (non-Javadoc) * @see org.vanbest.xmltv.EPGSource#getChannels() */ - @Override public List getChannels() { List result = new ArrayList(10); URL url = null; @@ -162,8 +170,7 @@ public class TvGidsLegacy extends AbstractEPGSource implements EPGSource { /* (non-Javadoc) * @see org.vanbest.xmltv.EPGSource#getProgrammes(java.util.List, int, boolean) */ - @Override - public Set getProgrammes(List channels, int day, boolean fetchDetails) throws Exception { + public Set getProgrammes1(List channels, int day, boolean fetchDetails) throws Exception { Set result = new HashSet(); URL url = programmeUrl(channels, day); @@ -206,6 +213,31 @@ public class TvGidsLegacy extends AbstractEPGSource implements EPGSource { return p; } + protected String fetchURL(URL url) throws Exception { + Thread.sleep(config.niceMilliseconds); + StringBuffer buf = new StringBuffer(); + boolean done = false; + IOException finalException = null; + for(int count = 0; count