imdb.py 4.3 KB

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