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.0 KiB

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