[youtube] fix extraction for embed restricted live streams(fixes #16433)
[youtube-dl] / youtube_dl / extractor / imdb.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8     determine_ext,
9     mimetype2ext,
10     qualities,
11     remove_end,
12 )
13
14
15 class ImdbIE(InfoExtractor):
16     IE_NAME = 'imdb'
17     IE_DESC = 'Internet Movie Database trailers'
18     _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title).+?[/-]vi(?P<id>\d+)'
19
20     _TESTS = [{
21         'url': 'http://www.imdb.com/video/imdb/vi2524815897',
22         'info_dict': {
23             'id': '2524815897',
24             'ext': 'mp4',
25             'title': 'Ice Age: Continental Drift Trailer (No. 2)',
26             'description': 'md5:9061c2219254e5d14e03c25c98e96a81',
27         }
28     }, {
29         'url': 'http://www.imdb.com/video/_/vi2524815897',
30         'only_matching': True,
31     }, {
32         'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
33         'only_matching': True,
34     }, {
35         'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
36         'only_matching': True,
37     }, {
38         'url': 'http://www.imdb.com/videoplayer/vi1562949145',
39         'only_matching': True,
40     }, {
41         'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
42         'only_matching': True,
43     }]
44
45     def _real_extract(self, url):
46         video_id = self._match_id(url)
47         webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
48         descr = self._html_search_regex(
49             r'(?s)<span itemprop="description">(.*?)</span>',
50             webpage, 'description', fatal=False)
51         player_url = 'http://www.imdb.com/video/imdb/vi%s/imdb/single' % video_id
52         player_page = self._download_webpage(
53             player_url, video_id, 'Downloading player page')
54         # the player page contains the info for the default format, we have to
55         # fetch other pages for the rest of the formats
56         extra_formats = re.findall(r'href="(?P<url>%s.*?)".*?>(?P<name>.*?)<' % re.escape(player_url), player_page)
57         format_pages = [
58             self._download_webpage(
59                 f_url, video_id, 'Downloading info for %s format' % f_name)
60             for f_url, f_name in extra_formats]
61         format_pages.append(player_page)
62
63         quality = qualities(('SD', '480p', '720p', '1080p'))
64         formats = []
65         for format_page in format_pages:
66             json_data = self._search_regex(
67                 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
68                 format_page, 'json data', flags=re.DOTALL)
69             info = self._parse_json(json_data, video_id, fatal=False)
70             if not info:
71                 continue
72             format_info = info.get('videoPlayerObject', {}).get('video', {})
73             if not format_info:
74                 continue
75             video_info_list = format_info.get('videoInfoList')
76             if not video_info_list or not isinstance(video_info_list, list):
77                 continue
78             for video_info in video_info_list:
79                 if not video_info or not isinstance(video_info, dict):
80                     continue
81                 video_url = video_info.get('videoUrl')
82                 if not video_url or not isinstance(video_url, compat_str):
83                     continue
84                 if (video_info.get('videoMimeType') == 'application/x-mpegURL' or
85                         determine_ext(video_url) == 'm3u8'):
86                     formats.extend(self._extract_m3u8_formats(
87                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
88                         m3u8_id='hls', fatal=False))
89                     continue
90                 format_id = format_info.get('ffname')
91                 formats.append({
92                     'format_id': format_id,
93                     'url': video_url,
94                     'ext': mimetype2ext(video_info.get('videoMimeType')),
95                     'quality': quality(format_id),
96                 })
97         self._sort_formats(formats)
98
99         return {
100             'id': video_id,
101             'title': remove_end(self._og_search_title(webpage), ' - IMDb'),
102             'formats': formats,
103             'description': descr,
104             'thumbnail': format_info.get('slate'),
105         }
106
107
108 class ImdbListIE(InfoExtractor):
109     IE_NAME = 'imdb:list'
110     IE_DESC = 'Internet Movie Database lists'
111     _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/(?P<id>[\da-zA-Z_-]{11})'
112     _TEST = {
113         'url': 'http://www.imdb.com/list/JFs9NWw6XI0',
114         'info_dict': {
115             'id': 'JFs9NWw6XI0',
116             'title': 'March 23, 2012 Releases',
117         },
118         'playlist_count': 7,
119     }
120
121     def _real_extract(self, url):
122         list_id = self._match_id(url)
123         webpage = self._download_webpage(url, list_id)
124         entries = [
125             self.url_result('http://www.imdb.com' + m, 'Imdb')
126             for m in re.findall(r'href="(/video/imdb/vi[^"]+)"\s+data-type="playlist"', webpage)]
127
128         list_title = self._html_search_regex(
129             r'<h1 class="header">(.*?)</h1>', webpage, 'list title')
130
131         return self.playlist_result(entries, list_id, list_title)