]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/imdb.py
[aenetworks] add encoding declaration
[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     determine_ext,
8     mimetype2ext,
9     parse_duration,
10     qualities,
11     url_or_none,
12 )
13
14
15 class ImdbIE(InfoExtractor):
16     IE_NAME = 'imdb'
17     IE_DESC = 'Internet Movie Database trailers'
18     _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title|list).+?[/-]vi(?P<id>\d+)'
19
20     _TESTS = [{
21         'url': 'http://www.imdb.com/video/imdb/vi2524815897',
22         'info_dict': {
23             'id': '2524815897',
24             'ext': 'mp4',
25             'title': 'No. 2 from Ice Age: Continental Drift (2012)',
26             'description': 'md5:87bd0bdc61e351f21f20d2d7441cb4e7',
27         }
28     }, {
29         'url': 'http://www.imdb.com/video/_/vi2524815897',
30         'only_matching': True,
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,
37     }, {
38         'url': 'http://www.imdb.com/videoplayer/vi1562949145',
39         'only_matching': True,
40     }, {
41         'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
42         'only_matching': True,
43     }, {
44         'url': 'https://www.imdb.com/list/ls009921623/videoplayer/vi260482329',
45         'only_matching': True,
46     }]
47
48     def _real_extract(self, url):
49         video_id = self._match_id(url)
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']
58
59         quality = qualities(('SD', '480p', '720p', '1080p'))
60         formats = []
61         for encoding in video_metadata.get('encodings', []):
62             if not encoding or not isinstance(encoding, dict):
63                 continue
64             video_url = url_or_none(encoding.get('videoUrl'))
65             if not video_url:
66                 continue
67             ext = mimetype2ext(encoding.get(
68                 'mimeType')) or determine_ext(video_url)
69             if ext == 'm3u8':
70                 formats.extend(self._extract_m3u8_formats(
71                     video_url, video_id, 'mp4', entry_protocol='m3u8_native',
72                     m3u8_id='hls', fatal=False))
73                 continue
74             format_id = encoding.get('definition')
75             formats.append({
76                 'format_id': format_id,
77                 'url': video_url,
78                 'ext': ext,
79                 'quality': quality(format_id),
80             })
81         self._sort_formats(formats)
82
83         return {
84             'id': video_id,
85             'title': title,
86             'formats': formats,
87             'description': video_metadata.get('description'),
88             'thumbnail': video_metadata.get('slate', {}).get('url'),
89             'duration': parse_duration(video_metadata.get('duration')),
90         }
91
92
93 class ImdbListIE(InfoExtractor):
94     IE_NAME = 'imdb:list'
95     IE_DESC = 'Internet Movie Database lists'
96     _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/ls(?P<id>\d{9})(?!/videoplayer/vi\d+)'
97     _TEST = {
98         'url': 'https://www.imdb.com/list/ls009921623/',
99         'info_dict': {
100             'id': '009921623',
101             'title': 'The Bourne Legacy',
102             'description': 'A list of trailers, clips, and more from The Bourne Legacy, starring Jeremy Renner and Rachel Weisz.',
103         },
104         'playlist_count': 8,
105     }
106
107     def _real_extract(self, url):
108         list_id = self._match_id(url)
109         webpage = self._download_webpage(url, list_id)
110         entries = [
111             self.url_result('http://www.imdb.com' + m, 'Imdb')
112             for m in re.findall(r'href="(/list/ls%s/videoplayer/vi[^"]+)"' % list_id, webpage)]
113
114         list_title = self._html_search_regex(
115             r'<h1[^>]+class="[^"]*header[^"]*"[^>]*>(.*?)</h1>',
116             webpage, 'list title')
117         list_description = self._html_search_regex(
118             r'<div[^>]+class="[^"]*list-description[^"]*"[^>]*><p>(.*?)</p>',
119             webpage, 'list description')
120
121         return self.playlist_result(entries, list_id, list_title, list_description)