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