[miomio] Support new 'h5' player (closes #9605)
[youtube-dl] / youtube_dl / extractor / miomio.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import random
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8 from ..utils import (
9     xpath_text,
10     int_or_none,
11     ExtractorError,
12     sanitized_Request,
13 )
14
15
16 class MioMioIE(InfoExtractor):
17     IE_NAME = 'miomio.tv'
18     _VALID_URL = r'https?://(?:www\.)?miomio\.tv/watch/cc(?P<id>[0-9]+)'
19     _TESTS = [{
20         # "type=video" in flashvars
21         'url': 'http://www.miomio.tv/watch/cc88912/',
22         'md5': '317a5f7f6b544ce8419b784ca8edae65',
23         'info_dict': {
24             'id': '88912',
25             'ext': 'flv',
26             'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
27             'duration': 5923,
28         },
29     }, {
30         'url': 'http://www.miomio.tv/watch/cc184024/',
31         'info_dict': {
32             'id': '43729',
33             'title': '《动漫同人插画绘制》',
34         },
35         'playlist_mincount': 86,
36         'skip': 'This video takes time too long for retrieving the URL',
37     }, {
38         'url': 'http://www.miomio.tv/watch/cc173113/',
39         'info_dict': {
40             'id': '173113',
41             'title': 'The New Macbook 2015 上手试玩与简评'
42         },
43         'playlist_mincount': 2,
44     }, {
45         # new 'h5' player
46         'url': 'http://www.miomio.tv/watch/cc273295/',
47         'md5': '',
48         'info_dict': {
49             'id': '273295',
50             'ext': 'mp4',
51             'title': 'アウト×デラックス 20160526',
52         },
53         'params': {
54             # intermittent HTTP 500
55             'skip_download': True,
56         },
57     }]
58
59     def _extract_mioplayer(self, webpage, video_id, title, http_headers):
60         xml_config = self._search_regex(
61             r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
62             webpage, 'xml config')
63
64         # skipping the following page causes lags and eventually connection drop-outs
65         self._request_webpage(
66             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/xml.php?id=%s&r=%s' % (id, random.randint(100, 999)),
67             video_id)
68
69         vid_config_request = sanitized_Request(
70             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
71             headers=http_headers)
72
73         # the following xml contains the actual configuration information on the video file(s)
74         vid_config = self._download_xml(vid_config_request, video_id)
75
76         if not int_or_none(xpath_text(vid_config, 'timelength')):
77             raise ExtractorError('Unable to load videos!', expected=True)
78
79         entries = []
80         for f in vid_config.findall('./durl'):
81             segment_url = xpath_text(f, 'url', 'video url')
82             if not segment_url:
83                 continue
84             order = xpath_text(f, 'order', 'order')
85             segment_id = video_id
86             segment_title = title
87             if order:
88                 segment_id += '-%s' % order
89                 segment_title += ' part %s' % order
90             entries.append({
91                 'id': segment_id,
92                 'url': segment_url,
93                 'title': segment_title,
94                 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
95                 'http_headers': http_headers,
96             })
97
98         return entries
99
100     def _real_extract(self, url):
101         video_id = self._match_id(url)
102         webpage = self._download_webpage(url, video_id)
103
104         title = self._html_search_meta(
105             'description', webpage, 'title', fatal=True)
106
107         mioplayer_path = self._search_regex(
108             r'src="(/mioplayer(?:_h5)?/[^"]+)"', webpage, 'ref_path')
109
110         if '_h5' in mioplayer_path:
111             player_url = compat_urlparse.urljoin(url, mioplayer_path)
112             player_webpage = self._download_webpage(
113                 player_url, video_id,
114                 note='Downloading player webpage', headers={'Referer': url})
115             entries = self._parse_html5_media_entries(player_url, player_webpage)
116             http_headers = {'Referer': player_url}
117         else:
118             http_headers = {'Referer': 'http://www.miomio.tv%s' % mioplayer_path}
119             entries = self._extract_mioplayer(webpage, video_id, title, http_headers)
120
121         if len(entries) == 1:
122             segment = entries[0]
123             segment['id'] = video_id
124             segment['title'] = title
125             segment['http_headers'] = http_headers
126             return segment
127
128         return {
129             '_type': 'multi_video',
130             'id': video_id,
131             'entries': entries,
132             'title': title,
133             'http_headers': http_headers,
134         }