]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/miomio.py
[ie/box] Fix formats extraction (#8649)
[yt-dlp.git] / yt_dlp / extractor / miomio.py
CommitLineData
f5b66911
S
1import random
2
c41a2ec4 3from .common import InfoExtractor
c3baaedf 4from ..compat import compat_urlparse
3d2623a8 5from ..networking import Request
6from ..utils import ExtractorError, int_or_none, xpath_text
c41a2ec4 7
8
e03bfb30 9class MioMioIE(InfoExtractor):
c41a2ec4 10 IE_NAME = 'miomio.tv'
11 _VALID_URL = r'https?://(?:www\.)?miomio\.tv/watch/cc(?P<id>[0-9]+)'
f5b66911 12 _TESTS = [{
8da1bb04
YCH
13 # "type=video" in flashvars
14 'url': 'http://www.miomio.tv/watch/cc88912/',
c41a2ec4 15 'info_dict': {
8da1bb04 16 'id': '88912',
f5b66911 17 'ext': 'flv',
8da1bb04
YCH
18 'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
19 'duration': 5923,
f5b66911 20 },
a9a3b4a0 21 'skip': 'Unable to load videos',
f5b66911
S
22 }, {
23 'url': 'http://www.miomio.tv/watch/cc184024/',
24 'info_dict': {
25 'id': '43729',
26 'title': '《动漫同人插画绘制》',
27 },
28 'playlist_mincount': 86,
ae018501 29 'skip': 'Unable to load videos',
8b0e8990
YCH
30 }, {
31 'url': 'http://www.miomio.tv/watch/cc173113/',
32 'info_dict': {
33 'id': '173113',
34 'title': 'The New Macbook 2015 上手试玩与简评'
35 },
36 'playlist_mincount': 2,
ae018501 37 'skip': 'Unable to load videos',
c3baaedf
YCH
38 }, {
39 # new 'h5' player
a9a3b4a0
YCH
40 'url': 'http://www.miomio.tv/watch/cc273997/',
41 'md5': '0b27a4b4495055d826813f8c3a6b2070',
c3baaedf 42 'info_dict': {
a9a3b4a0 43 'id': '273997',
c3baaedf 44 'ext': 'mp4',
a9a3b4a0 45 'title': 'マツコの知らない世界【劇的進化SP!ビニール傘&冷凍食品2016】 1_2 - 16 05 31',
c3baaedf 46 },
0f6b87d0 47 'skip': 'Unable to load videos',
f5b66911 48 }]
c41a2ec4 49
c3baaedf 50 def _extract_mioplayer(self, webpage, video_id, title, http_headers):
f5b66911 51 xml_config = self._search_regex(
8da1bb04 52 r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
f5b66911 53 webpage, 'xml config')
4bbeb19f 54
5d1f0e60 55 # skipping the following page causes lags and eventually connection drop-outs
f5b66911
S
56 self._request_webpage(
57 'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/xml.php?id=%s&r=%s' % (id, random.randint(100, 999)),
58 video_id)
5d1f0e60 59
3d2623a8 60 vid_config_request = Request(
f5b66911 61 'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
e68dd192 62 headers=http_headers)
6953d8e9 63
64 # the following xml contains the actual configuration information on the video file(s)
65 vid_config = self._download_xml(vid_config_request, video_id)
c41a2ec4 66
8da1bb04
YCH
67 if not int_or_none(xpath_text(vid_config, 'timelength')):
68 raise ExtractorError('Unable to load videos!', expected=True)
69
c41a2ec4 70 entries = []
f5b66911
S
71 for f in vid_config.findall('./durl'):
72 segment_url = xpath_text(f, 'url', 'video url')
73 if not segment_url:
74 continue
75 order = xpath_text(f, 'order', 'order')
76 segment_id = video_id
77 segment_title = title
78 if order:
79 segment_id += '-%s' % order
80 segment_title += ' part %s' % order
c41a2ec4 81 entries.append({
82 'id': segment_id,
f5b66911 83 'url': segment_url,
c41a2ec4 84 'title': segment_title,
f5b66911
S
85 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
86 'http_headers': http_headers,
c41a2ec4 87 })
88
c3baaedf
YCH
89 return entries
90
0f6b87d0
YCH
91 def _download_chinese_webpage(self, *args, **kwargs):
92 # Requests with English locales return garbage
93 headers = {
94 'Accept-Language': 'zh-TW,en-US;q=0.7,en;q=0.3',
95 }
96 kwargs.setdefault('headers', {}).update(headers)
97 return self._download_webpage(*args, **kwargs)
98
c3baaedf
YCH
99 def _real_extract(self, url):
100 video_id = self._match_id(url)
0f6b87d0
YCH
101 webpage = self._download_chinese_webpage(
102 url, video_id)
c3baaedf
YCH
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)
0f6b87d0 112 player_webpage = self._download_chinese_webpage(
c3baaedf
YCH
113 player_url, video_id,
114 note='Downloading player webpage', headers={'Referer': url})
a9a3b4a0 115 entries = self._parse_html5_media_entries(player_url, player_webpage, video_id)
c3baaedf
YCH
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
c41a2ec4 121 if len(entries) == 1:
f5b66911
S
122 segment = entries[0]
123 segment['id'] = video_id
124 segment['title'] = title
c3baaedf 125 segment['http_headers'] = http_headers
f5b66911 126 return segment
c41a2ec4 127
128 return {
129 '_type': 'multi_video',
130 'id': video_id,
c41a2ec4 131 'entries': entries,
f5b66911
S
132 'title': title,
133 'http_headers': http_headers,
c41a2ec4 134 }