myvideo.py 6.1 KB

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