1from __future__ import unicode_literals
2
3
4from .common import InfoExtractor
5from ..utils import (
6    dict_get,
7    float_or_none,
8)
9
10
11class PlaywireIE(InfoExtractor):
12    _VALID_URL = r'https?://(?:config|cdn)\.playwire\.com(?:/v2)?/(?P<publisher_id>\d+)/(?:videos/v2|embed|config)/(?P<id>\d+)'
13    _TESTS = [{
14        'url': 'http://config.playwire.com/14907/videos/v2/3353705/player.json',
15        'md5': 'e6398701e3595888125729eaa2329ed9',
16        'info_dict': {
17            'id': '3353705',
18            'ext': 'mp4',
19            'title': 'S04_RM_UCL_Rus',
20            'thumbnail': r're:^https?://.*\.png$',
21            'duration': 145.94,
22        },
23    }, {
24        # m3u8 in f4m
25        'url': 'http://config.playwire.com/21772/videos/v2/4840492/zeus.json',
26        'info_dict': {
27            'id': '4840492',
28            'ext': 'mp4',
29            'title': 'ITV EL SHOW FULL',
30        },
31        'params': {
32            # m3u8 download
33            'skip_download': True,
34        },
35    }, {
36        # Multiple resolutions while bitrates missing
37        'url': 'http://cdn.playwire.com/11625/embed/85228.html',
38        'only_matching': True,
39    }, {
40        'url': 'http://config.playwire.com/12421/videos/v2/3389892/zeus.json',
41        'only_matching': True,
42    }, {
43        'url': 'http://cdn.playwire.com/v2/12342/config/1532636.json',
44        'only_matching': True,
45    }]
46
47    def _real_extract(self, url):
48        mobj = self._match_valid_url(url)
49        publisher_id, video_id = mobj.group('publisher_id'), mobj.group('id')
50
51        player = self._download_json(
52            'http://config.playwire.com/%s/videos/v2/%s/zeus.json' % (publisher_id, video_id),
53            video_id)
54
55        title = player['settings']['title']
56        duration = float_or_none(player.get('duration'), 1000)
57
58        content = player['content']
59        thumbnail = content.get('poster')
60        src = content['media']['f4m']
61
62        formats = self._extract_f4m_formats(src, video_id, m3u8_id='hls')
63        for a_format in formats:
64            if not dict_get(a_format, ['tbr', 'width', 'height']):
65                a_format['quality'] = 1 if '-hd.' in a_format['url'] else 0
66        self._sort_formats(formats)
67
68        return {
69            'id': video_id,
70            'title': title,
71            'thumbnail': thumbnail,
72            'duration': duration,
73            'formats': formats,
74        }
75