Merge branch 'gfycat' of https://github.com/julianrichen/youtube-dl into julianrichen...
[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 ..utils import (
8     xpath_text,
9     int_or_none,
10     ExtractorError,
11 )
12
13
14 class MioMioIE(InfoExtractor):
15     IE_NAME = 'miomio.tv'
16     _VALID_URL = r'https?://(?:www\.)?miomio\.tv/watch/cc(?P<id>[0-9]+)'
17     _TESTS = [{
18         # "type=video" in flashvars
19         'url': 'http://www.miomio.tv/watch/cc88912/',
20         'md5': '317a5f7f6b544ce8419b784ca8edae65',
21         'info_dict': {
22             'id': '88912',
23             'ext': 'flv',
24             'title': '【SKY】字幕 铠武昭和VS平成 假面骑士大战FEAT战队 魔星字幕组 字幕',
25             'duration': 5923,
26         },
27     }, {
28         'url': 'http://www.miomio.tv/watch/cc184024/',
29         'info_dict': {
30             'id': '43729',
31             'title': '《动漫同人插画绘制》',
32         },
33         'playlist_mincount': 86,
34     }]
35
36     def _real_extract(self, url):
37         video_id = self._match_id(url)
38         webpage = self._download_webpage(url, video_id)
39
40         title = self._html_search_meta(
41             'description', webpage, 'title', fatal=True)
42
43         mioplayer_path = self._search_regex(
44             r'src="(/mioplayer/[^"]+)"', webpage, 'ref_path')
45
46         xml_config = self._search_regex(
47             r'flashvars="type=(?:sina|video)&amp;(.+?)&amp;',
48             webpage, 'xml config')
49
50         # skipping the following page causes lags and eventually connection drop-outs
51         self._request_webpage(
52             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/xml.php?id=%s&r=%s' % (id, random.randint(100, 999)),
53             video_id)
54
55         # the following xml contains the actual configuration information on the video file(s)
56         vid_config = self._download_xml(
57             'http://www.miomio.tv/mioplayer/mioplayerconfigfiles/sina.php?{0}'.format(xml_config),
58             video_id)
59
60         http_headers = {
61             'Referer': 'http://www.miomio.tv%s' % mioplayer_path,
62         }
63
64         if not int_or_none(xpath_text(vid_config, 'timelength')):
65             raise ExtractorError('Unable to load videos!', expected=True)
66
67         entries = []
68         for f in vid_config.findall('./durl'):
69             segment_url = xpath_text(f, 'url', 'video url')
70             if not segment_url:
71                 continue
72             order = xpath_text(f, 'order', 'order')
73             segment_id = video_id
74             segment_title = title
75             if order:
76                 segment_id += '-%s' % order
77                 segment_title += ' part %s' % order
78             entries.append({
79                 'id': segment_id,
80                 'url': segment_url,
81                 'title': segment_title,
82                 'duration': int_or_none(xpath_text(f, 'length', 'duration'), 1000),
83                 'http_headers': http_headers,
84             })
85
86         if len(entries) == 1:
87             segment = entries[0]
88             segment['id'] = video_id
89             segment['title'] = title
90             return segment
91
92         return {
93             '_type': 'multi_video',
94             'id': video_id,
95             'entries': entries,
96             'title': title,
97             'http_headers': http_headers,
98         }