]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Get Horizon API urls from a JavaScript configuration file
authorJan-Pascal van Best <janpascal@vanbest.org>
Wed, 9 Mar 2016 20:04:15 +0000 (21:04 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Wed, 9 Mar 2016 20:04:15 +0000 (21:04 +0100)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Horizon.java

index 4684ea7f852033ffdebdd81ae3403901bc673316..47ec9ea01a57dd4806dfc8a67a709cf78a61ea9d 100644 (file)
@@ -72,8 +72,10 @@ public abstract class AbstractEPGSource implements EPGSource {
                                BufferedReader reader = new BufferedReader(
                                                new InputStreamReader(url.openStream(), charset));
                                String s;
-                               while ((s = reader.readLine()) != null)
+                               while ((s = reader.readLine()) != null) {
                                        buf.append(s);
+                                        buf.append("\n");
+                                }
                                done = true;
                        } catch (IOException e) {
                                if (!config.quiet) {
index 1594fc9281bd21a6e823f45d104f007e4b48da6b..d9b9e37a1349a38e8d052b903db5e452a477292b 100644 (file)
@@ -24,9 +24,13 @@ import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
+import javax.script.*;
+
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamWriter;
 
+import sun.org.mozilla.javascript.NativeObject;
+
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 
@@ -35,24 +39,117 @@ import net.sf.json.JSONObject;
 
 public class Horizon extends AbstractEPGSource implements EPGSource {
 
-    static String channels_url = "https://web-api-salt.horizon.tv/oesp/api/NL/nld/web/channels/";
-    static String listings_url = "https://web-api-salt.horizon.tv/oesp/api/NL/nld/web/listings";
+    static String config_url = "https://www.horizon.tv/content/orion-js-app/settings.js?countryhomepage=/content/www-horizon-tv/nl_nl";
+
+    private String channels_url = null;
+    private String listings_url = null;
 
     private static final int MAX_DAYS_AHEAD_SUPPORTED_BY_HORIZON = 7;
 
-        public final static String NAME="horizon.tv";
+    public final static String NAME="horizon.tv";
 
     static Logger logger = Logger.getLogger(Horizon.class);
 
     public Horizon(Config config) {
         super(config);
+        find_urls();
+    }
+
+    private void find_urls() {
+        URL url = null;
+        try {
+            url = new URL(config_url);
+        } catch (MalformedURLException e) {
+            logger.error("Exception creating horizon config url", e);
+        }
+        String config;
+        try {
+            config = fetchURL(url, "UTF-8");
+        } catch (Exception e1) {
+            // TODO Auto-generated catch block
+            e1.printStackTrace();
+            return;
+        }
+        //logger.debug("horizon config javascript: " + config);
+
+        // Prefix config javascript with definitions for the structures
+        // it will write to
+        String prefix = "var window = {" +
+                 "    location: {" +
+                 "           href: \"http://localhost\"" +
+                 "    }" +
+                 "};" +
+                 "var orion = {" +
+                 "    entities: {" +
+                 "    }" +
+                 "};";
+
+        //config = prefix + "var BBVSettingsObject = {test: \"test\"};\n" + /*config + */ postfix;
+        config = prefix + "\n" + config; //  + "\n" + postfix;
+
+        //System.out.println("\ntest config javascript: ");
+        //System.out.println(config);
+
+        // Get the JavaScript engine
+        ScriptEngineManager manager = new ScriptEngineManager();
+        ScriptEngine engine = manager.getEngineByName("JavaScript");
+
+        // Set JavaScript variables
+        Bindings vars = new SimpleBindings();
+        
+        // Evaluate script
+        try {
+            engine.eval(config);
+        } catch (Exception e) {
+            logger.error("Exception parsing Horizon javascript config", e);
+            return;
+        }
+
+        Object settings = engine.get("BBVSettingsObject");
+
+        String base_url = null;
+
+        // Parse URLs from settings object
+        if (settings instanceof NativeObject) {
+            NativeObject nObj = (NativeObject)settings;
+            Object api = nObj.get("api", nObj);
+            if (api instanceof NativeObject) {
+                NativeObject nApi = (NativeObject) api;
+                Object urls = nApi.get("urls", nApi);
+                if (urls instanceof NativeObject) {
+                    NativeObject nUrls = (NativeObject) urls;
+                    Object base = nUrls.get("base", nUrls);
+                    try {
+                        URL url_base = new URL((String) base);
+                        URL root_base = new URL(url_base.getProtocol(), url_base.getHost(), url_base.getPort(), "");
+                        base_url = root_base.toString();
+                    } catch (MalformedURLException e) {
+                        logger.error("Malformed URL trying to calculate Horizon base URL", e);
+                    }
+                }
+            }
+            Object routes = nObj.get("oespRoutes", nObj);
+            if (routes instanceof NativeObject) {
+                NativeObject nRoutes = (NativeObject) routes;
+
+                Object channels = nRoutes.get("channels", nRoutes);
+                this.channels_url = base_url + (String) channels;
+                //System.out.println("channels_url: " + channels_url);
+
+                Object listings = nRoutes.get("listings", nRoutes);
+                this.listings_url = base_url + (String) listings;
+                //System.out.println("listings_url: " + listings_url);
+            }
+
+        }
+                
     }
 
     public String getName() {
         return NAME;
     }
 
-    public static URL programmeUrl(Channel channel, int day)
+    public URL programmeUrl(Channel channel, int day)
             throws Exception {
         StringBuilder s = new StringBuilder(listings_url);
         s.append("?byStationId=");