]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/imdb.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / 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,
96c2e3e9 10 mimetype2ext,
0167f0db 11 parse_duration,
dd67702a 12 qualities,
5d9f6cbc 13 try_get,
3052a30d 14 url_or_none,
d8d61486
JMF
15)
16
17
18class ImdbIE(InfoExtractor):
ecfef3e5
PH
19 IE_NAME = 'imdb'
20 IE_DESC = 'Internet Movie Database trailers'
5d9f6cbc 21 _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title|list).*?[/-]vi(?P<id>\d+)'
d8d61486 22
f196508f 23 _TESTS = [{
ecfef3e5 24 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
ecfef3e5
PH
25 'info_dict': {
26 'id': '2524815897',
27 'ext': 'mp4',
5d9f6cbc 28 'title': 'No. 2',
0167f0db 29 'description': 'md5:87bd0bdc61e351f21f20d2d7441cb4e7',
5d9f6cbc 30 'duration': 152,
d8d61486 31 }
f196508f
S
32 }, {
33 'url': 'http://www.imdb.com/video/_/vi2524815897',
34 'only_matching': True,
4c93ee8d
S
35 }, {
36 'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
37 'only_matching': True,
38 }, {
39 'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
40 'only_matching': True,
13607896
S
41 }, {
42 'url': 'http://www.imdb.com/videoplayer/vi1562949145',
43 'only_matching': True,
b8457665
S
44 }, {
45 'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
46 'only_matching': True,
0167f0db
RA
47 }, {
48 'url': 'https://www.imdb.com/list/ls009921623/videoplayer/vi260482329',
49 'only_matching': True,
f196508f 50 }]
d8d61486
JMF
51
52 def _real_extract(self, url):
11fba175 53 video_id = self._match_id(url)
5d9f6cbc
S
54
55 data = self._download_json(
56 'https://www.imdb.com/ve/data/VIDEO_PLAYBACK_DATA', video_id,
57 query={
58 'key': base64.b64encode(json.dumps({
59 'type': 'VIDEO_PLAYER',
60 'subType': 'FORCE_LEGACY',
61 'id': 'vi%s' % video_id,
62 }).encode()).decode(),
63 })[0]
dd67702a 64
f7f2e53a 65 quality = qualities(('SD', '480p', '720p', '1080p'))
d8d61486 66 formats = []
5d9f6cbc 67 for encoding in data['videoLegacyEncodings']:
0167f0db 68 if not encoding or not isinstance(encoding, dict):
96c2e3e9 69 continue
5d9f6cbc 70 video_url = url_or_none(encoding.get('url'))
3052a30d 71 if not video_url:
96c2e3e9 72 continue
a62460aa
S
73 ext = mimetype2ext(encoding.get(
74 'mimeType')) or determine_ext(video_url)
0167f0db
RA
75 if ext == 'm3u8':
76 formats.extend(self._extract_m3u8_formats(
77 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
5d9f6cbc 78 preference=1, m3u8_id='hls', fatal=False))
96c2e3e9 79 continue
0167f0db
RA
80 format_id = encoding.get('definition')
81 formats.append({
82 'format_id': format_id,
83 'url': video_url,
84 'ext': ext,
85 'quality': quality(format_id),
86 })
dd67702a 87 self._sort_formats(formats)
d8d61486 88
5d9f6cbc
S
89 webpage = self._download_webpage(
90 'https://www.imdb.com/video/vi' + video_id, video_id)
91 video_metadata = self._parse_json(self._search_regex(
92 r'args\.push\(\s*({.+?})\s*\)\s*;', webpage,
93 'video metadata'), video_id)
94
95 video_info = video_metadata.get('VIDEO_INFO')
96 if video_info and isinstance(video_info, dict):
97 info = try_get(
98 video_info, lambda x: x[list(video_info.keys())[0]][0], dict)
99 else:
100 info = {}
101
102 title = self._html_search_meta(
103 ['og:title', 'twitter:title'], webpage) or self._html_search_regex(
104 r'<title>(.+?)</title>', webpage, 'title',
105 default=None) or info['videoTitle']
106
d8d61486
JMF
107 return {
108 'id': video_id,
0167f0db 109 'title': title,
5d9f6cbc 110 'alt_title': info.get('videoSubTitle'),
d8d61486 111 'formats': formats,
5d9f6cbc
S
112 'description': info.get('videoDescription'),
113 'thumbnail': url_or_none(try_get(
114 video_metadata, lambda x: x['videoSlate']['source'])),
115 'duration': parse_duration(info.get('videoRuntime')),
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)