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