Merge pull request #10971 from kasper93/openload
[youtube-dl] / youtube_dl / extractor / openload.py
1 # coding: utf-8
2 from __future__ import unicode_literals, division
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_chr,
7     compat_ord,
8 )
9 from ..utils import (
10     determine_ext,
11     ExtractorError,
12 )
13
14
15 class OpenloadIE(InfoExtractor):
16     _VALID_URL = r'https?://openload\.(?:co|io)/(?:f|embed)/(?P<id>[a-zA-Z0-9-_]+)'
17
18     _TESTS = [{
19         'url': 'https://openload.co/f/kUEfGclsU9o',
20         'md5': 'bf1c059b004ebc7a256f89408e65c36e',
21         'info_dict': {
22             'id': 'kUEfGclsU9o',
23             'ext': 'mp4',
24             'title': 'skyrim_no-audio_1080.mp4',
25             'thumbnail': 're:^https?://.*\.jpg$',
26         },
27     }, {
28         'url': 'https://openload.co/embed/rjC09fkPLYs',
29         'info_dict': {
30             'id': 'rjC09fkPLYs',
31             'ext': 'mp4',
32             'title': 'movie.mp4',
33             'thumbnail': 're:^https?://.*\.jpg$',
34             'subtitles': {
35                 'en': [{
36                     'ext': 'vtt',
37                 }],
38             },
39         },
40         'params': {
41             'skip_download': True,  # test subtitles only
42         },
43     }, {
44         'url': 'https://openload.co/embed/kUEfGclsU9o/skyrim_no-audio_1080.mp4',
45         'only_matching': True,
46     }, {
47         'url': 'https://openload.io/f/ZAn6oz-VZGE/',
48         'only_matching': True,
49     }, {
50         'url': 'https://openload.co/f/_-ztPaZtMhM/',
51         'only_matching': True,
52     }, {
53         # unavailable via https://openload.co/f/Sxz5sADo82g/, different layout
54         # for title and ext
55         'url': 'https://openload.co/embed/Sxz5sADo82g/',
56         'only_matching': True,
57     }]
58
59     def _real_extract(self, url):
60         video_id = self._match_id(url)
61         webpage = self._download_webpage('https://openload.co/embed/%s/' % video_id, video_id)
62
63         if 'File not found' in webpage or 'deleted by the owner' in webpage:
64             raise ExtractorError('File not found', expected=True)
65
66         # The following decryption algorithm is written by @yokrysty and
67         # declared to be freely used in youtube-dl
68         # See https://github.com/rg3/youtube-dl/issues/10408
69         enc_data = self._html_search_regex(
70             r'<span[^>]*>([^<]+)</span>\s*<span[^>]*>[^<]+</span>\s*<span[^>]+id="streamurl"',
71             webpage, 'encrypted data')
72
73         magic = compat_ord(enc_data[-1])
74         video_url_chars = []
75
76         for idx, c in enumerate(enc_data):
77             j = compat_ord(c)
78             if j == magic:
79                 j -= 1
80             elif j == magic - 1:
81                 j += 1
82             if j >= 33 and j <= 126:
83                 j = ((j + 14) % 94) + 33
84             if idx == len(enc_data) - 1:
85                 j += 2
86             video_url_chars += compat_chr(j)
87
88         video_url = 'https://openload.co/stream/%s?mime=true' % ''.join(video_url_chars)
89
90         title = self._og_search_title(webpage, default=None) or self._search_regex(
91             r'<span[^>]+class=["\']title["\'][^>]*>([^<]+)', webpage,
92             'title', default=None) or self._html_search_meta(
93             'description', webpage, 'title', fatal=True)
94
95         entries = self._parse_html5_media_entries(url, webpage, video_id)
96         subtitles = entries[0]['subtitles'] if entries else None
97
98         info_dict = {
99             'id': video_id,
100             'title': title,
101             'thumbnail': self._og_search_thumbnail(webpage, default=None),
102             'url': video_url,
103             # Seems all videos have extensions in their titles
104             'ext': determine_ext(title),
105             'subtitles': subtitles,
106         }
107
108         return info_dict