Improve URL extraction
[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 = 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))
72                 continue
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             })
80         self._sort_formats(formats)
81
82         return {
83             'id': video_id,
84             'title': title,
85             'formats': formats,
86             'description': video_metadata.get('description'),
87             'thumbnail': video_metadata.get('slate', {}).get('url'),
88             'duration': parse_duration(video_metadata.get('duration')),
89         }
90
91
92 class ImdbListIE(InfoExtractor):
93     IE_NAME = 'imdb:list'
94     IE_DESC = 'Internet Movie Database lists'
95     _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/ls(?P<id>\d{9})(?!/videoplayer/vi\d+)'
96     _TEST = {
97         'url': 'https://www.imdb.com/list/ls009921623/',
98         'info_dict': {
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.',
102         },
103         'playlist_count': 8,
104     }
105
106     def _real_extract(self, url):
107         list_id = self._match_id(url)
108         webpage = self._download_webpage(url, list_id)
109         entries = [
110             self.url_result('http://www.imdb.com' + m, 'Imdb')
111             for m in re.findall(r'href="(/list/ls%s/videoplayer/vi[^"]+)"' % list_id, webpage)]
112
113         list_title = self._html_search_regex(
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')
119
120         return self.playlist_result(entries, list_id, list_title, list_description)