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