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.

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