Merge pull request #14225 from Tithen-Firion/openload-phantomjs-method
[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 ..utils import (
8     determine_ext,
9     ExtractorError,
10     get_element_by_id,
11     PhantomJSwrapper,
12 )
13
14
15 class OpenloadIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:openload\.(?:co|io)|oload\.tv)/(?: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': r'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': r'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         'url': 'https://oload.tv/embed/KnG-kKZdcfY/',
59         'only_matching': True,
60     }]
61
62     _USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
63
64     @staticmethod
65     def _extract_urls(webpage):
66         return re.findall(
67             r'<iframe[^>]+src=["\']((?:https?://)?(?:openload\.(?:co|io)|oload\.tv)/embed/[a-zA-Z0-9-_]+)',
68             webpage)
69
70     def _real_extract(self, url):
71         video_id = self._match_id(url)
72         url = 'https://openload.co/embed/%s/' % video_id
73         headers = {
74             'User-Agent': self._USER_AGENT,
75         }
76
77         webpage = self._download_webpage(url, video_id, headers=headers)
78
79         if 'File not found' in webpage or 'deleted by the owner' in webpage:
80             raise ExtractorError('File not found', expected=True, video_id=video_id)
81
82         phantom = PhantomJSwrapper(self, required_version='2.0')
83         webpage, _ = phantom.get(url, html=webpage, video_id=video_id, headers=headers)
84
85         decoded_id = get_element_by_id('streamurl', webpage)
86
87         video_url = 'https://openload.co/stream/%s?mime=true' % decoded_id
88
89         title = self._og_search_title(webpage, default=None) or self._search_regex(
90             r'<span[^>]+class=["\']title["\'][^>]*>([^<]+)', webpage,
91             'title', default=None) or self._html_search_meta(
92             'description', webpage, 'title', fatal=True)
93
94         entries = self._parse_html5_media_entries(url, webpage, video_id)
95         entry = entries[0] if entries else {}
96         subtitles = entry.get('subtitles')
97
98         info_dict = {
99             'id': video_id,
100             'title': title,
101             'thumbnail': entry.get('thumbnail') or 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, 'mp4'),
105             'subtitles': subtitles,
106             'http_headers': headers,
107         }
108         return info_dict