]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/miomio.py
Merge pull request #1 from e7appew/python3
[youtubedl] / 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 'info_dict': {
23 'id': '88912',
24 'ext': 'flv',
25 'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
26 'duration': 5923,
27 },
28 'params': {
29 # The server provides broken file
30 'skip_download': True,
31 }
32 }, {
33 'url': 'http://www.miomio.tv/watch/cc184024/',
34 'info_dict': {
35 'id': '43729',
36 'title': '《动漫同人插画绘制》',
37 },
38 'playlist_mincount': 86,
39 'skip': 'Unable to load videos',
40 }, {
41 'url': 'http://www.miomio.tv/watch/cc173113/',
42 'info_dict': {
43 'id': '173113',
44 'title': 'The New Macbook 2015 上手试玩与简评'
45 },
46 'playlist_mincount': 2,
47 'skip': 'Unable to load videos',
48 }, {
49 # new 'h5' player
50 'url': 'http://www.miomio.tv/watch/cc273295/',
51 'md5': '',
52 'info_dict': {
53 'id': '273295',
54 'ext': 'mp4',
55 'title': 'アウト×デラックス 20160526',
56 },
57 'params': {
58 # intermittent HTTP 500
59 'skip_download': True,
60 },
61 }]
62
63 def _extract_mioplayer(self, webpage, video_id, title, http_headers):
64 xml_config = self._search_regex(
65 r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
66 webpage, 'xml config')
67
68 # skipping the following page causes lags and eventually connection drop-outs
69 self._request_webpage(
70 'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/xml.php?id=%s&r=%s' % (id, random.randint(100, 999)),
71 video_id)
72
73 vid_config_request = sanitized_Request(
74 'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
75 headers=http_headers)
76
77 # the following xml contains the actual configuration information on the video file(s)
78 vid_config = self._download_xml(vid_config_request, video_id)
79
80 if not int_or_none(xpath_text(vid_config, 'timelength')):
81 raise ExtractorError('Unable to load videos!', expected=True)
82
83 entries = []
84 for f in vid_config.findall('./durl'):
85 segment_url = xpath_text(f, 'url', 'video url')
86 if not segment_url:
87 continue
88 order = xpath_text(f, 'order', 'order')
89 segment_id = video_id
90 segment_title = title
91 if order:
92 segment_id += '-%s' % order
93 segment_title += ' part %s' % order
94 entries.append({
95 'id': segment_id,
96 'url': segment_url,
97 'title': segment_title,
98 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
99 'http_headers': http_headers,
100 })
101
102 return entries
103
104 def _real_extract(self, url):
105 video_id = self._match_id(url)
106 webpage = self._download_webpage(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_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)
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 }