[imdb] Improve title extraction and make thumbnail non-fatal
[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/tt\d+.*?#lb-)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
37     def _real_extract(self, url):
38         video_id = self._match_id(url)
39         webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
40         descr = self._html_search_regex(
41             r'(?s)<span itemprop="description">(.*?)</span>',
42             webpage, 'description', fatal=False)
43         player_url = 'http://www.imdb.com/video/imdb/vi%s/imdb/single' % video_id
44         player_page = self._download_webpage(
45             player_url, video_id, 'Downloading player page')
46         # the player page contains the info for the default format, we have to
47         # fetch other pages for the rest of the formats
48         extra_formats = re.findall(r'href="(?P<url>%s.*?)".*?>(?P<name>.*?)<' % re.escape(player_url), player_page)
49         format_pages = [
50             self._download_webpage(
51                 f_url, video_id, 'Downloading info for %s format' % f_name)
52             for f_url, f_name in extra_formats]
53         format_pages.append(player_page)
54
55         quality = qualities(('SD', '480p', '720p', '1080p'))
56         formats = []
57         for format_page in format_pages:
58             json_data = self._search_regex(
59                 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
60                 format_page, 'json data', flags=re.DOTALL)
61             info = self._parse_json(json_data, video_id, fatal=False)
62             if not info:
63                 continue
64             format_info = info.get('videoPlayerObject', {}).get('video', {})
65             if not format_info:
66                 continue
67             video_info_list = format_info.get('videoInfoList')
68             if not video_info_list or not isinstance(video_info_list, list):
69                 continue
70             video_info = video_info_list[0]
71             if not video_info or not isinstance(video_info, dict):
72                 continue
73             video_url = video_info.get('videoUrl')
74             if not video_url:
75                 continue
76             format_id = format_info.get('ffname')
77             formats.append({
78                 'format_id': format_id,
79                 'url': video_url,
80                 'ext': mimetype2ext(video_info.get('videoMimeType')),
81                 'quality': quality(format_id),
82             })
83         self._sort_formats(formats)
84
85         return {
86             'id': video_id,
87             'title': remove_end(self._og_search_title(webpage), ' - IMDb'),
88             'formats': formats,
89             'description': descr,
90             'thumbnail': format_info.get('slate'),
91         }
92
93
94 class ImdbListIE(InfoExtractor):
95     IE_NAME = 'imdb:list'
96     IE_DESC = 'Internet Movie Database lists'
97     _VALID_URL = r'https?://www\.imdb\.com/list/(?P<id>[\da-zA-Z_-]{11})'
98     _TEST = {
99         'url': 'http://www.imdb.com/list/JFs9NWw6XI0',
100         'info_dict': {
101             'id': 'JFs9NWw6XI0',
102             'title': 'March 23, 2012 Releases',
103         },
104         'playlist_count': 7,
105     }
106
107     def _real_extract(self, url):
108         list_id = self._match_id(url)
109         webpage = self._download_webpage(url, list_id)
110         entries = [
111             self.url_result('http://www.imdb.com' + m, 'Imdb')
112             for m in re.findall(r'href="(/video/imdb/vi[^"]+)"\s+data-type="playlist"', webpage)]
113
114         list_title = self._html_search_regex(
115             r'<h1 class="header">(.*?)</h1>', webpage, 'list title')
116
117         return self.playlist_result(entries, list_id, list_title)