[vzaar] Add support for HLS
[ytdl] / youtube_dl / extractor / vzaar.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     int_or_none,
10     float_or_none,
11     unified_timestamp,
12     url_or_none,
13 )
14
15
16 class VzaarIE(InfoExtractor):
17     _VALID_URL = r'https?://(?:(?:www|view)\.)?vzaar\.com/(?:videos/)?(?P<id>\d+)'
18     _TESTS = [{
19         # HTTP and HLS
20         'url': 'https://vzaar.com/videos/1152805',
21         'md5': 'bde5ddfeb104a6c56a93a06b04901dbf',
22         'info_dict': {
23             'id': '1152805',
24             'ext': 'mp4',
25             'title': 'sample video (public)',
26         },
27     }, {
28         'url': 'https://view.vzaar.com/27272/player',
29         'md5': '3b50012ac9bbce7f445550d54e0508f2',
30         'info_dict': {
31             'id': '27272',
32             'ext': 'mp3',
33             'title': 'MP3',
34         },
35     }]
36
37     @staticmethod
38     def _extract_urls(webpage):
39         return re.findall(
40             r'<iframe[^>]+src=["\']((?:https?:)?//(?:view\.vzaar\.com)/[0-9]+)',
41             webpage)
42
43     def _real_extract(self, url):
44         video_id = self._match_id(url)
45         video_data = self._download_json(
46             'http://view.vzaar.com/v2/%s/video' % video_id, video_id)
47
48         title = video_data['videoTitle']
49
50         formats = []
51
52         source_url = url_or_none(video_data.get('sourceUrl'))
53         if source_url:
54             f = {
55                 'url': source_url,
56                 'format_id': 'http',
57             }
58             if 'audio' in source_url:
59                 f.update({
60                     'vcodec': 'none',
61                     'ext': 'mp3',
62                 })
63             else:
64                 f.update({
65                     'width': int_or_none(video_data.get('width')),
66                     'height': int_or_none(video_data.get('height')),
67                     'ext': 'mp4',
68                     'fps': float_or_none(video_data.get('fps')),
69                 })
70             formats.append(f)
71
72         video_guid = video_data.get('guid')
73         usp = video_data.get('usp')
74         if isinstance(video_guid, compat_str) and isinstance(usp, dict):
75             m3u8_url = ('http://fable.vzaar.com/v4/usp/%s/%s.ism/.m3u8?'
76                         % (video_guid, video_id)) + '&'.join(
77                 '%s=%s' % (k, v) for k, v in usp.items())
78             formats.extend(self._extract_m3u8_formats(
79                 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
80                 m3u8_id='hls', fatal=False))
81
82         self._sort_formats(formats)
83
84         return {
85             'id': video_id,
86             'title': title,
87             'thumbnail': self._proto_relative_url(video_data.get('poster')),
88             'duration': float_or_none(video_data.get('videoDuration')),
89             'timestamp': unified_timestamp(video_data.get('ts')),
90             'formats': formats,
91         }