]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/imdb.py
[cleanup] Use `_html_extract_title`
[yt-dlp.git] / yt_dlp / extractor / imdb.py
CommitLineData
ecfef3e5
PH
1from __future__ import unicode_literals
2
5d9f6cbc
S
3import base64
4import json
d8d61486 5import re
d8d61486
JMF
6
7from .common import InfoExtractor
dd67702a 8from ..utils import (
7dd6ab4a 9 determine_ext,
18eac302 10 int_or_none,
96c2e3e9 11 mimetype2ext,
dd67702a 12 qualities,
18eac302 13 traverse_obj,
5d9f6cbc 14 try_get,
3052a30d 15 url_or_none,
d8d61486
JMF
16)
17
18
19class ImdbIE(InfoExtractor):
ecfef3e5
PH
20 IE_NAME = 'imdb'
21 IE_DESC = 'Internet Movie Database trailers'
5d9f6cbc 22 _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title|list).*?[/-]vi(?P<id>\d+)'
d8d61486 23
f196508f 24 _TESTS = [{
ecfef3e5 25 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
ecfef3e5
PH
26 'info_dict': {
27 'id': '2524815897',
28 'ext': 'mp4',
5d9f6cbc 29 'title': 'No. 2',
0167f0db 30 'description': 'md5:87bd0bdc61e351f21f20d2d7441cb4e7',
5d9f6cbc 31 'duration': 152,
18eac302
HTL
32 'thumbnail': r're:^https?://.+\.jpg',
33 }
34 }, {
35 'url': 'https://www.imdb.com/video/vi3516832537',
36 'info_dict': {
37 'id': '3516832537',
38 'ext': 'mp4',
39 'title': 'Paul: U.S. Trailer #1',
40 'description': 'md5:17fcc4fe11ec29b4399be9d4c5ef126c',
41 'duration': 153,
42 'thumbnail': r're:^https?://.+\.jpg',
d8d61486 43 }
f196508f
S
44 }, {
45 'url': 'http://www.imdb.com/video/_/vi2524815897',
46 'only_matching': True,
4c93ee8d
S
47 }, {
48 'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
49 'only_matching': True,
50 }, {
51 'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
52 'only_matching': True,
13607896
S
53 }, {
54 'url': 'http://www.imdb.com/videoplayer/vi1562949145',
55 'only_matching': True,
b8457665
S
56 }, {
57 'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
58 'only_matching': True,
0167f0db
RA
59 }, {
60 'url': 'https://www.imdb.com/list/ls009921623/videoplayer/vi260482329',
61 'only_matching': True,
f196508f 62 }]
d8d61486
JMF
63
64 def _real_extract(self, url):
11fba175 65 video_id = self._match_id(url)
18eac302
HTL
66 webpage = self._download_webpage(f'https://www.imdb.com/video/vi{video_id}', video_id)
67 info = self._search_nextjs_data(webpage, video_id)
68 video_info = traverse_obj(info, ('props', 'pageProps', 'videoPlaybackData', 'video'), default={})
69 title = (traverse_obj(video_info, ('name', 'value'), ('primaryTitle', 'titleText', 'text'))
70 or self._html_search_meta(('og:title', 'twitter:title'), webpage, default=None)
04f3fd2c 71 or self._html_extract_title(webpage))
18eac302 72 data = video_info.get('playbackURLs') or try_get(self._download_json(
5d9f6cbc
S
73 'https://www.imdb.com/ve/data/VIDEO_PLAYBACK_DATA', video_id,
74 query={
75 'key': base64.b64encode(json.dumps({
76 'type': 'VIDEO_PLAYER',
77 'subType': 'FORCE_LEGACY',
78 'id': 'vi%s' % video_id,
79 }).encode()).decode(),
18eac302 80 }), lambda x: x[0]['videoLegacyEncodings'])
f7f2e53a 81 quality = qualities(('SD', '480p', '720p', '1080p'))
18eac302
HTL
82 formats, subtitles = [], {}
83 for encoding in data:
0167f0db 84 if not encoding or not isinstance(encoding, dict):
96c2e3e9 85 continue
5d9f6cbc 86 video_url = url_or_none(encoding.get('url'))
3052a30d 87 if not video_url:
96c2e3e9 88 continue
a62460aa
S
89 ext = mimetype2ext(encoding.get(
90 'mimeType')) or determine_ext(video_url)
0167f0db 91 if ext == 'm3u8':
18eac302 92 fmts, subs = self._extract_m3u8_formats_and_subtitles(
0167f0db 93 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
18eac302
HTL
94 preference=1, m3u8_id='hls', fatal=False)
95 subtitles = self._merge_subtitles(subtitles, subs)
96 formats.extend(fmts)
96c2e3e9 97 continue
18eac302 98 format_id = traverse_obj(encoding, ('displayName', 'value'), 'definition')
0167f0db
RA
99 formats.append({
100 'format_id': format_id,
101 'url': video_url,
102 'ext': ext,
103 'quality': quality(format_id),
104 })
dd67702a 105 self._sort_formats(formats)
d8d61486
JMF
106
107 return {
108 'id': video_id,
0167f0db 109 'title': title,
5d9f6cbc 110 'alt_title': info.get('videoSubTitle'),
d8d61486 111 'formats': formats,
18eac302
HTL
112 'description': try_get(video_info, lambda x: x['description']['value']),
113 'thumbnail': url_or_none(try_get(video_info, lambda x: x['thumbnail']['url'])),
114 'duration': int_or_none(try_get(video_info, lambda x: x['runtime']['value'])),
115 'subtitles': subtitles,
d8d61486 116 }
c645c765 117
ecfef3e5 118
c645c765 119class ImdbListIE(InfoExtractor):
ecfef3e5
PH
120 IE_NAME = 'imdb:list'
121 IE_DESC = 'Internet Movie Database lists'
27694fe7 122 _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/ls(?P<id>\d{9})(?!/videoplayer/vi\d+)'
22a6f150 123 _TEST = {
0167f0db 124 'url': 'https://www.imdb.com/list/ls009921623/',
22a6f150 125 'info_dict': {
0167f0db
RA
126 'id': '009921623',
127 'title': 'The Bourne Legacy',
128 'description': 'A list of trailers, clips, and more from The Bourne Legacy, starring Jeremy Renner and Rachel Weisz.',
22a6f150 129 },
0167f0db 130 'playlist_count': 8,
22a6f150 131 }
5f6a1245 132
c645c765 133 def _real_extract(self, url):
11fba175 134 list_id = self._match_id(url)
d7b51547 135 webpage = self._download_webpage(url, list_id)
d7b51547
PH
136 entries = [
137 self.url_result('http://www.imdb.com' + m, 'Imdb')
0167f0db 138 for m in re.findall(r'href="(/list/ls%s/videoplayer/vi[^"]+)"' % list_id, webpage)]
d7b51547
PH
139
140 list_title = self._html_search_regex(
0167f0db
RA
141 r'<h1[^>]+class="[^"]*header[^"]*"[^>]*>(.*?)</h1>',
142 webpage, 'list title')
143 list_description = self._html_search_regex(
144 r'<div[^>]+class="[^"]*list-description[^"]*"[^>]*><p>(.*?)</p>',
145 webpage, 'list description')
d7b51547 146
0167f0db 147 return self.playlist_result(entries, list_id, list_title, list_description)