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