From 6a6541a65bdc9f2f26f9ee04f61cee65a71631dd Mon Sep 17 00:00:00 2001
From: Jan-Pascal van Best <janpascal@vanbest.org>
Date: Tue, 5 Mar 2013 12:10:33 +0100
Subject: [PATCH] More work on Horizon fetcher, looking good now

---
 .classpath                                    | 32 ++++++++--
 src/main/java/org/vanbest/xmltv/Config.java   |  1 +
 src/main/java/org/vanbest/xmltv/Horizon.java  | 59 +++++++++++++++++--
 .../java/org/vanbest/xmltv/Programme.java     | 24 ++++++++
 .../org/vanbest/xmltv/ProgrammeCache.java     |  4 +-
 src/main/resources/tv_grab_nl_java.properties |  3 +-
 6 files changed, 110 insertions(+), 13 deletions(-)

diff --git a/.classpath b/.classpath
index d90b917..f32149e 100644
--- a/.classpath
+++ b/.classpath
@@ -1,9 +1,31 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
-	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
-	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
 	<classpathentry kind="output" path="target/classes"/>
 </classpath>
diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java
index 54eda71..8a999dc 100644
--- a/src/main/java/org/vanbest/xmltv/Config.java
+++ b/src/main/java/org/vanbest/xmltv/Config.java
@@ -126,6 +126,7 @@ public class Config {
 		result.put("sport", "Sports");
 		result.put("theater", "Arts/Culture");
 		result.put("wetenschap", "Science/Nature");
+		result.put("news", "News");
 		return result;
 	}
 
diff --git a/src/main/java/org/vanbest/xmltv/Horizon.java b/src/main/java/org/vanbest/xmltv/Horizon.java
index 70f714f..01c50fe 100644
--- a/src/main/java/org/vanbest/xmltv/Horizon.java
+++ b/src/main/java/org/vanbest/xmltv/Horizon.java
@@ -121,11 +121,21 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
 			
 			JSONObject firstSchedule = stationSchedules.getJSONObject(0);
 			JSONObject station = firstSchedule.getJSONObject("station");
-			logger.info("firstschedule: " + firstSchedule.toString());
+			logger.debug("firstschedule: " + firstSchedule.toString());
 			int id = station.getInt("id");
 			String name = station.getString("title");
+			// Use the largest available station logo as icon
 			JSONArray images = station.getJSONArray("images");
 			String icon = "";
+			int maxSize = 0;
+			for( int j=0; j<images.size(); j++) {
+				JSONObject image = images.getJSONObject(j);
+				if (image.getString("assetType").startsWith("station-logo") &&
+						image.getInt("width")>maxSize) {
+					icon = image.getString("url");
+					maxSize = image.getInt("width");
+				}
+			}
 			Channel c = Channel.getChannel(getId(), Integer.toString(id), name,
 					icon);
 			result.add(c);
@@ -150,9 +160,9 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
 
 		for (Channel c : channels) {
 			URL url = programmeUrl(c, day);
-			logger.info("Programme url:" + url);
+			logger.debug("Programme url:" + url);
 			JSONObject jsonObject = fetchJSON(url);
-			logger.info(jsonObject.toString());
+			logger.debug(jsonObject.toString());
 			JSONArray listings = jsonObject.getJSONArray("listings");
 			for (int i = 0; i < listings.size(); i++) {
 				JSONObject listing = listings.getJSONObject(i);
@@ -212,7 +222,45 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
 			result.startTime = new Date(json.getLong("startTime"));
 			result.endTime = new Date(json.getLong("endTime"));
 			JSONObject prog = json.getJSONObject("program");
-			result.addTitle(prog.getString("title"));
+			if (prog.containsKey("secondaryTitle")){
+				result.addTitle(prog.getString("secondaryTitle"));
+			} else {
+				result.addTitle(prog.getString("title"));
+			}
+			String description = prog.getString("longDescription");
+			if (description==null || description.isEmpty()) {
+			description = prog.getString("description");
+				if (description==null || description.isEmpty()) {
+					description = prog.getString("shortDescription");
+				}
+			}
+			result.addDescription(description);
+
+			JSONArray cast = prog.getJSONArray("cast");
+			for( int j=0; j<cast.size(); j++) {
+				result.addActor(cast.getString(j));
+			}
+
+			JSONArray directors = prog.getJSONArray("directors");
+			for( int j=0; j<directors.size(); j++) {
+				result.addDirector(directors.getString(j));
+			}
+			JSONArray categories = prog.getJSONArray("categories");
+			for( int j=0; j<categories.size(); j++) {
+				String cat = categories.getJSONObject(j).getString("title");
+				if (!cat.contains("/")) {
+					// Remove things like "drama/drama" and subcategories
+					result.addCategory(config.translateCategory(cat));
+				}
+			}
+			if (prog.containsKey("seriesEpisodeNumber")){
+				String episode = prog.getString("seriesEpisodeNumber");
+				result.addEpisode(episode,"onscreen");
+			}
+			if (prog.containsKey("parentalRating")){
+				String rating  = prog.getString("parentalRating");
+				result.addRating("kijkwijzer", "Afgeraden voor kinderen jonger dan "+rating+" jaar");
+			}
 			/*
 			// Do this here, because we can only add to these fields. Pity if
 			// they're updated
@@ -253,7 +301,8 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
 	 */
 	public static void main(String[] args) {
 		Config config = Config.getDefaultConfig();
-		Horizon horizon = new Horizon(1, config);
+		Horizon horizon = new Horizon(3, config);
+		horizon.clearCache();
 		try {
 			List<Channel> channels = horizon.getChannels();
 			System.out.println("Channels: " + channels);
diff --git a/src/main/java/org/vanbest/xmltv/Programme.java b/src/main/java/org/vanbest/xmltv/Programme.java
index be17c00..884ecd3 100644
--- a/src/main/java/org/vanbest/xmltv/Programme.java
+++ b/src/main/java/org/vanbest/xmltv/Programme.java
@@ -152,6 +152,15 @@ public class Programme implements Serializable {
 		descriptions.add(new Title(title, lang));
 	}
 
+	public void addEpisode(String episode, String system) {
+		if (episodes == null)
+			episodes = new ArrayList<Episode>();
+		Episode e = new Episode();
+		e.episode = episode;
+		e.system = system;
+		episodes.add(e);
+	}
+		
 	public void addCategory(String category) {
 		addCategory(category, null);
 	}
@@ -323,6 +332,20 @@ public class Programme implements Serializable {
 		}
 	}
 
+	private void writeEpisodeList(List<Episode> episodes, XMLStreamWriter writer)
+			throws XMLStreamException {
+		if (episodes == null)
+			return;
+		for (Episode e: episodes) {
+			writer.writeStartElement("episode");
+			if (e.system != null)
+				writer.writeAttribute("system", e.system);
+			if (e.episode != null)
+				writer.writeCharacters(e.episode);
+			writer.writeEndElement();
+		}
+	}
+
 	private void writeIconList(List<Icon> icons, XMLStreamWriter writer)
 			throws XMLStreamException {
 		if (icons == null)
@@ -355,6 +378,7 @@ public class Programme implements Serializable {
 		writeTitleList(categories, "category", writer);
 		writeIconList(icons, writer);
 		writeStringList(urls, "url", writer);
+		writeEpisodeList(episodes,writer);
 		if (video != null) {
 			writer.writeStartElement("video");
 			if (!video.present) {
diff --git a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java
index 01de8a4..9248120 100644
--- a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java
+++ b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java
@@ -52,7 +52,7 @@ public class ProgrammeCache {
     private PreparedStatement clearSourceStatement;
 
     /** The Constant SCHEMA_VERSION. */
-    private final static Integer SCHEMA_VERSION = 1;
+    private final static Integer SCHEMA_VERSION = 2;
 
     /** The Constant SCHEMA_KEY. */
     private final static String SCHEMA_KEY = "TV_GRAB_NL_JAVA_SCHEMA_VERSION";
@@ -128,7 +128,7 @@ public class ProgrammeCache {
                     // System.out.println("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(64), date DATE, programme OTHER, PRIMARY KEY (source,id))");
+                    stat.execute("CREATE CACHED TABLE IF NOT EXISTS cache (source INTEGER, id VARCHAR(128), date DATE, programme OTHER, PRIMARY KEY (source,id))");
                     stat.close();
 
                     // System.out.println("Writing new schema version to database");
diff --git a/src/main/resources/tv_grab_nl_java.properties b/src/main/resources/tv_grab_nl_java.properties
index 2efb1d9..0051cac 100644
--- a/src/main/resources/tv_grab_nl_java.properties
+++ b/src/main/resources/tv_grab_nl_java.properties
@@ -3,4 +3,5 @@ project.name=${project.name}
 project.description=${project.description}
 build.time=${build.time}
 org.vanbest.xmltv.epgsource.impl.1=org.vanbest.xmltv.TvGids
-org.vanbest.xmltv.epgsource.impl.2=org.vanbest.xmltv.RTL
\ No newline at end of file
+org.vanbest.xmltv.epgsource.impl.2=org.vanbest.xmltv.RTL
+org.vanbest.xmltv.epgsource.impl.3=org.vanbest.xmltv.Horizon
\ No newline at end of file
-- 
2.39.5