display errors via notifications
authorChristian Weiske <cweiske@cweiske.de>
Wed, 12 Aug 2015 19:13:44 +0000 (21:13 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 12 Aug 2015 19:13:44 +0000 (21:13 +0200)
README.rst
development.rst
index.js

index ebcc8eb297f7a7be69088a6db4bed4f0af67bf52..e226ce456851d5be9628006d1fb8a38b8b248608 100644 (file)
@@ -26,6 +26,7 @@ Features
   Nice for video lists; no need to access the detail page anymore.
 - Dreambox web interface access token support
 - Supports hundreds of video sites, see the `youtube-dl site support list`__.
+- Errors are displayed via the operating system's notification system
 
 __ http://rg3.github.io/youtube-dl/supportedsites.html
 
index d901813b6d2cc9060438378a992f37cf2b3f88a5..ee719445297b4e27d2d286a76c0d2a77a96492e9 100644 (file)
@@ -34,6 +34,14 @@ Stop current video and end dreambox mediaplayer::
  $ curl 'http://192.168.3.42/web/mediaplayercmd?command=exit'
 
 
+Debugging
+=========
+``about:config``
+
+``extensions.sdk.console.logLevel``
+  ``debug``
+
+
 
 URLs
 ====
index 209057ff4b02a7303550ffae2d91898393b169f7..dfbbdd7fd0350611266b3c1ef9cd9bc81075a601 100644 (file)
--- a/index.js
+++ b/index.js
@@ -54,21 +54,31 @@ function playPageUrl(pageUrl)
     var ytdl = child_process.spawn(youtubedlPath, ['--get-url', pageUrl]);
 
     var videoUrl = null;
+    var errors = '';
     ytdl.stdout.on('data', function (data) {
-        videoUrl = data;
-        console.log('youtube-dl URL: ' + data);
+        videoUrl = data.trim();
+        console.debug('youtube-dl URL: ' + data.trim());
     });
 
     ytdl.stderr.on('data', function (data) {
-        console.error('youtube-dl error: ' + data);
+        console.debug('youtube-dl stderr: ' + data.trim());
+        if (data.substr(0, 7) != 'WARNING') {
+            errors += "\n" + data.trim();
+        }
     });
 
     ytdl.on('close', function (code) {
         if (code == 0) {
             //we have a url. run dreambox
             playVideoOnDreambox(videoUrl);
+        } else if (code == -1) {
+            showError('youtube-dl not found');
         } else {
-            console.error('youtube-dl exited with code ' + code);
+            console.log('youtube-dl exit code ' + code);
+            showError(
+                'Failed to extract video URL with youtube-dl'
+                + errors
+            );
         }
     });
 }
@@ -87,6 +97,7 @@ function playVideoOnDreambox(videoUrl)
             sessionid: dbToken
         },
         onComplete: function (response) {
+            // https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_codes
             if (response.status == 412) {
                 //web interface requires an access token
                 // fetch it and try to play the video again
@@ -95,6 +106,14 @@ function playVideoOnDreambox(videoUrl)
                         playVideoOnDreambox(videoUrl);
                     }
                 );
+            } else if (response.status == 0) {
+                showError('Failed to connect to dreambox. Maybe wrong IP?');
+            } else if (response.status > 399) {
+                showError('Dreambox failed to play the movie');
+                console.error('Dreambox play response status: ' + response.status);
+            } else {
+                //all fine
+                console.log('Dreambox play response status: ' + response.status);
             }
         }
     }).post();
@@ -124,7 +143,7 @@ function fetchAccessToken(replayFunc)
             var nStart = response.text.indexOf('<e2sessionid>') + 13;
             var nEnd   = response.text.indexOf('</e2sessionid>');
             if (nStart == -1 || nEnd == -1) {
-                console.error('Could not parse token XML');
+                showError('Could not parse web interface token XML');
                 return;
             }
 
@@ -135,3 +154,30 @@ function fetchAccessToken(replayFunc)
         }
     }).post();
 }
+
+/**
+ * Show the error message in a notification popup window
+ * @link https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/notifications
+ */
+function showError(msg)
+{
+    require("sdk/notifications").notify({
+        title: "Error - Play video on Dreambox",
+        text: encodeXmlEntities(msg.trim()),
+        iconURL: "./play-32.png"
+    });
+    console.error('showError: ' + msg.trim());
+}
+
+/**
+ * Encoding XML entities is necessary on Ubuntu 14.04 with Mate 1.8.2
+ * If I don't do it, the message is empty.
+ */
+function encodeXmlEntities(str)
+{
+    return str.replace('&', '&amp;')
+        .replace('<', '&lt;')
+        .replace('>', '&gt;')
+        .replace('"', '&quot;')
+        .replace("'", '&apos;');
+};