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