[openload] Fix ext extraction
[youtube-dl] / youtube_dl / extractor / openload.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_chr
8 from ..utils import (
9     determine_ext,
10     encode_base_n,
11     ExtractorError,
12     mimetype2ext,
13 )
14
15
16 class OpenloadIE(InfoExtractor):
17     _VALID_URL = r'https://openload.(?:co|io)/(?:f|embed)/(?P<id>[a-zA-Z0-9-]+)'
18
19     _TESTS = [{
20         'url': 'https://openload.co/f/kUEfGclsU9o',
21         'md5': 'bf1c059b004ebc7a256f89408e65c36e',
22         'info_dict': {
23             'id': 'kUEfGclsU9o',
24             'ext': 'mp4',
25             'title': 'skyrim_no-audio_1080.mp4',
26             'thumbnail': 're:^https?://.*\.jpg$',
27         },
28     }, {
29         'url': 'https://openload.co/embed/kUEfGclsU9o/skyrim_no-audio_1080.mp4',
30         'only_matching': True,
31     }, {
32         'url': 'https://openload.io/f/ZAn6oz-VZGE/',
33         'only_matching': True,
34     }]
35
36     @staticmethod
37     def openload_level2_debase(m):
38         radix, num = int(m.group(1)) + 27, int(m.group(2))
39         return '"' + encode_base_n(num, radix) + '"'
40
41     @classmethod
42     def openload_level2(cls, txt):
43         # The function name is ǃ \u01c3
44         # Using escaped unicode literals does not work in Python 3.2
45         return re.sub(r'ǃ\((\d+),(\d+)\)', cls.openload_level2_debase, txt, re.UNICODE).replace('"+"', '')
46
47     # Openload uses a variant of aadecode
48     # openload_decode and related functions are originally written by
49     # vitas@matfyz.cz and released with public domain
50     # See https://github.com/rg3/youtube-dl/issues/8489
51     @classmethod
52     def openload_decode(cls, txt):
53         symbol_table = [
54             ('_', '(゚Д゚) [゚Θ゚]'),
55             ('a', '(゚Д゚) [゚ω゚ノ]'),
56             ('b', '(゚Д゚) [゚Θ゚ノ]'),
57             ('c', '(゚Д゚) [\'c\']'),
58             ('d', '(゚Д゚) [゚ー゚ノ]'),
59             ('e', '(゚Д゚) [゚Д゚ノ]'),
60             ('f', '(゚Д゚) [1]'),
61
62             ('o', '(゚Д゚) [\'o\']'),
63             ('u', '(o゚ー゚o)'),
64             ('c', '(゚Д゚) [\'c\']'),
65
66             ('7', '((゚ー゚) + (o^_^o))'),
67             ('6', '((o^_^o) +(o^_^o) +(c^_^o))'),
68             ('5', '((゚ー゚) + (゚Θ゚))'),
69             ('4', '(-~3)'),
70             ('3', '(-~-~1)'),
71             ('2', '(-~1)'),
72             ('1', '(-~0)'),
73             ('0', '((c^_^o)-(c^_^o))'),
74         ]
75         delim = '(゚Д゚)[゚ε゚]+'
76         ret = ''
77         for aachar in txt.split(delim):
78             for val, pat in symbol_table:
79                 aachar = aachar.replace(pat, val)
80             aachar = aachar.replace('+ ', '')
81             m = re.match(r'^\d+', aachar)
82             if m:
83                 ret += compat_chr(int(m.group(0), 8))
84             else:
85                 m = re.match(r'^u([\da-f]+)', aachar)
86                 if m:
87                     ret += compat_chr(int(m.group(1), 16))
88         return cls.openload_level2(ret)
89
90     def _real_extract(self, url):
91         video_id = self._match_id(url)
92         webpage = self._download_webpage(url, video_id)
93
94         if 'File not found' in webpage:
95             raise ExtractorError('File not found', expected=True)
96
97         code = self._search_regex(
98             r'<video[^>]+>\s*<script[^>]+>([^<]+)</script>',
99             webpage, 'JS code')
100
101         decoded = self.openload_decode(code)
102
103         video_url = self._search_regex(
104             r'return\s+"(https?://[^"]+)"', decoded, 'video URL')
105
106         title = self._og_search_title(webpage, default=None) or self._search_regex(
107             r'<span[^>]+class=["\']title["\'][^>]*>([^<]+)', webpage,
108             'title', default=None) or self._html_search_meta(
109             'description', webpage, 'title', fatal=True)
110
111         ext = mimetype2ext(self._search_regex(
112             r'window\.vt\s*=\s*(["\'])(?P<mimetype>.+?)\1', decoded,
113             'mimetype', default=None, group='mimetype')) or determine_ext(
114             video_url, 'mp4')
115
116         return {
117             'id': video_id,
118             'title': title,
119             'ext': ext,
120             'thumbnail': self._og_search_thumbnail(webpage),
121             'url': video_url,
122         }