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