]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/imdb.py
[peertube] Add support for generic embeds
[yt-dlp.git] / youtube_dl / extractor / imdb.py
CommitLineData
ecfef3e5
PH
1from __future__ import unicode_literals
2
d8d61486 3import re
d8d61486
JMF
4
5from .common import InfoExtractor
7dd6ab4a 6from ..compat import compat_str
dd67702a 7from ..utils import (
7dd6ab4a 8 determine_ext,
96c2e3e9 9 mimetype2ext,
0167f0db 10 parse_duration,
dd67702a 11 qualities,
d8d61486
JMF
12)
13
14
15class ImdbIE(InfoExtractor):
ecfef3e5
PH
16 IE_NAME = 'imdb'
17 IE_DESC = 'Internet Movie Database trailers'
0167f0db 18 _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title|list).+?[/-]vi(?P<id>\d+)'
d8d61486 19
f196508f 20 _TESTS = [{
ecfef3e5 21 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
ecfef3e5
PH
22 'info_dict': {
23 'id': '2524815897',
24 'ext': 'mp4',
0167f0db
RA
25 'title': 'No. 2 from Ice Age: Continental Drift (2012)',
26 'description': 'md5:87bd0bdc61e351f21f20d2d7441cb4e7',
d8d61486 27 }
f196508f
S
28 }, {
29 'url': 'http://www.imdb.com/video/_/vi2524815897',
30 'only_matching': True,
4c93ee8d
S
31 }, {
32 'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
33 'only_matching': True,
34 }, {
35 'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
36 'only_matching': True,
13607896
S
37 }, {
38 'url': 'http://www.imdb.com/videoplayer/vi1562949145',
39 'only_matching': True,
b8457665
S
40 }, {
41 'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
42 'only_matching': True,
0167f0db
RA
43 }, {
44 'url': 'https://www.imdb.com/list/ls009921623/videoplayer/vi260482329',
45 'only_matching': True,
f196508f 46 }]
d8d61486
JMF
47
48 def _real_extract(self, url):
11fba175 49 video_id = self._match_id(url)
0167f0db
RA
50 webpage = self._download_webpage(
51 'https://www.imdb.com/videoplayer/vi' + video_id, video_id)
52 video_metadata = self._parse_json(self._search_regex(
53 r'window\.IMDbReactInitialState\.push\(({.+?})\);', webpage,
54 'video metadata'), video_id)['videos']['videoMetadata']['vi' + video_id]
55 title = self._html_search_meta(
56 ['og:title', 'twitter:title'], webpage) or self._html_search_regex(
57 r'<title>(.+?)</title>', webpage, 'title', fatal=False) or video_metadata['title']
dd67702a 58
f7f2e53a 59 quality = qualities(('SD', '480p', '720p', '1080p'))
d8d61486 60 formats = []
0167f0db
RA
61 for encoding in video_metadata.get('encodings', []):
62 if not encoding or not isinstance(encoding, dict):
96c2e3e9 63 continue
0167f0db
RA
64 video_url = encoding.get('videoUrl')
65 if not video_url or not isinstance(video_url, compat_str):
96c2e3e9 66 continue
0167f0db
RA
67 ext = determine_ext(video_url, mimetype2ext(encoding.get('mimeType')))
68 if ext == 'm3u8':
69 formats.extend(self._extract_m3u8_formats(
70 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
71 m3u8_id='hls', fatal=False))
96c2e3e9 72 continue
0167f0db
RA
73 format_id = encoding.get('definition')
74 formats.append({
75 'format_id': format_id,
76 'url': video_url,
77 'ext': ext,
78 'quality': quality(format_id),
79 })
dd67702a 80 self._sort_formats(formats)
d8d61486
JMF
81
82 return {
83 'id': video_id,
0167f0db 84 'title': title,
d8d61486 85 'formats': formats,
0167f0db
RA
86 'description': video_metadata.get('description'),
87 'thumbnail': video_metadata.get('slate', {}).get('url'),
88 'duration': parse_duration(video_metadata.get('duration')),
d8d61486 89 }
c645c765 90
ecfef3e5 91
c645c765 92class ImdbListIE(InfoExtractor):
ecfef3e5
PH
93 IE_NAME = 'imdb:list'
94 IE_DESC = 'Internet Movie Database lists'
27694fe7 95 _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/ls(?P<id>\d{9})(?!/videoplayer/vi\d+)'
22a6f150 96 _TEST = {
0167f0db 97 'url': 'https://www.imdb.com/list/ls009921623/',
22a6f150 98 'info_dict': {
0167f0db
RA
99 'id': '009921623',
100 'title': 'The Bourne Legacy',
101 'description': 'A list of trailers, clips, and more from The Bourne Legacy, starring Jeremy Renner and Rachel Weisz.',
22a6f150 102 },
0167f0db 103 'playlist_count': 8,
22a6f150 104 }
5f6a1245 105
c645c765 106 def _real_extract(self, url):
11fba175 107 list_id = self._match_id(url)
d7b51547 108 webpage = self._download_webpage(url, list_id)
d7b51547
PH
109 entries = [
110 self.url_result('http://www.imdb.com' + m, 'Imdb')
0167f0db 111 for m in re.findall(r'href="(/list/ls%s/videoplayer/vi[^"]+)"' % list_id, webpage)]
d7b51547
PH
112
113 list_title = self._html_search_regex(
0167f0db
RA
114 r'<h1[^>]+class="[^"]*header[^"]*"[^>]*>(.*?)</h1>',
115 webpage, 'list title')
116 list_description = self._html_search_regex(
117 r'<div[^>]+class="[^"]*list-description[^"]*"[^>]*><p>(.*?)</p>',
118 webpage, 'list description')
d7b51547 119
0167f0db 120 return self.playlist_result(entries, list_id, list_title, list_description)