<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
+ <dependency>
+ <groupId>org.reflections</groupId>
+ <artifactId>reflections</artifactId>
+ <version>0.9.9-RC1</version>
+ </dependency>
</dependencies>
<build>
<plugins>
public abstract class AbstractEPGSource implements EPGSource {
- private int sourceId;
protected Config config;
protected ProgrammeCache cache;
protected Stats stats = new Stats();
public static final int MAX_FETCH_TRIES = 5;
- public AbstractEPGSource(int sourceId, Config config) {
+ public AbstractEPGSource(Config config) {
this.config = config;
- this.sourceId = sourceId;
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);
}
public void clearCache() {
- cache.clear(sourceId);
+ cache.clear(getName());
}
String kijkwijzerCategorie(char c) {
-package org.vanbest.xmltv;\r
-\r
-import java.io.PrintWriter;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import javax.xml.stream.XMLStreamException;\r
-import javax.xml.stream.XMLStreamWriter;\r
-import org.apache.log4j.Logger;\r
-\r
-public class Channel {\r
- String id; // Internal id, used by EPG provider\r
- String xmltv; // xmltv id such as '1.tvgids.nl' or 'Nederland1.horizon.tv'\r
- List<String> names; // at least one name is obligatory\r
- List<Icon> icons;\r
- List<String> urls;\r
- protected boolean enabled = true;\r
- int source;\r
- static Logger logger = Logger.getLogger(Channel.class);\r
-\r
- protected Channel(int source, String id, String xmltv) {\r
- this.id = id;\r
- this.source = source;\r
- this.xmltv = xmltv;\r
- names = new ArrayList<String>();\r
- icons = new ArrayList<Icon>();\r
- urls = new ArrayList<String>();\r
- }\r
-\r
- public String defaultName() {\r
- return names.get(0);\r
- }\r
-\r
- static Channel getChannel(int source, String id, String xmltv, String name) {\r
- Channel c = new Channel(source, id, xmltv);\r
- c.names.add(name);\r
- return c;\r
- }\r
-\r
- // Use default xmltvid with id+"."+sourceName\r
- static Channel getChannel(int source, String id, String name) {\r
- String xmltv = id + "." + EPGSourceFactory.getChannelSourceName(source);\r
- Channel c = new Channel(source, id, xmltv);\r
- c.names.add(name);\r
- return c;\r
- }\r
-/*\r
- static Channel getChannel(int source, String id, String name, String extraConfig) {\r
- Channel c;\r
- if(EPGSourceFactory.getChannelSourceName(source).equals(Horizon.NAME)) {\r
- long horizonId=Long.parseLong(extraConfig);\r
- c = new Horizon.HorizonChannel(source, id, horizonId);\r
- } else {\r
- c = new Channel(source, id);\r
- }\r
- c.names.add(name);\r
- return c;\r
- }\r
-*/\r
- public String getXmltvChannelId() {\r
- return xmltv; // id + "." + getSourceName();\r
- }\r
-\r
- public String getSourceName() {\r
- return EPGSourceFactory.getChannelSourceName(source);\r
- }\r
-\r
- public void serialize(XMLStreamWriter writer, boolean writeLogos) throws XMLStreamException {\r
- writer.writeStartElement("channel");\r
- writer.writeAttribute("id", getXmltvChannelId());\r
- for (String name : names) {\r
- writer.writeStartElement("display-name");\r
- writer.writeAttribute("lang", "nl");\r
- writer.writeCharacters(name);\r
- writer.writeEndElement();\r
- }\r
- if (writeLogos) {\r
- for (Icon i : icons) {\r
- i.serialize(writer);\r
- }\r
- }\r
- for (String url : urls) {\r
- writer.writeStartElement("url");\r
- writer.writeCharacters(url);\r
- writer.writeEndElement();\r
- }\r
- writer.writeEndElement();\r
- writer.writeCharacters(System.getProperty("line.separator"));\r
- }\r
-\r
- // Convenience method\r
- public void addIcon(String url) {\r
- icons.add(new Icon(url));\r
- }\r
-\r
- public void setEnabled(boolean b) {\r
- this.enabled = b;\r
- }\r
-\r
- public String extraConfig() {\r
- return "";\r
- }\r
-\r
- // must start with channel: <sourcename>\r
- public void writeConfig(PrintWriter out) {\r
- // FIXME: handle multiple channels names, icons and urls\r
- out.print("channel: " + getSourceName() + ": "\r
- + Config.escape(xmltv) + ": "\r
- + id + ": "\r
- + (enabled ? "enabled" : "disabled") + ": "\r
- + Config.escape(defaultName()) + ": "\r
- + Config.escape(extraConfig()));\r
- if (!icons.isEmpty()) {\r
- out.print(" : " + Config.escape(icons.get(0).url));\r
- }\r
- out.println();\r
- }\r
-\r
- public static Channel parseConfig(int fileformat, List<String> parts) {\r
- Channel c = null;\r
- switch (fileformat) {\r
- case 0:\r
- c = Channel.getChannel(EPGSourceFactory.newInstance()\r
- .getChannelSourceId("tvgids.nl"), parts.get(1),\r
- parts.get(2));\r
- if (parts.size() > 3) {\r
- c.addIcon(parts.get(3));\r
- }\r
- break;\r
- case 1:\r
- c = Channel.getChannel(EPGSourceFactory.newInstance()\r
- .getChannelSourceId("tvgids.nl"), parts.get(1),\r
- parts.get(3));\r
- if (parts.size() > 4) {\r
- c.addIcon(parts.get(4));\r
- }\r
- String value = parts.get(2);\r
- if (value.equals("enabled")) {\r
- c.setEnabled(true);\r
- } else if (value.equals("disabled")) {\r
- c.setEnabled(false);\r
- } else {\r
- logger.error("Error in config file, unknown channel status \""\r
- + parts.get(2)\r
- + "\", should be enabled or disabled");\r
- }\r
- break;\r
- case 2:\r
- case 3:\r
- case 4:\r
- case 5: {\r
- int source;\r
- if (fileformat == 2) {\r
- source = Integer.parseInt(parts.get(1));\r
- } else {\r
- source = EPGSourceFactory.newInstance()\r
- .getChannelSourceId(parts.get(1));\r
- }\r
- String id = parts.get(2);\r
- String enabled = parts.get(3);\r
- String name = parts.get(4);\r
- if(fileformat<5) {\r
- c = Channel.getChannel(source, id, name);\r
- } else {\r
- String extra = parts.get(5);\r
- if(extra.isEmpty()) {\r
- c = Channel.getChannel(source, id, name);\r
- } else {\r
- // Horizon channel\r
- String xmltv=id+"."+EPGSourceFactory.getChannelSourceName(source);\r
- String horizonId=extra;\r
- c = Channel.getChannel(source, horizonId, xmltv, name);\r
- }\r
- }\r
- int iconPart = (fileformat<5?5:6);\r
- if (parts.size() > iconPart) {\r
- c.addIcon(parts.get(iconPart));\r
- }\r
- if (enabled.equals("enabled")) {\r
- c.setEnabled(true);\r
- } else if (enabled.equals("disabled")) {\r
- c.setEnabled(false);\r
- } else {\r
- logger.error("Error in config file, unknown channel status \""\r
- + enabled \r
- + "\", should be enabled or disabled");\r
- }\r
- break;\r
- }\r
- case 6: {\r
- int source = EPGSourceFactory.getChannelSourceId(parts.get(1));\r
- String xmltv = parts.get(2);\r
- String id = parts.get(3);\r
- String enabled = parts.get(4);\r
- String name = parts.get(5);\r
- String extra = parts.get(6);\r
- c = Channel.getChannel(source, id, xmltv, name);\r
- if (parts.size() > 7) {\r
- c.addIcon(parts.get(7));\r
- }\r
- if (enabled.equals("enabled")) {\r
- c.setEnabled(true);\r
- } else if (enabled.equals("disabled")) {\r
- c.setEnabled(false);\r
- } else {\r
- logger.error("Error in config file, unknown channel status \""\r
- + enabled\r
- + "\", should be enabled or disabled");\r
- }\r
- }\r
- }\r
- return c;\r
- }\r
-\r
- public String toString() {\r
- return "Channel " + source + "::" + id + " (" + defaultName() + ")";\r
- }\r
-}\r
+package org.vanbest.xmltv;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import org.apache.log4j.Logger;
+
+public class Channel {
+ String id; // Internal id, used by EPG provider
+ String xmltv; // xmltv id such as '1.tvgids.nl' or 'Nederland1.horizon.tv'
+ List<String> names; // at least one name is obligatory
+ List<Icon> icons;
+ List<String> urls;
+ protected boolean enabled = true;
+ String source;
+ static Logger logger = Logger.getLogger(Channel.class);
+
+ protected Channel(String source, String id, String xmltv) {
+ this.id = id;
+ this.source = source;
+ this.xmltv = xmltv;
+ names = new ArrayList<String>();
+ icons = new ArrayList<Icon>();
+ urls = new ArrayList<String>();
+ }
+
+ public String defaultName() {
+ return names.get(0);
+ }
+
+ static Channel getChannel(String source, String id, String xmltv, String name) {
+ Channel c = new Channel(source, id, xmltv);
+ c.names.add(name);
+ return c;
+ }
+
+ // Use default xmltvid with id+"."+sourceName
+ static Channel getChannel(String source, String id, String name) {
+ String xmltv = id + "." + source;
+ Channel c = new Channel(source, id, xmltv);
+ c.names.add(name);
+ return c;
+ }
+/*
+ static Channel getChannel(int source, String id, String name, String extraConfig) {
+ Channel c;
+ if(EPGSourceFactory.getChannelSourceName(source).equals(Horizon.NAME)) {
+ long horizonId=Long.parseLong(extraConfig);
+ c = new Horizon.HorizonChannel(source, id, horizonId);
+ } else {
+ c = new Channel(source, id);
+ }
+ c.names.add(name);
+ return c;
+ }
+*/
+ public String getXmltvChannelId() {
+ return xmltv; // id + "." + getSourceName();
+ }
+
+ public String getSourceName() {
+ return source;
+ }
+
+ public void serialize(XMLStreamWriter writer, boolean writeLogos) throws XMLStreamException {
+ writer.writeStartElement("channel");
+ writer.writeAttribute("id", getXmltvChannelId());
+ for (String name : names) {
+ writer.writeStartElement("display-name");
+ writer.writeAttribute("lang", "nl");
+ writer.writeCharacters(name);
+ writer.writeEndElement();
+ }
+ if (writeLogos) {
+ for (Icon i : icons) {
+ i.serialize(writer);
+ }
+ }
+ for (String url : urls) {
+ writer.writeStartElement("url");
+ writer.writeCharacters(url);
+ writer.writeEndElement();
+ }
+ writer.writeEndElement();
+ writer.writeCharacters(System.getProperty("line.separator"));
+ }
+
+ // Convenience method
+ public void addIcon(String url) {
+ icons.add(new Icon(url));
+ }
+
+ public void setEnabled(boolean b) {
+ this.enabled = b;
+ }
+
+ public String extraConfig() {
+ return "";
+ }
+
+ // must start with channel: <sourcename>
+ public void writeConfig(PrintWriter out) {
+ // FIXME: handle multiple channels names, icons and urls
+ out.print("channel: " + getSourceName() + ": "
+ + Config.escape(xmltv) + ": "
+ + id + ": "
+ + (enabled ? "enabled" : "disabled") + ": "
+ + Config.escape(defaultName()) + ": "
+ + Config.escape(extraConfig()));
+ if (!icons.isEmpty()) {
+ out.print(" : " + Config.escape(icons.get(0).url));
+ }
+ out.println();
+ }
+
+ public static Channel parseConfig(int fileformat, List<String> parts) {
+ Channel c = null;
+ switch (fileformat) {
+ case 0:
+ c = Channel.getChannel("tvgids.nl", parts.get(1),
+ parts.get(2));
+ if (parts.size() > 3) {
+ c.addIcon(parts.get(3));
+ }
+ break;
+ case 1:
+ c = Channel.getChannel("tvgids.nl", parts.get(1),
+ parts.get(3));
+ if (parts.size() > 4) {
+ c.addIcon(parts.get(4));
+ }
+ String value = parts.get(2);
+ if (value.equals("enabled")) {
+ c.setEnabled(true);
+ } else if (value.equals("disabled")) {
+ c.setEnabled(false);
+ } else {
+ logger.error("Error in config file, unknown channel status \""
+ + parts.get(2)
+ + "\", should be enabled or disabled");
+ }
+ break;
+ case 2:
+ System.err.println("This config file format is no longer supported...");
+ break;
+ case 3:
+ case 4:
+ case 5: {
+ String source = parts.get(1);
+ String id = parts.get(2);
+ String enabled = parts.get(3);
+ String name = parts.get(4);
+ if(fileformat<5) {
+ c = Channel.getChannel(source, id, name);
+ } else {
+ String extra = parts.get(5);
+ if(extra.isEmpty()) {
+ c = Channel.getChannel(source, id, name);
+ } else {
+ // Horizon channel
+ String xmltv=id+"."+source;
+ String horizonId=extra;
+ c = Channel.getChannel(source, horizonId, xmltv, name);
+ }
+ }
+ int iconPart = (fileformat<5?5:6);
+ if (parts.size() > iconPart) {
+ c.addIcon(parts.get(iconPart));
+ }
+ if (enabled.equals("enabled")) {
+ c.setEnabled(true);
+ } else if (enabled.equals("disabled")) {
+ c.setEnabled(false);
+ } else {
+ logger.error("Error in config file, unknown channel status \""
+ + enabled
+ + "\", should be enabled or disabled");
+ }
+ break;
+ }
+ case 6: {
+ String source = parts.get(1);
+ String xmltv = parts.get(2);
+ String id = parts.get(3);
+ String enabled = parts.get(4);
+ String name = parts.get(5);
+ String extra = parts.get(6);
+ c = Channel.getChannel(source, id, xmltv, name);
+ if (parts.size() > 7) {
+ c.addIcon(parts.get(7));
+ }
+ if (enabled.equals("enabled")) {
+ c.setEnabled(true);
+ } else if (enabled.equals("disabled")) {
+ c.setEnabled(false);
+ } else {
+ logger.error("Error in config file, unknown channel status \""
+ + enabled
+ + "\", should be enabled or disabled");
+ }
+ }
+ }
+ return c;
+ }
+
+ public String toString() {
+ return "Channel " + source + "::" + id + " (" + defaultName() + ")";
+ }
+}
int cacheMisses = 0;
}
- public int getId();
-
- public void setId(int id);
+ // All implementing class must declare a
+ // public final static String NAME = "....";
+ // (e.g tvgids.nl or rtl.nl)
public String getName();
- // All implementing class must declare a
- // public static final String NAME (e.g tvgids.nl or rtl.nl)
-
public List<Channel> getChannels();
// Convenience method
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.NoSuchFieldException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.Properties;
+import org.reflections.Reflections;
+import org.reflections.util.ClasspathHelper;
+import org.reflections.scanners.SubTypesScanner;
+
import org.apache.log4j.Logger;
public class EPGSourceFactory {
- 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;
- private static List<Integer> sources = new ArrayList<Integer>();
- static Logger logger = Logger.getLogger(EPGSourceFactory.class);
+ private static Map<String, Class<? extends EPGSource>> sources = new HashMap<String, Class<? extends EPGSource>>();
+
+ private static boolean initialised = false;
+ static Logger logger = Logger.getLogger(EPGSourceFactory.class);
- static void init() {
- if (initialised)
- return;
- Properties configProp = new Properties();
- ClassLoader loader = ClassLoader.getSystemClassLoader();
- InputStream in = loader
- .getResourceAsStream("tv_grab_nl_java.properties");
- try {
- configProp.load(in);
- } catch (IOException e) {
- logger.warn("Error reading application properties resource", e);
- }
- 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);
- // System.out.println("clazz: " + clazz.toString());
- Field NAME = clazz.getField("NAME");
- // System.out.println("NAME: " + NAME.toString());
- String sourceName = (String) NAME.get(null);
- names.put(source, sourceName);
- ids.put(sourceName, source);
- sources.add(source);
- } catch (Exception e) {
- logger.error("Error reading EPG Source class " + name, e);
- }
- }
- initialised = true;
- }
+ static void init() {
+ if (initialised) return;
- private EPGSourceFactory() {
- init();
- }
+ Reflections reflections = new Reflections(
+ ClasspathHelper.forPackage("org.vanbest.xmltv"), new SubTypesScanner());
+ Set<Class<? extends EPGSource>> implementingTypes =
+ reflections.getSubTypesOf(EPGSource.class);
- public static EPGSourceFactory newInstance() {
- return new EPGSourceFactory();
- }
+ for(Class<? extends EPGSource> clazz: implementingTypes) {
+ try {
+ Field NAME;
+ try {
+ NAME = clazz.getField("NAME");
+ } catch (NoSuchFieldException e2) {
+ continue;
+ }
+ logger.debug("EPG source classname: " + NAME.toString());
+ String sourceName = (String) NAME.get(null);
+ sources.put(sourceName, clazz);
+ } catch (Exception e) {
+ logger.error("Error reading EPG Source class " + clazz, e);
+ }
+ }
+ initialised = true;
+ }
- public EPGSource createEPGSource(int source, Config config) {
- Constructor<EPGSource> constructor;
- try {
- constructor = classes.get(source).getConstructor(Integer.TYPE,
- Config.class);
- return constructor.newInstance(source, config);
- } catch (Exception e) {
- logger.error(
- "Error instantiating EPG source " + classes.get(source), e);
- }
- return null;
- }
+ private EPGSourceFactory() {
+ init();
+ }
- public EPGSource createEPGSource(String source, Config config) {
- int sourceId = getChannelSourceId(source);
- return createEPGSource(sourceId, config);
- }
+ public static EPGSourceFactory newInstance() {
+ return new EPGSourceFactory();
+ }
- public static String getChannelSourceName(int id) {
- init();
- return names.get(id);
- }
+ public EPGSource createEPGSource(Class<? extends EPGSource> source, Config config) {
+ Constructor<? extends EPGSource> constructor;
+ try {
+ constructor = source.getConstructor(Config.class);
+ return constructor.newInstance(config);
+ } catch (Exception e) {
+ logger.error("Error instantiating EPG source " + source, e);
+ }
+ return null;
+ }
- public static int getChannelSourceId(String name) {
- init();
- return ids.get(name);
- }
+ public EPGSource createEPGSource(String source, Config config) {
+ return createEPGSource(sources.get(source), config);
+ }
- public int[] getAll() {
- int[] result = new int[sources.size()];
- for (int i = 0; i < result.length; i++) {
- result[i] = sources.get(i);
- }
- return result;
- }
+ public List<EPGSource> getAll(Config config) {
+ ArrayList<EPGSource> result = new ArrayList<EPGSource>();
+ for(Class<? extends EPGSource> clazz: sources.values()) {
+ result.add(createEPGSource(clazz, config));
+ }
+ return result;
+ }
}
private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_HORIZON = 7;
- public static String NAME = "horizon.tv";
+ public final static String NAME="horizon.tv";
static Logger logger = Logger.getLogger(Horizon.class);
- public Horizon(int sourceId, Config config) {
- super(sourceId, config);
+ public Horizon(Config config) {
+ super(config);
}
public String getName() {
}
}
String xmltv = name.replaceAll("[^a-zA-Z0-9]", "")+"."+getName();
- Channel c = Channel.getChannel(getId(), Long.toString(horizonId), xmltv, name);
+ Channel c = Channel.getChannel(getName(), Long.toString(horizonId), xmltv, name);
//Channel c = new HorizonChannel(getId(), horizonId, name);
c.addIcon(icon);
result.add(c);
private Programme programmeFromJSON(JSONObject json,
boolean fetchDetails) throws Exception {
String id = json.getString("id");
- Programme result = cache.get(getId(), id);
+ Programme result = cache.get(getName(), id);
boolean cached = (result != null);
boolean doNotCache = false;
if (result == null) {
// TODO other fields
if (!cached && !doNotCache) {
- cache.put(getId(), id, result);
+ cache.put(getName(), id, result);
}
logger.debug(result);
return result;
*/
public static void main(String[] args) {
Config config = Config.getDefaultConfig();
- Horizon horizon = new Horizon(3, config);
+ Horizon horizon = new Horizon(config);
horizon.clearCache();
try {
List<Channel> channels = horizon.getChannels();
cache.clear();
cache.close();
}
- Map<Integer, EPGSource> guides = new HashMap<Integer, EPGSource>();
+ Map<String, EPGSource> guides = new HashMap<String, EPGSource>();
EPGSourceFactory factory = EPGSourceFactory.newInstance();
// EPGSource gids = new TvGids(config);
// if (clearCache) gids.clearCache();
writer.flush();
writer.close();
- for (int source : guides.keySet()) {
+ for (String source : guides.keySet()) {
guides.get(source).close();
}
if (!config.quiet) {
EPGSource.Stats stats = new EPGSource.Stats();
- for (int source : guides.keySet()) {
+ for (String source : guides.keySet()) {
EPGSource.Stats part = guides.get(source).getStats();
stats.cacheHits += part.cacheHits;
stats.cacheMisses += part.cacheMisses;
Set<String> oldChannels = new HashSet<String>();
Set<String> oldChannelNames = new HashSet<String>();
- Set<Integer> oldGuides = new HashSet<Integer>();
+ Set<String> oldGuides = new HashSet<String>();
for (Channel c : config.channels) {
if (c.enabled) {
oldChannels.add(c.source + "::" + c.id);
}
EPGSourceFactory factory = EPGSourceFactory.newInstance();
- int[] sources = factory.getAll();
+ List<? extends EPGSource> allSources = factory.getAll(config);
- System.out
- .println("Please select the TV programme information sources to use");
+ System.out.println("Please select the TV programme information sources to use");
List<EPGSource> guides = new ArrayList<EPGSource>();
- for (int source : sources) {
- boolean selected = oldGuides.contains(source);
- EPGSource guide = factory.createEPGSource(source, config);
- System.out.print(" Use \"" + guide.getName()
+ for(EPGSource source: allSources) {
+ boolean selected = oldGuides.contains(source.getName());
+ System.out.print(" Use \"" + source.getName()
+ "\" (Y/N, default=" + (selected ? "Y" : "N") + "):");
if(config.configYes) {
- guides.add(guide);
+ guides.add(source);
System.out.println("Y");
continue;
}
String s = reader.readLine().toLowerCase();
if (s.isEmpty()) {
if (selected)
- guides.add(guide);
+ guides.add(source);
break;
} else if (s.startsWith("y")) {
- guides.add(guide);
+ guides.add(source);
break;
} else if (s.startsWith("n")) {
break;
private PreparedStatement clearSourceStatement;
/** The Constant SCHEMA_VERSION. */
- private final static Integer SCHEMA_VERSION = 2;
+ private final static Integer SCHEMA_VERSION = 3;
- /** The Constant SCHEMA_KEY. */
- private final static String SCHEMA_KEY = "TV_GRAB_NL_JAVA_SCHEMA_VERSION";
+ /** The Constant SCHEMA_KEY_ID. */
+ private final static String SCHEMA_KEY_SOURCE = "TV_GRAB_NL_JAVA";
+ private final static String SCHEMA_KEY_ID = "SCHEMA_VERSION";
/** The logger. */
static Logger logger = Logger.getLogger(ProgrammeCache.class);
try {
PreparedStatement stat = db
.prepareStatement("SELECT programme FROM cache WHERE source=? AND id=?");
- stat.setInt(1, 1);
- stat.setString(2, SCHEMA_KEY);
+ stat.setString(1, SCHEMA_KEY_SOURCE);
+ stat.setString(2, SCHEMA_KEY_ID);
ResultSet result = stat.executeQuery();
if (!result.next()) {
logger.debug("No schema version found in database");
logger.info("Unknown cache schema, removing and recreating cache");
try {
Statement stat = db.createStatement();
- // System.out.println("Dropping old table");
+ logger.trace("Dropping old table");
stat.execute("DROP TABLE IF EXISTS cache");
- // System.out.println("Creating new table");
- stat.execute("CREATE CACHED TABLE IF NOT EXISTS cache (source INTEGER, id VARCHAR(128), date DATE, programme OTHER, PRIMARY KEY (source,id))");
+ logger.trace("Creating new table");
+ stat.execute("CREATE CACHED TABLE IF NOT EXISTS cache (source CHAR(20), id VARCHAR(128), date DATE, programme OTHER, PRIMARY KEY (source,id))");
stat.close();
- // System.out.println("Writing new schema version to database");
+ logger.trace("Writing new schema version to database");
PreparedStatement stat2 = db
.prepareStatement("INSERT INTO cache VALUES (?,?,?,?)");
- stat2.setInt(1, 1);
- stat2.setString(2, SCHEMA_KEY);
+ stat2.setString(1, SCHEMA_KEY_SOURCE);
+ stat2.setString(2, SCHEMA_KEY_ID);
stat2.setDate(3, new java.sql.Date(new java.util.Date(2100,
11, 31).getTime()));
stat2.setObject(4, SCHEMA_VERSION);
* @param id the id
* @return the programme
*/
- public Programme get(int source, String id) {
+ public Programme get(String source, String id) {
if (db == null)
return null;
try {
- getStatement.setInt(1, source);
+ getStatement.setString(1, source);
getStatement.setString(2, id);
ResultSet r = getStatement.executeQuery();
if (!r.next())
* @param source the source
* @param id the id
*/
- private void removeCacheEntry(int source, String id) {
+ private void removeCacheEntry(String source, String id) {
try {
- removeStatement.setInt(1, source);
+ removeStatement.setString(1, source);
removeStatement.setString(2, id);
removeStatement.execute();
} catch (SQLException e) {
* @param id the id
* @param prog the prog
*/
- public void put(int source, String id, Programme prog) {
+ public void put(String source, String id, Programme prog) {
if (db == null)
return;
try {
- putStatement.setInt(1, source);
+ putStatement.setString(1, source);
putStatement.setString(2, id);
- putStatement
- .setDate(3, new java.sql.Date(prog.startTime.getTime()));
+ putStatement.setDate(3, new java.sql.Date(prog.startTime.getTime()));
putStatement.setObject(4, prog);
// System.out.println(putStatement.toString());
int count = putStatement.executeUpdate();
*
* @param source the source
*/
- public void clear(int source) {
+ public void clear(String source) {
if (db == null)
return;
try {
- clearSourceStatement.setInt(1, source);
+ clearSourceStatement.setString(1, source);
int count = clearSourceStatement.executeUpdate();
if (!config.quiet && count > 0) {
logger.info("Cleared " + count + " entries from cache");
private static final String icon_url = "http://www.rtl.nl/service/gids/components/vaste_componenten/";
private static final int MAX_PROGRAMMES_PER_DAY = 9999;
- public static final String NAME = "rtl.nl";
+ public static final String NAME="rtl.nl";
static Logger logger = Logger.getLogger(RTL.class);
static boolean debug = false;
}
}
- public RTL(int sourceId, Config config) {
- super(sourceId, config);
+ public RTL(Config config) {
+ super(config);
}
public String getName() {
JSONObject schedule = schedules.getJSONObject(i);
String channel = schedule.getString("station");
if(!channels.containsKey(channel)) {
- Channel c = Channel.getChannel(getId(), channel, channel);
+ Channel c = Channel.getChannel(getName(), channel, channel);
// TODO: channel icon
channels.put(channel, c);
}
List<Programme> result = new LinkedList<Programme>();
Map<String, Channel> channelMap = new HashMap<String, Channel>();
for (Channel c : channels) {
- if (c.enabled && c.source == getId())
+ if (c.enabled && c.source.equals(getName()))
channelMap.put(c.id, c);
}
URL url = programmeUrl(0, day);
Config config = Config.getDefaultConfig();
config.niceMilliseconds = 50;
- RTL rtl = new RTL(2, config);
+ RTL rtl = new RTL(config);
if (debug) {
rtl.cache.clear();
logger.info("Writing CSV to rtl.csv");
private static final int MAX_PROGRAMMES_PER_DAY = 9999;
private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_TVGIDS = 3;
-
- public static String NAME = "tvgids.nl";
+ public static final String NAME="tvgids.nl";
static Logger logger = Logger.getLogger(TvGids.class);
- public TvGids(int sourceId, Config config) {
- super(sourceId, config);
+ public TvGids(Config config) {
+ super(config);
}
public String getName() {
- return NAME;
+ return NAME;
}
public static URL programmeUrl(List<Channel> channels, int day)
.unescapeHtml(zender.getString("name"));
String icon = "http://tvgidsassets.nl/img/channels/53x27/" + id
+ ".png";
- Channel c = Channel.getChannel(getId(), Integer.toString(id), name);
+ Channel c = Channel.getChannel(getName(), Integer.toString(id), name);
c.addIcon(icon);
result.add(c);
}
private Programme programmeFromJSON(JSONObject programme,
boolean fetchDetails) throws Exception {
String id = programme.getString("db_id");
- Programme result = cache.get(getId(), id);
+ Programme result = cache.get(getName(), id);
boolean cached = (result != null);
if (result == null) {
stats.cacheMisses++;
}
if (!cached) {
// FIXME where to do this?
- cache.put(getId(), id, result);
+ cache.put(getName(), id, result);
}
logger.debug(result);
return result;
*/
public static void main(String[] args) {
Config config = Config.getDefaultConfig();
- TvGids gids = new TvGids(1, config);
+ TvGids gids = new TvGids(config);
try {
List<Channel> channels = gids.getChannels();
System.out.println("Channels: " + channels);
private static final int MAX_PROGRAMMES_PER_DAY = 9999;
private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_ZIGGOGIDS = 3;
private static final int MAX_CHANNELS_PER_REQUEST = 25;
-
- public static String NAME = "ziggogids.nl";
+ public final static String NAME="ziggogids.nl";
static Logger logger = Logger.getLogger(ZiggoGids.class);
- public ZiggoGids(int sourceId, Config config) {
- super(sourceId, config);
+ public ZiggoGids(Config config) {
+ super(config);
}
public String getName() {
String index = e.select("input").first().attr("value");
String name = e.select("label").first().text();
logger.debug(" "+index+": \""+name+"\"");
- Channel c = Channel.getChannel(getId(), index, name);
+ Channel c = Channel.getChannel(getName(), index, name);
try {
String icon = fetchIconUrl(httpclient, index);
logger.debug(" "+icon);
long start = Long.parseLong(item.attr("pr-start")); // unix time
String id = Long.toString(start)+"_"+progid;
- Programme p = cache.get(getId(), id);
+ Programme p = cache.get(getName(), id);
boolean cached = (p != null);
if (p == null) {
stats.cacheMisses++;
}
if (!cached) {
// FIXME where to do this?
- cache.put(getId(), id, p);
+ cache.put(getName(), id, p);
}
return p;
}
public static void main(String[] args) {
Config config = Config.getDefaultConfig();
logger.setLevel(Level.TRACE);
- ZiggoGids gids = new ZiggoGids(4, config);
+ ZiggoGids gids = new ZiggoGids(config);
try {
List<Channel> channels = gids.getChannels();
System.out.println("Channels: " + channels);
public class HorizonTest {
private static Horizon horizon;
- private final static int HORIZON_SOURCE_ID = 3;
private static List<Channel> channels = null;
@BeforeClass
public static void setUp() throws Exception {
Config config = Config.getDefaultConfig();
- horizon = new Horizon(HORIZON_SOURCE_ID, config);
+ horizon = new Horizon(config);
horizon.clearCache();
}
c.icons.isEmpty());
}
assertEquals("All channels should have RTL.nl source id",
- HORIZON_SOURCE_ID, c.source);
+ horizon.getName(), c.source);
}
if (!foundRTL4) {
fail("Channel RTL4 not found, should be there");
public class RTLTest {
private static RTL rtl;
- private final static int RTL_SOURCE_ID = 2;
private static List<Channel> channels = null;
@BeforeClass
public static void setUp() throws Exception {
Config config = Config.getDefaultConfig();
- rtl = new RTL(RTL_SOURCE_ID, config);
+ rtl = new RTL(config);
rtl.clearCache();
}
//assertFalse("RTL4 channel should have at least one icon", c.icons.isEmpty());
}
assertEquals("All channels should have RTL.nl source id",
- RTL_SOURCE_ID, c.source);
+ rtl.getName(), c.source);
}
if (!foundRTL4) {
fail("Channel RTL4 not found, should be there");