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