You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.1 KiB

10 years ago
10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import binascii
  3. import base64
  4. import hashlib
  5. import re
  6. import json
  7. from .common import InfoExtractor
  8. from ..compat import (
  9. compat_ord,
  10. compat_urllib_parse,
  11. compat_urllib_parse_unquote,
  12. compat_urllib_request,
  13. )
  14. from ..utils import (
  15. ExtractorError,
  16. )
  17. class MyVideoIE(InfoExtractor):
  18. _VALID_URL = r'http://(?:www\.)?myvideo\.de/(?:[^/]+/)?watch/(?P<id>[0-9]+)/[^?/]+.*'
  19. IE_NAME = 'myvideo'
  20. _TEST = {
  21. 'url': 'http://www.myvideo.de/watch/8229274/bowling_fail_or_win',
  22. 'md5': '2d2753e8130479ba2cb7e0a37002053e',
  23. 'info_dict': {
  24. 'id': '8229274',
  25. 'ext': 'flv',
  26. 'title': 'bowling-fail-or-win',
  27. }
  28. }
  29. # Original Code from: https://github.com/dersphere/plugin.video.myvideo_de.git
  30. # Released into the Public Domain by Tristan Fischer on 2013-05-19
  31. # https://github.com/rg3/youtube-dl/pull/842
  32. def __rc4crypt(self, data, key):
  33. x = 0
  34. box = list(range(256))
  35. for i in list(range(256)):
  36. x = (x + box[i] + compat_ord(key[i % len(key)])) % 256
  37. box[i], box[x] = box[x], box[i]
  38. x = 0
  39. y = 0
  40. out = ''
  41. for char in data:
  42. x = (x + 1) % 256
  43. y = (y + box[x]) % 256
  44. box[x], box[y] = box[y], box[x]
  45. out += chr(compat_ord(char) ^ box[(box[x] + box[y]) % 256])
  46. return out
  47. def __md5(self, s):
  48. return hashlib.md5(s).hexdigest().encode()
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VALID_URL, url)
  51. video_id = mobj.group('id')
  52. GK = (
  53. b'WXpnME1EZGhNRGhpTTJNM01XVmhOREU0WldNNVpHTTJOakpt'
  54. b'TW1FMU5tVTBNR05pWkRaa05XRXhNVFJoWVRVd1ptSXhaVEV3'
  55. b'TnpsbA0KTVRkbU1tSTRNdz09'
  56. )
  57. # Get video webpage
  58. webpage_url = 'http://www.myvideo.de/watch/%s' % video_id
  59. webpage = self._download_webpage(webpage_url, video_id)
  60. mobj = re.search('source src=\'(.+?)[.]([^.]+)\'', webpage)
  61. if mobj is not None:
  62. self.report_extraction(video_id)
  63. video_url = mobj.group(1) + '.flv'
  64. video_title = self._html_search_regex('<title>([^<]+)</title>',
  65. webpage, 'title')
  66. return {
  67. 'id': video_id,
  68. 'url': video_url,
  69. 'title': video_title,
  70. }
  71. mobj = re.search(r'data-video-service="/service/data/video/%s/config' % video_id, webpage)
  72. if mobj is not None:
  73. request = compat_urllib_request.Request('http://www.myvideo.de/service/data/video/%s/config' % video_id, '')
  74. response = self._download_webpage(request, video_id,
  75. 'Downloading video info')
  76. info = json.loads(base64.b64decode(response).decode('utf-8'))
  77. return {
  78. 'id': video_id,
  79. 'title': info['title'],
  80. 'url': info['streaming_url'].replace('rtmpe', 'rtmpt'),
  81. 'play_path': info['filename'],
  82. 'ext': 'flv',
  83. 'thumbnail': info['thumbnail'][0]['url'],
  84. }
  85. # try encxml
  86. mobj = re.search('var flashvars={(.+?)}', webpage)
  87. if mobj is None:
  88. raise ExtractorError('Unable to extract video')
  89. params = {}
  90. encxml = ''
  91. sec = mobj.group(1)
  92. for (a, b) in re.findall('(.+?):\'(.+?)\',?', sec):
  93. if not a == '_encxml':
  94. params[a] = b
  95. else:
  96. encxml = compat_urllib_parse_unquote(b)
  97. if not params.get('domain'):
  98. params['domain'] = 'www.myvideo.de'
  99. xmldata_url = '%s?%s' % (encxml, compat_urllib_parse.urlencode(params))
  100. if 'flash_playertype=MTV' in xmldata_url:
  101. self._downloader.report_warning('avoiding MTV player')
  102. xmldata_url = (
  103. 'http://www.myvideo.de/dynamic/get_player_video_xml.php'
  104. '?flash_playertype=D&ID=%s&_countlimit=4&autorun=yes'
  105. ) % video_id
  106. # get enc data
  107. enc_data = self._download_webpage(xmldata_url, video_id).split('=')[1]
  108. enc_data_b = binascii.unhexlify(enc_data)
  109. sk = self.__md5(
  110. base64.b64decode(base64.b64decode(GK)) +
  111. self.__md5(
  112. str(video_id).encode('utf-8')
  113. )
  114. )
  115. dec_data = self.__rc4crypt(enc_data_b, sk)
  116. # extracting infos
  117. self.report_extraction(video_id)
  118. video_url = None
  119. mobj = re.search('connectionurl=\'(.*?)\'', dec_data)
  120. if mobj:
  121. video_url = compat_urllib_parse_unquote(mobj.group(1))
  122. if 'myvideo2flash' in video_url:
  123. self.report_warning(
  124. 'Rewriting URL to use unencrypted rtmp:// ...',
  125. video_id)
  126. video_url = video_url.replace('rtmpe://', 'rtmp://')
  127. if not video_url:
  128. # extract non rtmp videos
  129. mobj = re.search('path=\'(http.*?)\' source=\'(.*?)\'', dec_data)
  130. if mobj is None:
  131. raise ExtractorError('unable to extract url')
  132. video_url = compat_urllib_parse_unquote(mobj.group(1)) + compat_urllib_parse_unquote(mobj.group(2))
  133. video_file = self._search_regex('source=\'(.*?)\'', dec_data, 'video file')
  134. video_file = compat_urllib_parse_unquote(video_file)
  135. if not video_file.endswith('f4m'):
  136. ppath, prefix = video_file.split('.')
  137. video_playpath = '%s:%s' % (prefix, ppath)
  138. else:
  139. video_playpath = ''
  140. video_swfobj = self._search_regex('swfobject.embedSWF\(\'(.+?)\'', webpage, 'swfobj')
  141. video_swfobj = compat_urllib_parse_unquote(video_swfobj)
  142. video_title = self._html_search_regex("<h1(?: class='globalHd')?>(.*?)</h1>",
  143. webpage, 'title')
  144. return {
  145. 'id': video_id,
  146. 'url': video_url,
  147. 'tc_url': video_url,
  148. 'title': video_title,
  149. 'ext': 'flv',
  150. 'play_path': video_playpath,
  151. 'player_url': video_swfobj,
  152. }