[periscope] Add support for running streams
authorSergey M․ <dstftw@gmail.com>
Fri, 7 Aug 2015 23:33:53 +0000 (05:33 +0600)
committerSergey M․ <dstftw@gmail.com>
Fri, 7 Aug 2015 23:33:53 +0000 (05:33 +0600)
youtube_dl/extractor/periscope.py

index 11648a5116e3ae8e49ac4037906c5d1cdadd88e5..de53b752d124ad64d173bcbf02e31e86517dc3a8 100644 (file)
@@ -2,13 +2,15 @@
 from __future__ import unicode_literals
 
 from .common import InfoExtractor
-from ..utils import (
-    parse_iso8601,
-    unescapeHTML,
+from ..compat import (
+    compat_urllib_parse,
+    compat_urllib_request,
 )
+from ..utils import parse_iso8601
 
 
 class PeriscopeIE(InfoExtractor):
+    IE_DESC = 'Periscope'
     _VALID_URL = r'https?://(?:www\.)?periscope\.tv/w/(?P<id>[^/?#]+)'
     _TEST = {
         'url': 'https://www.periscope.tv/w/aJUQnjY3MjA3ODF8NTYxMDIyMDl2zCg2pECBgwTqRpQuQD352EMPTKQjT4uqlM3cgWFA-g==',
@@ -32,9 +34,6 @@ class PeriscopeIE(InfoExtractor):
     def _real_extract(self, url):
         token = self._match_id(url)
 
-        replay = self._call_api('getAccessPublic', token)
-        video_url = replay['replay_url']
-
         broadcast_data = self._call_api('getBroadcastPublic', token)
         broadcast = broadcast_data['broadcast']
         status = broadcast['status']
@@ -43,20 +42,37 @@ class PeriscopeIE(InfoExtractor):
         uploader_id = broadcast.get('user_id') or broadcast_data.get('user', {}).get('id')
 
         title = '%s - %s' % (uploader, status) if uploader else status
+        state = broadcast.get('state').lower()
+        if state == 'running':
+            title = self._live_title(title)
         timestamp = parse_iso8601(broadcast.get('created_at'))
 
         thumbnails = [{
             'url': broadcast[image],
         } for image in ('image_url', 'image_url_small') if broadcast.get(image)]
 
+        stream = self._call_api('getAccessPublic', token)
+
+        formats = []
+        for format_id in ('replay', 'rtmp', 'hls', 'https_hls'):
+            video_url = stream.get(format_id + '_url')
+            if not video_url:
+                continue
+            f = {
+                'url': video_url,
+                'ext': 'flv' if format_id == 'rtmp' else 'mp4',
+            }
+            if format_id != 'rtmp':
+                f['protocol'] = 'm3u8_native' if state == 'ended' else 'm3u8'
+            formats.append(f)
+        self._sort_formats(formats)
+
         return {
             'id': broadcast.get('id') or token,
-            'url': video_url,
-            'ext': 'mp4',
-            'protocol': 'm3u8_native',
             'title': title,
             'timestamp': timestamp,
             'uploader': uploader,
             'uploader_id': uploader_id,
             'thumbnails': thumbnails,
+            'formats': formats,
         }