]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/imdb.py
[videa] Improve and simplify (closes #8181, closes #11133)
[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
dd67702a 6from ..utils import (
96c2e3e9 7 mimetype2ext,
dd67702a 8 qualities,
5b9d187c 9 remove_end,
d8d61486
JMF
10)
11
12
13class ImdbIE(InfoExtractor):
ecfef3e5
PH
14 IE_NAME = 'imdb'
15 IE_DESC = 'Internet Movie Database trailers'
4c93ee8d 16 _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video/[^/]+/|title/tt\d+.*?#lb-)vi(?P<id>\d+)'
d8d61486 17
f196508f 18 _TESTS = [{
ecfef3e5 19 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
ecfef3e5
PH
20 'info_dict': {
21 'id': '2524815897',
22 'ext': 'mp4',
5b9d187c 23 'title': 'Ice Age: Continental Drift Trailer (No. 2)',
ecfef3e5 24 'description': 'md5:9061c2219254e5d14e03c25c98e96a81',
d8d61486 25 }
f196508f
S
26 }, {
27 'url': 'http://www.imdb.com/video/_/vi2524815897',
28 'only_matching': True,
4c93ee8d
S
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,
f196508f 35 }]
d8d61486
JMF
36
37 def _real_extract(self, url):
11fba175 38 video_id = self._match_id(url)
97e302a4 39 webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
11fba175
PH
40 descr = self._html_search_regex(
41 r'(?s)<span itemprop="description">(.*?)</span>',
42 webpage, 'description', fatal=False)
dd67702a
JMF
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
f7f2e53a 55 quality = qualities(('SD', '480p', '720p', '1080p'))
d8d61486 56 formats = []
dd67702a 57 for format_page in format_pages:
b03d0d06
JMF
58 json_data = self._search_regex(
59 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
ecfef3e5 60 format_page, 'json data', flags=re.DOTALL)
96c2e3e9
S
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')
d8d61486 77 formats.append({
96c2e3e9
S
78 'format_id': format_id,
79 'url': video_url,
80 'ext': mimetype2ext(video_info.get('videoMimeType')),
81 'quality': quality(format_id),
d8d61486 82 })
dd67702a 83 self._sort_formats(formats)
d8d61486
JMF
84
85 return {
86 'id': video_id,
5b9d187c 87 'title': remove_end(self._og_search_title(webpage), ' - IMDb'),
d8d61486
JMF
88 'formats': formats,
89 'description': descr,
5b9d187c 90 'thumbnail': format_info.get('slate'),
d8d61486 91 }
c645c765 92
ecfef3e5 93
c645c765 94class ImdbListIE(InfoExtractor):
ecfef3e5
PH
95 IE_NAME = 'imdb:list'
96 IE_DESC = 'Internet Movie Database lists'
92519402 97 _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/(?P<id>[\da-zA-Z_-]{11})'
22a6f150
PH
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 }
5f6a1245 106
c645c765 107 def _real_extract(self, url):
11fba175 108 list_id = self._match_id(url)
d7b51547 109 webpage = self._download_webpage(url, list_id)
d7b51547
PH
110 entries = [
111 self.url_result('http://www.imdb.com' + m, 'Imdb')
a3978a61 112 for m in re.findall(r'href="(/video/imdb/vi[^"]+)"\s+data-type="playlist"', webpage)]
d7b51547
PH
113
114 list_title = self._html_search_regex(
115 r'<h1 class="header">(.*?)</h1>', webpage, 'list title')
116
ecfef3e5 117 return self.playlist_result(entries, list_id, list_title)