[imdb] Relax _VALID_URL (Closes #9481)
[youtube-dl] / youtube_dl / extractor / imdb.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
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/[^/]+/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
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
33         descr = self._html_search_regex(
34             r'(?s)<span itemprop="description">(.*?)</span>',
35             webpage, 'description', fatal=False)
36         player_url = 'http://www.imdb.com/video/imdb/vi%s/imdb/single' % video_id
37         player_page = self._download_webpage(
38             player_url, video_id, 'Downloading player page')
39         # the player page contains the info for the default format, we have to
40         # fetch other pages for the rest of the formats
41         extra_formats = re.findall(r'href="(?P<url>%s.*?)".*?>(?P<name>.*?)<' % re.escape(player_url), player_page)
42         format_pages = [
43             self._download_webpage(
44                 f_url, video_id, 'Downloading info for %s format' % f_name)
45             for f_url, f_name in extra_formats]
46         format_pages.append(player_page)
47
48         quality = qualities(('SD', '480p', '720p', '1080p'))
49         formats = []
50         for format_page in format_pages:
51             json_data = self._search_regex(
52                 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
53                 format_page, 'json data', flags=re.DOTALL)
54             info = json.loads(json_data)
55             format_info = info['videoPlayerObject']['video']
56             f_id = format_info['ffname']
57             formats.append({
58                 'format_id': f_id,
59                 'url': format_info['videoInfoList'][0]['videoUrl'],
60                 'quality': quality(f_id),
61             })
62         self._sort_formats(formats)
63
64         return {
65             'id': video_id,
66             'title': self._og_search_title(webpage),
67             'formats': formats,
68             'description': descr,
69             'thumbnail': format_info['slate'],
70         }
71
72
73 class ImdbListIE(InfoExtractor):
74     IE_NAME = 'imdb:list'
75     IE_DESC = 'Internet Movie Database lists'
76     _VALID_URL = r'https?://www\.imdb\.com/list/(?P<id>[\da-zA-Z_-]{11})'
77     _TEST = {
78         'url': 'http://www.imdb.com/list/JFs9NWw6XI0',
79         'info_dict': {
80             'id': 'JFs9NWw6XI0',
81             'title': 'March 23, 2012 Releases',
82         },
83         'playlist_count': 7,
84     }
85
86     def _real_extract(self, url):
87         list_id = self._match_id(url)
88         webpage = self._download_webpage(url, list_id)
89         entries = [
90             self.url_result('http://www.imdb.com' + m, 'Imdb')
91             for m in re.findall(r'href="(/video/imdb/vi[^"]+)"\s+data-type="playlist"', webpage)]
92
93         list_title = self._html_search_regex(
94             r'<h1 class="header">(.*?)</h1>', webpage, 'list title')
95
96         return self.playlist_result(entries, list_id, list_title)