]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/myvideo.py
[SpankBang] Use python2.6 compatible string formatting spec
[youtube-dl] / youtube_dl / extractor / myvideo.py
1 from __future__ import unicode_literals
2
3 import binascii
4 import base64
5 import hashlib
6 import re
7 import json
8
9 from .common import InfoExtractor
10 from ..compat import (
11     compat_ord,
12     compat_urllib_parse,
13     compat_urllib_request,
14 )
15 from ..utils import (
16     ExtractorError,
17 )
18
19
20 class MyVideoIE(InfoExtractor):
21     _VALID_URL = r'http://(?:www\.)?myvideo\.de/(?:[^/]+/)?watch/(?P<id>[0-9]+)/[^?/]+.*'
22     IE_NAME = 'myvideo'
23     _TEST = {
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',
30         }
31     }
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
36     def __rc4crypt(self, data, key):
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
52     def __md5(self, s):
53         return hashlib.md5(s).hexdigest().encode()
54
55     def _real_extract(self, url):
56         mobj = re.match(self._VALID_URL, url)
57         video_id = mobj.group('id')
58
59         GK = (
60             b'WXpnME1EZGhNRGhpTTJNM01XVmhOREU0WldNNVpHTTJOakpt'
61             b'TW1FMU5tVTBNR05pWkRaa05XRXhNVFJoWVRVd1ptSXhaVEV3'
62             b'TnpsbA0KTVRkbU1tSTRNdz09'
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>',
75                                                   webpage, 'title')
76
77             return {
78                 'id': video_id,
79                 'url': video_url,
80                 'title': video_title,
81             }
82
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,
87                                               'Downloading video info')
88             info = json.loads(base64.b64decode(response).decode('utf-8'))
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             }
97
98         # try encxml
99         mobj = re.search('var flashvars={(.+?)}', webpage)
100         if mobj is None:
101             raise ExtractorError('Unable to extract video')
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:
115             self._downloader.report_warning('avoiding MTV player')
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:
140                 self.report_warning(
141                     'Rewriting URL to use unencrypted rtmp:// ...',
142                     video_id)
143                 video_url = video_url.replace('rtmpe://', 'rtmp://')
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:
149                 raise ExtractorError('unable to extract url')
150             video_url = compat_urllib_parse.unquote(mobj.group(1)) + compat_urllib_parse.unquote(mobj.group(2))
151
152         video_file = self._search_regex('source=\'(.*?)\'', dec_data, 'video file')
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)
158         else:
159             video_playpath = ''
160
161         video_swfobj = self._search_regex('swfobject.embedSWF\(\'(.+?)\'', webpage, 'swfobj')
162         video_swfobj = compat_urllib_parse.unquote(video_swfobj)
163
164         video_title = self._html_search_regex("<h1(?: class='globalHd')?>(.*?)</h1>",
165                                               webpage, 'title')
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         }