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