]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/imdb.py
Imported Upstream version 2014.01.17.2
[youtubedl] / 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 compat_urlparse,
9 get_element_by_attribute,
10 )
11
12
13 class ImdbIE(InfoExtractor):
14 IE_NAME = 'imdb'
15 IE_DESC = 'Internet Movie Database trailers'
16 _VALID_URL = r'http://(?:www|m)\.imdb\.com/video/imdb/vi(?P<id>\d+)'
17
18 _TEST = {
19 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
20 'md5': '9f34fa777ade3a6e57a054fdbcb3a068',
21 'info_dict': {
22 'id': '2524815897',
23 'ext': 'mp4',
24 'title': 'Ice Age: Continental Drift Trailer (No. 2) - IMDb',
25 'description': 'md5:9061c2219254e5d14e03c25c98e96a81',
26 }
27 }
28
29 def _real_extract(self, url):
30 mobj = re.match(self._VALID_URL, url)
31 video_id = mobj.group('id')
32 webpage = self._download_webpage('http://www.imdb.com/video/imdb/vi%s' % video_id, video_id)
33 descr = get_element_by_attribute('itemprop', 'description', webpage)
34 available_formats = re.findall(
35 r'case \'(?P<f_id>.*?)\' :$\s+url = \'(?P<path>.*?)\'', webpage,
36 flags=re.MULTILINE)
37 formats = []
38 for f_id, f_path in available_formats:
39 f_path = f_path.strip()
40 format_page = self._download_webpage(
41 compat_urlparse.urljoin(url, f_path),
42 'Downloading info for %s format' % f_id)
43 json_data = self._search_regex(
44 r'<script[^>]+class="imdb-player-data"[^>]*?>(.*?)</script>',
45 format_page, 'json data', flags=re.DOTALL)
46 info = json.loads(json_data)
47 format_info = info['videoPlayerObject']['video']
48 formats.append({
49 'format_id': f_id,
50 'url': format_info['url'],
51 })
52
53 return {
54 'id': video_id,
55 'title': self._og_search_title(webpage),
56 'formats': formats,
57 'description': descr,
58 'thumbnail': format_info['slate'],
59 }
60
61
62 class ImdbListIE(InfoExtractor):
63 IE_NAME = 'imdb:list'
64 IE_DESC = 'Internet Movie Database lists'
65 _VALID_URL = r'http://www\.imdb\.com/list/(?P<id>[\da-zA-Z_-]{11})'
66
67 def _real_extract(self, url):
68 mobj = re.match(self._VALID_URL, url)
69 list_id = mobj.group('id')
70
71 # RSS XML is sometimes malformed
72 rss = self._download_webpage('http://rss.imdb.com/list/%s' % list_id, list_id, 'Downloading list RSS')
73 list_title = self._html_search_regex(r'<title>(.*?)</title>', rss, 'list title')
74
75 # Export is independent of actual author_id, but returns 404 if no author_id is provided.
76 # However, passing dummy author_id seems to be enough.
77 csv = self._download_webpage('http://www.imdb.com/list/export?list_id=%s&author_id=ur00000000' % list_id,
78 list_id, 'Downloading list CSV')
79
80 entries = []
81 for item in csv.split('\n')[1:]:
82 cols = item.split(',')
83 if len(cols) < 2:
84 continue
85 item_id = cols[1][1:-1]
86 if item_id.startswith('vi'):
87 entries.append(self.url_result('http://www.imdb.com/video/imdb/%s' % item_id, 'Imdb'))
88
89 return self.playlist_result(entries, list_id, list_title)