]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/miomio.py
[ie/box] Fix formats extraction (#8649)
[yt-dlp.git] / yt_dlp / extractor / miomio.py
1 import random
2
3 from .common import InfoExtractor
4 from ..compat import compat_urlparse
5 from ..networking import Request
6 from ..utils import ExtractorError, int_or_none, xpath_text
7
8
9 class MioMioIE(InfoExtractor):
10 IE_NAME = 'miomio.tv'
11 _VALID_URL = r'https?://(?:www\.)?miomio\.tv/watch/cc(?P<id>[0-9]+)'
12 _TESTS = [{
13 # "type=video" in flashvars
14 'url': 'http://www.miomio.tv/watch/cc88912/',
15 'info_dict': {
16 'id': '88912',
17 'ext': 'flv',
18 'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
19 'duration': 5923,
20 },
21 'skip': 'Unable to load videos',
22 }, {
23 'url': 'http://www.miomio.tv/watch/cc184024/',
24 'info_dict': {
25 'id': '43729',
26 'title': '《动漫同人插画绘制》',
27 },
28 'playlist_mincount': 86,
29 'skip': 'Unable to load videos',
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,
37 'skip': 'Unable to load videos',
38 }, {
39 # new 'h5' player
40 'url': 'http://www.miomio.tv/watch/cc273997/',
41 'md5': '0b27a4b4495055d826813f8c3a6b2070',
42 'info_dict': {
43 'id': '273997',
44 'ext': 'mp4',
45 'title': 'マツコの知らない世界【劇的進化SP!ビニール傘&冷凍食品2016】 1_2 - 16 05 31',
46 },
47 'skip': 'Unable to load videos',
48 }]
49
50 def _extract_mioplayer(self, webpage, video_id, title, http_headers):
51 xml_config = self._search_regex(
52 r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
53 webpage, 'xml config')
54
55 # skipping the following page causes lags and eventually connection drop-outs
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)
59
60 vid_config_request = Request(
61 'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
62 headers=http_headers)
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)
66
67 if not int_or_none(xpath_text(vid_config, 'timelength')):
68 raise ExtractorError('Unable to load videos!', expected=True)
69
70 entries = []
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
81 entries.append({
82 'id': segment_id,
83 'url': segment_url,
84 'title': segment_title,
85 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
86 'http_headers': http_headers,
87 })
88
89 return entries
90
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
99 def _real_extract(self, url):
100 video_id = self._match_id(url)
101 webpage = self._download_chinese_webpage(
102 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_chinese_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, video_id)
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 }