[openload] Fix extraction (closes #10408)
[youtube-dl] / youtube_dl / extractor / openload.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_chr
6 from ..utils import (
7     determine_ext,
8     ExtractorError,
9 )
10
11
12 class OpenloadIE(InfoExtractor):
13     _VALID_URL = r'https?://openload\.(?:co|io)/(?:f|embed)/(?P<id>[a-zA-Z0-9-_]+)'
14
15     _TESTS = [{
16         'url': 'https://openload.co/f/kUEfGclsU9o',
17         'md5': 'bf1c059b004ebc7a256f89408e65c36e',
18         'info_dict': {
19             'id': 'kUEfGclsU9o',
20             'ext': 'mp4',
21             'title': 'skyrim_no-audio_1080.mp4',
22             'thumbnail': 're:^https?://.*\.jpg$',
23         },
24     }, {
25         'url': 'https://openload.co/embed/rjC09fkPLYs',
26         'info_dict': {
27             'id': 'rjC09fkPLYs',
28             'ext': 'mp4',
29             'title': 'movie.mp4',
30             'thumbnail': 're:^https?://.*\.jpg$',
31             'subtitles': {
32                 'en': [{
33                     'ext': 'vtt',
34                 }],
35             },
36         },
37         'params': {
38             'skip_download': True,  # test subtitles only
39         },
40     }, {
41         'url': 'https://openload.co/embed/kUEfGclsU9o/skyrim_no-audio_1080.mp4',
42         'only_matching': True,
43     }, {
44         'url': 'https://openload.io/f/ZAn6oz-VZGE/',
45         'only_matching': True,
46     }, {
47         'url': 'https://openload.co/f/_-ztPaZtMhM/',
48         'only_matching': True,
49     }, {
50         # unavailable via https://openload.co/f/Sxz5sADo82g/, different layout
51         # for title and ext
52         'url': 'https://openload.co/embed/Sxz5sADo82g/',
53         'only_matching': True,
54     }]
55
56     def _real_extract(self, url):
57         video_id = self._match_id(url)
58         webpage = self._download_webpage('https://openload.co/embed/%s/' % video_id, video_id)
59
60         if 'File not found' in webpage or 'deleted by the owner' in webpage:
61             raise ExtractorError('File not found', expected=True)
62
63         ol_id = self._search_regex(
64             '<span[^>]+id="[a-zA-Z0-9]+x"[^>]*>([0-9]+)</span>',
65             webpage, 'openload ID')
66
67         first_two_chars = int(float(ol_id[0:][:2]))
68         urlcode = ''
69         num = 2
70
71         while num < len(ol_id):
72             urlcode += compat_chr(int(float(ol_id[num:][:3])) -
73                                   first_two_chars * int(float(ol_id[num + 3:][:2])))
74             num += 5
75
76         video_url = 'https://openload.co/stream/' + urlcode
77
78         title = self._og_search_title(webpage, default=None) or self._search_regex(
79             r'<span[^>]+class=["\']title["\'][^>]*>([^<]+)', webpage,
80             'title', default=None) or self._html_search_meta(
81             'description', webpage, 'title', fatal=True)
82
83         entries = self._parse_html5_media_entries(url, webpage, video_id)
84         subtitles = entries[0]['subtitles'] if entries else None
85
86         info_dict = {
87             'id': video_id,
88             'title': title,
89             'thumbnail': self._og_search_thumbnail(webpage, default=None),
90             'url': video_url,
91             # Seems all videos have extensions in their titles
92             'ext': determine_ext(title),
93             'subtitles': subtitles,
94         }
95         return info_dict