[svtplay] Add new extractor (Fixes #4914)
[youtube-dl] / youtube_dl / extractor / svtplay.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     determine_ext,
6 )
7
8
9 class SVTPlayIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?svtplay\.se/video/(?P<id>[0-9]+)'
11     _TEST = {
12         'url': 'http://www.svtplay.se/video/2609989/sm-veckan/sm-veckan-rally-final-sasong-1-sm-veckan-rally-final',
13         'md5': '2521cd644e862936cf2e698206e47385',
14         'info_dict': {
15             'id': '3966754',
16             'ext': 'mp4',
17             'title': 'FIFA 14 - E3 2013 Trailer',
18             'duration': 4500,
19             'thumbnail': 're:^https?://.*\.jpg$',
20         },
21     }
22
23     def _real_extract(self, url):
24         video_id = self._match_id(url)
25         info = self._download_json(
26             'http://www.svtplay.se/video/%s?output=json' % video_id, video_id)
27
28         title = info['context']['title']
29         thumbnail = info['context'].get('thumbnailImage')
30
31         video_info = info['video']
32         formats = []
33         for vr in video_info['videoReferences']:
34             vurl = vr['url']
35             if determine_ext(vurl) == 'm3u8':
36                 formats.extend(self._extract_m3u8_formats(
37                     vurl, video_id,
38                     ext='mp4', entry_protocol='m3u8_native',
39                     m3u8_id=vr.get('playerType')))
40             else:
41                 formats.append({
42                     'format_id': vr.get('playerType'),
43                     'url': vurl,
44                 })
45         self._sort_formats(formats)
46
47         duration = video_info.get('materialLength')
48
49         return {
50             'id': video_id,
51             'title': title,
52             'formats': formats,
53             'thumbnail': thumbnail,
54             'duration': duration,
55         }