[weboob] [PATCH 2/2] _play_proxy function to handle cookies and redirect url

Vincent Texier vit at free.fr
Sun Jan 12 12:06:02 CET 2014


Add an attribute "_play_proxy" to the video object to trigger _play_proxy.
The _play_proxy function require "wget" to be properly installed.

First module requiring _play_proxy : dailymotion.

Signed-off-by: Vincent Texier <vit at free.fr>
---
 modules/dailymotion/pages.py             |    4 ++++
 weboob/tools/application/media_player.py |   38 ++++++++++++++++++++++++++++--
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/modules/dailymotion/pages.py b/modules/dailymotion/pages.py
index a640d43..7e2ea2e 100644
--- a/modules/dailymotion/pages.py
+++ b/modules/dailymotion/pages.py
@@ -138,6 +138,10 @@ class VideoPage(BasePage):
 
         video.url = info[max_quality]
 
+        # dailymotion video url is protected by a redirection with cookie verification
+        # so we need to do a "play_proxy" using wget to handle this
+        video._play_proxy = True
+
         video.set_empty_fields(NotAvailable)
 
         return video
diff --git a/weboob/tools/application/media_player.py b/weboob/tools/application/media_player.py
index 81bdfd7..8990526 100644
--- a/weboob/tools/application/media_player.py
+++ b/weboob/tools/application/media_player.py
@@ -83,12 +83,17 @@ class MediaPlayer(object):
         if media.url.startswith('rtmp'):
             self._play_rtmp(media, player_name, args=player_args)
         else:
-            self._play_default(media, player_name)
+            self._play_default(media, player_name, args=player_args)
 
-    def _play_default(self, media, player_name):
+    def _play_default(self, media, player_name, args):
         """
         Play media.url with the media player.
         """
+        # if flag play_proxy...
+        if hasattr(media, '_play_proxy') and media._play_proxy == True:
+           # use wget to handle redirect and cookies
+            self._play_proxy(media, player_name, args)
+
         args = player_name.split(' ')
 
         player_name = args[0]
@@ -97,6 +102,35 @@ class MediaPlayer(object):
         print 'Invoking "%s".' % (' '.join(args))
         os.spawnlp(os.P_WAIT, player_name, *args)
 
+    def _play_proxy(self, media, player_name, args):
+        """
+        Download data with wget and pipe them to a media player.
+
+        We need that for url that use redirection and cookies.
+        This function is used if the non-standard
+        non-API compliant '_play_proxy' attribute of the 'media' object is defined and is True.
+        """
+        if not self._find_in_path(os.environ['PATH'], 'wget'):
+            self.logger.warning('"wget" binary not found')
+            return False
+
+        media_url = media.url
+
+        wget = 'wget -c \'%s\' -O - |' % media_url
+
+        if args is None:
+            for (binary, stdin_args) in PLAYERS:
+                if binary == player_name:
+                    args = stdin_args
+
+        assert args is not None
+
+        print ':: Streaming from %s' % media_url
+        print ':: to %s %s' % (player_name, args)
+        print ':: %s' % wget
+        print wget + ' ' + player_name + ' ' + args
+        Popen(wget + ' ' + player_name + ' ' + args, shell=True)
+
     def _play_rtmp(self, media, player_name, args):
         """
         Download data with rtmpdump and pipe them to a media player.
-- 
1.7.9.5




More information about the weboob mailing list