[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / kanalplay.py
index 869757ec044b82caf3c16af60a51847863558989..6c3498c6722e2312631cb6a327be4a90ba6d102f 100644 (file)
@@ -7,6 +7,7 @@ from .common import InfoExtractor
 from ..utils import (
     ExtractorError,
     float_or_none,
+    srt_subtitles_timecode,
 )
 
 
@@ -15,14 +16,17 @@ class KanalPlayIE(InfoExtractor):
     _VALID_URL = r'https?://(?:www\.)?kanal(?P<channel_id>5|9|11)play\.se/(?:#!/)?(?:play/)?program/\d+/video/(?P<id>\d+)'
     _TESTS = [{
         'url': 'http://www.kanal5play.se/#!/play/program/3060212363/video/3270012277',
-        'md5': '',
         'info_dict': {
-            'id': '2609989',
+            'id': '3270012277',
             'ext': 'flv',
             'title': 'Saknar både dusch och avlopp',
-            'description': 'md5:',
+            'description': 'md5:6023a95832a06059832ae93bc3c7efb7',
             'duration': 2636.36,
         },
+        'params': {
+            # rtmp download
+            'skip_download': True,
+        }
     }, {
         'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
         'only_matching': True,
@@ -31,6 +35,22 @@ class KanalPlayIE(InfoExtractor):
         'only_matching': True,
     }]
 
+    def _fix_subtitles(self, subs):
+        return '\r\n\r\n'.join(
+            '%s\r\n%s --> %s\r\n%s'
+            % (
+                num,
+                srt_subtitles_timecode(item['startMillis'] / 1000.0),
+                srt_subtitles_timecode(item['endMillis'] / 1000.0),
+                item['text'],
+            ) for num, item in enumerate(subs, 1))
+
+    def _get_subtitles(self, channel_id, video_id):
+        subs = self._download_json(
+            'http://www.kanal%splay.se/api/subtitles/%s' % (channel_id, video_id),
+            video_id, 'Downloading subtitles JSON', fatal=False)
+        return {'sv': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} if subs else {}
+
     def _real_extract(self, url):
         mobj = re.match(self._VALID_URL, url)
         video_id = mobj.group('id')
@@ -62,6 +82,10 @@ class KanalPlayIE(InfoExtractor):
         } for stream in video['streams']]
         self._sort_formats(formats)
 
+        subtitles = {}
+        if video.get('hasSubtitle'):
+            subtitles = self.extract_subtitles(channel_id, video_id)
+
         return {
             'id': video_id,
             'title': title,
@@ -69,4 +93,5 @@ class KanalPlayIE(InfoExtractor):
             'thumbnail': thumbnail,
             'duration': duration,
             'formats': formats,
+            'subtitles': subtitles,
         }