miomio.py 4.9 KB

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