]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imdb.py
Merge pull request #1 from e7appew/python3
[youtubedl] / 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 mimetype2ext,
8 qualities,
9 )
10
11
12 class ImdbIE(InfoExtractor):
13 IE_NAME = 'imdb'
14 IE_DESC = 'Internet Movie Database trailers'
15 _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video/[^/]+/|title/tt\d+.*?#lb-)vi(?P<id>\d+)'
16
17 _TESTS = [{
18 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
19 'info_dict': {
20 'id': '2524815897',
21 'ext': 'mp4',
22 'title': 'Ice Age: Continental Drift Trailer (No. 2) - IMDb',
23 'description': 'md5:9061c2219254e5d14e03c25c98e96a81',
24 }
25 }, {
26 'url': 'http://www.imdb.com/video/_/vi2524815897',
27 'only_matching': True,
28 }, {
29 'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
30 'only_matching': True,
31 }, {
32 'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
33 'only_matching': True,
34 }]
35
36 def _real_extract(self, url):
37 video_id = self._match_id(url)
38 webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
39 descr = self._html_search_regex(
40 r'(?s)<span itemprop="description">(.*?)</span>',
41 webpage, 'description', fatal=False)
42 player_url = 'http://www.imdb.com/video/imdb/vi%s/imdb/single' % video_id
43 player_page = self._download_webpage(
44 player_url, video_id, 'Downloading player page')
45 # the player page contains the info for the default format, we have to
46 # fetch other pages for the rest of the formats
47 extra_formats = re.findall(r'href="(?P<url>%s.*?)".*?>(?P<name>.*?)<' % re.escape(player_url), player_page)
48 format_pages = [
49 self._download_webpage(
50 f_url, video_id, 'Downloading info for %s format' % f_name)
51 for f_url, f_name in extra_formats]
52 format_pages.append(player_page)
53
54 quality = qualities(('SD', '480p', '720p', '1080p'))
55 formats = []
56 for format_page in format_pages:
57 json_data = self._search_regex(
58 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
59 format_page, 'json data', flags=re.DOTALL)
60 info = self._parse_json(json_data, video_id, fatal=False)
61 if not info:
62 continue
63 format_info = info.get('videoPlayerObject', {}).get('video', {})
64 if not format_info:
65 continue
66 video_info_list = format_info.get('videoInfoList')
67 if not video_info_list or not isinstance(video_info_list, list):
68 continue
69 video_info = video_info_list[0]
70 if not video_info or not isinstance(video_info, dict):
71 continue
72 video_url = video_info.get('videoUrl')
73 if not video_url:
74 continue
75 format_id = format_info.get('ffname')
76 formats.append({
77 'format_id': format_id,
78 'url': video_url,
79 'ext': mimetype2ext(video_info.get('videoMimeType')),
80 'quality': quality(format_id),
81 })
82 self._sort_formats(formats)
83
84 return {
85 'id': video_id,
86 'title': self._og_search_title(webpage),
87 'formats': formats,
88 'description': descr,
89 'thumbnail': format_info['slate'],
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/(?P<id>[\da-zA-Z_-]{11})'
97 _TEST = {
98 'url': 'http://www.imdb.com/list/JFs9NWw6XI0',
99 'info_dict': {
100 'id': 'JFs9NWw6XI0',
101 'title': 'March 23, 2012 Releases',
102 },
103 'playlist_count': 7,
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="(/video/imdb/vi[^"]+)"\s+data-type="playlist"', webpage)]
112
113 list_title = self._html_search_regex(
114 r'<h1 class="header">(.*?)</h1>', webpage, 'list title')
115
116 return self.playlist_result(entries, list_id, list_title)