[subtitles] Added tests to check correct behavior when no subtitles are
[youtube-dl] / youtube_dl / extractor / myvideo.py
1 import binascii
2 import base64
3 import hashlib
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_ord,
9     compat_urllib_parse,
10
11     ExtractorError,
12 )
13
14
15
16 class MyVideoIE(InfoExtractor):
17     """Information Extractor for myvideo.de."""
18
19     _VALID_URL = r'(?:http://)?(?:www\.)?myvideo\.de/watch/([0-9]+)/([^?/]+).*'
20     IE_NAME = u'myvideo'
21     _TEST = {
22         u'url': u'http://www.myvideo.de/watch/8229274/bowling_fail_or_win',
23         u'file': u'8229274.flv',
24         u'md5': u'2d2753e8130479ba2cb7e0a37002053e',
25         u'info_dict': {
26             u"title": u"bowling-fail-or-win"
27         }
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
49     def __md5(self,s):
50         return hashlib.md5(s).hexdigest().encode()
51
52     def _real_extract(self,url):
53         mobj = re.match(self._VALID_URL, url)
54         if mobj is None:
55             raise ExtractorError(u'invalid URL: %s' % url)
56
57         video_id = mobj.group(1)
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, u'title')
76
77             video_ext = self._search_regex('[.](.+?)$', video_url, u'extension')
78
79             return [{
80                 'id':       video_id,
81                 'url':      video_url,
82                 'uploader': None,
83                 'upload_date':  None,
84                 'title':    video_title,
85                 'ext':      video_ext,
86             }]
87
88         # try encxml
89         mobj = re.search('var flashvars={(.+?)}', webpage)
90         if mobj is None:
91             raise ExtractorError(u'Unable to extract video')
92
93         params = {}
94         encxml = ''
95         sec = mobj.group(1)
96         for (a, b) in re.findall('(.+?):\'(.+?)\',?', sec):
97             if not a == '_encxml':
98                 params[a] = b
99             else:
100                 encxml = compat_urllib_parse.unquote(b)
101         if not params.get('domain'):
102             params['domain'] = 'www.myvideo.de'
103         xmldata_url = '%s?%s' % (encxml, compat_urllib_parse.urlencode(params))
104         if 'flash_playertype=MTV' in xmldata_url:
105             self._downloader.report_warning(u'avoiding MTV player')
106             xmldata_url = (
107                 'http://www.myvideo.de/dynamic/get_player_video_xml.php'
108                 '?flash_playertype=D&ID=%s&_countlimit=4&autorun=yes'
109             ) % video_id
110
111         # get enc data
112         enc_data = self._download_webpage(xmldata_url, video_id).split('=')[1]
113         enc_data_b = binascii.unhexlify(enc_data)
114         sk = self.__md5(
115             base64.b64decode(base64.b64decode(GK)) +
116             self.__md5(
117                 str(video_id).encode('utf-8')
118             )
119         )
120         dec_data = self.__rc4crypt(enc_data_b, sk)
121
122         # extracting infos
123         self.report_extraction(video_id)
124
125         video_url = None
126         mobj = re.search('connectionurl=\'(.*?)\'', dec_data)
127         if mobj:
128             video_url = compat_urllib_parse.unquote(mobj.group(1))
129             if 'myvideo2flash' in video_url:
130                 self._downloader.report_warning(u'forcing RTMPT ...')
131                 video_url = video_url.replace('rtmpe://', 'rtmpt://')
132
133         if not video_url:
134             # extract non rtmp videos
135             mobj = re.search('path=\'(http.*?)\' source=\'(.*?)\'', dec_data)
136             if mobj is None:
137                 raise ExtractorError(u'unable to extract url')
138             video_url = compat_urllib_parse.unquote(mobj.group(1)) + compat_urllib_parse.unquote(mobj.group(2))
139
140         video_file = self._search_regex('source=\'(.*?)\'', dec_data, u'video file')
141         video_file = compat_urllib_parse.unquote(video_file)
142
143         if not video_file.endswith('f4m'):
144             ppath, prefix = video_file.split('.')
145             video_playpath = '%s:%s' % (prefix, ppath)
146             video_hls_playlist = ''
147         else:
148             video_playpath = ''
149             video_hls_playlist = (
150                 video_file
151             ).replace('.f4m', '.m3u8')
152
153         video_swfobj = self._search_regex('swfobject.embedSWF\(\'(.+?)\'', webpage, u'swfobj')
154         video_swfobj = compat_urllib_parse.unquote(video_swfobj)
155
156         video_title = self._html_search_regex("<h1(?: class='globalHd')?>(.*?)</h1>",
157             webpage, u'title')
158
159         return [{
160             'id':                 video_id,
161             'url':                video_url,
162             'tc_url':             video_url,
163             'uploader':           None,
164             'upload_date':        None,
165             'title':              video_title,
166             'ext':                u'flv',
167             'play_path':          video_playpath,
168             'video_file':         video_file,
169             'video_hls_playlist': video_hls_playlist,
170             'player_url':         video_swfobj,
171         }]
172