[miomio] Adapt to the new API and update _TESTS
[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         'info_dict': {
23             'id': '88912',
24             'ext': 'flv',
25             'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
26             'duration': 5923,
27         },
28         'skip': 'Unable to load videos',
29     }, {
30         'url': 'http://www.miomio.tv/watch/cc184024/',
31         'info_dict': {
32             'id': '43729',
33             'title': '《动漫同人插画绘制》',
34         },
35         'playlist_mincount': 86,
36         'skip': 'Unable to load videos',
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         'skip': 'Unable to load videos',
45     }, {
46         # new 'h5' player
47         'url': 'http://www.miomio.tv/watch/cc273997/',
48         'md5': '0b27a4b4495055d826813f8c3a6b2070',
49         'info_dict': {
50             'id': '273997',
51             'ext': 'mp4',
52             'title': 'マツコの知らない世界【劇的進化SP!ビニール傘&冷凍食品2016】 1_2 - 16 05 31',
53         },
54     }]
55
56     def _extract_mioplayer(self, webpage, video_id, title, http_headers):
57         xml_config = self._search_regex(
58             r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
59             webpage, 'xml config')
60
61         # skipping the following page causes lags and eventually connection drop-outs
62         self._request_webpage(
63             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/xml.php?id=%s&r=%s' % (id, random.randint(100, 999)),
64             video_id)
65
66         vid_config_request = sanitized_Request(
67             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
68             headers=http_headers)
69
70         # the following xml contains the actual configuration information on the video file(s)
71         vid_config = self._download_xml(vid_config_request, video_id)
72
73         if not int_or_none(xpath_text(vid_config, 'timelength')):
74             raise ExtractorError('Unable to load videos!', expected=True)
75
76         entries = []
77         for f in vid_config.findall('./durl'):
78             segment_url = xpath_text(f, 'url', 'video url')
79             if not segment_url:
80                 continue
81             order = xpath_text(f, 'order', 'order')
82             segment_id = video_id
83             segment_title = title
84             if order:
85                 segment_id += '-%s' % order
86                 segment_title += ' part %s' % order
87             entries.append({
88                 'id': segment_id,
89                 'url': segment_url,
90                 'title': segment_title,
91                 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
92                 'http_headers': http_headers,
93             })
94
95         return entries
96
97     def _real_extract(self, url):
98         video_id = self._match_id(url)
99         webpage = self._download_webpage(url, video_id)
100
101         title = self._html_search_meta(
102             'description', webpage, 'title', fatal=True)
103
104         mioplayer_path = self._search_regex(
105             r'src="(/mioplayer(?:_h5)?/[^"]+)"', webpage, 'ref_path')
106
107         if '_h5' in mioplayer_path:
108             player_url = compat_urlparse.urljoin(url, mioplayer_path)
109             player_webpage = self._download_webpage(
110                 player_url, video_id,
111                 note='Downloading player webpage', headers={'Referer': url})
112             entries = self._parse_html5_media_entries(player_url, player_webpage, video_id)
113             http_headers = {'Referer': player_url}
114         else:
115             http_headers = {'Referer': 'http://www.miomio.tv%s' % mioplayer_path}
116             entries = self._extract_mioplayer(webpage, video_id, title, http_headers)
117
118         if len(entries) == 1:
119             segment = entries[0]
120             segment['id'] = video_id
121             segment['title'] = title
122             segment['http_headers'] = http_headers
123             return segment
124
125         return {
126             '_type': 'multi_video',
127             'id': video_id,
128             'entries': entries,
129             'title': title,
130             'http_headers': http_headers,
131         }