imdb.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import base64
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. mimetype2ext,
  9. qualities,
  10. traverse_obj,
  11. try_get,
  12. url_or_none,
  13. )
  14. class ImdbIE(InfoExtractor):
  15. IE_NAME = 'imdb'
  16. IE_DESC = 'Internet Movie Database trailers'
  17. _VALID_URL = r'https?://(?:www|m)\.imdb\.com/(?:video|title|list).*?[/-]vi(?P<id>\d+)'
  18. _TESTS = [{
  19. 'url': 'http://www.imdb.com/video/imdb/vi2524815897',
  20. 'info_dict': {
  21. 'id': '2524815897',
  22. 'ext': 'mp4',
  23. 'title': 'No. 2',
  24. 'description': 'md5:87bd0bdc61e351f21f20d2d7441cb4e7',
  25. 'duration': 152,
  26. 'thumbnail': r're:^https?://.+\.jpg',
  27. },
  28. }, {
  29. 'url': 'https://www.imdb.com/video/vi3516832537',
  30. 'info_dict': {
  31. 'id': '3516832537',
  32. 'ext': 'mp4',
  33. 'title': 'Paul: U.S. Trailer #1',
  34. 'description': 'md5:17fcc4fe11ec29b4399be9d4c5ef126c',
  35. 'duration': 153,
  36. 'thumbnail': r're:^https?://.+\.jpg',
  37. },
  38. }, {
  39. 'url': 'http://www.imdb.com/video/_/vi2524815897',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'http://www.imdb.com/title/tt1667889/?ref_=ext_shr_eml_vi#lb-vi2524815897',
  43. 'only_matching': True,
  44. }, {
  45. 'url': 'http://www.imdb.com/title/tt1667889/#lb-vi2524815897',
  46. 'only_matching': True,
  47. }, {
  48. 'url': 'http://www.imdb.com/videoplayer/vi1562949145',
  49. 'only_matching': True,
  50. }, {
  51. 'url': 'http://www.imdb.com/title/tt4218696/videoplayer/vi2608641561',
  52. 'only_matching': True,
  53. }, {
  54. 'url': 'https://www.imdb.com/list/ls009921623/videoplayer/vi260482329',
  55. 'only_matching': True,
  56. }]
  57. def _real_extract(self, url):
  58. video_id = self._match_id(url)
  59. webpage = self._download_webpage(f'https://www.imdb.com/video/vi{video_id}', video_id)
  60. info = self._search_nextjs_data(webpage, video_id)
  61. video_info = traverse_obj(info, ('props', 'pageProps', 'videoPlaybackData', 'video'), default={})
  62. title = (traverse_obj(video_info, ('name', 'value'), ('primaryTitle', 'titleText', 'text'))
  63. or self._html_search_meta(('og:title', 'twitter:title'), webpage, default=None)
  64. or self._html_extract_title(webpage))
  65. data = video_info.get('playbackURLs') or try_get(self._download_json(
  66. 'https://www.imdb.com/ve/data/VIDEO_PLAYBACK_DATA', video_id,
  67. query={
  68. 'key': base64.b64encode(json.dumps({
  69. 'type': 'VIDEO_PLAYER',
  70. 'subType': 'FORCE_LEGACY',
  71. 'id': f'vi{video_id}',
  72. }).encode()).decode(),
  73. }), lambda x: x[0]['videoLegacyEncodings'])
  74. quality = qualities(('SD', '480p', '720p', '1080p'))
  75. formats, subtitles = [], {}
  76. for encoding in data:
  77. if not encoding or not isinstance(encoding, dict):
  78. continue
  79. video_url = url_or_none(encoding.get('url'))
  80. if not video_url:
  81. continue
  82. ext = mimetype2ext(encoding.get(
  83. 'mimeType')) or determine_ext(video_url)
  84. if ext == 'm3u8':
  85. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  86. video_url, video_id, 'mp4', entry_protocol='m3u8_native',
  87. preference=1, m3u8_id='hls', fatal=False)
  88. subtitles = self._merge_subtitles(subtitles, subs)
  89. formats.extend(fmts)
  90. continue
  91. format_id = traverse_obj(encoding, ('displayName', 'value'), 'definition')
  92. formats.append({
  93. 'format_id': format_id,
  94. 'url': video_url,
  95. 'ext': ext,
  96. 'quality': quality(format_id),
  97. })
  98. return {
  99. 'id': video_id,
  100. 'title': title,
  101. 'alt_title': info.get('videoSubTitle'),
  102. 'formats': formats,
  103. 'description': try_get(video_info, lambda x: x['description']['value']),
  104. 'thumbnail': url_or_none(try_get(video_info, lambda x: x['thumbnail']['url'])),
  105. 'duration': int_or_none(try_get(video_info, lambda x: x['runtime']['value'])),
  106. 'subtitles': subtitles,
  107. }
  108. class ImdbListIE(InfoExtractor):
  109. IE_NAME = 'imdb:list'
  110. IE_DESC = 'Internet Movie Database lists'
  111. _VALID_URL = r'https?://(?:www\.)?imdb\.com/list/ls(?P<id>\d{9})(?!/videoplayer/vi\d+)'
  112. _TEST = {
  113. 'url': 'https://www.imdb.com/list/ls009921623/',
  114. 'info_dict': {
  115. 'id': '009921623',
  116. 'title': 'The Bourne Legacy',
  117. 'description': 'A list of trailers, clips, and more from The Bourne Legacy, starring Jeremy Renner and Rachel Weisz.',
  118. },
  119. 'playlist_count': 8,
  120. }
  121. def _real_extract(self, url):
  122. list_id = self._match_id(url)
  123. webpage = self._download_webpage(url, list_id)
  124. entries = [
  125. self.url_result('http://www.imdb.com' + m, 'Imdb')
  126. for m in re.findall(rf'href="(/list/ls{list_id}/videoplayer/vi[^"]+)"', webpage)]
  127. list_title = self._html_search_regex(
  128. r'<h1[^>]+class="[^"]*header[^"]*"[^>]*>(.*?)</h1>',
  129. webpage, 'list title')
  130. list_description = self._html_search_regex(
  131. r'<div[^>]+class="[^"]*list-description[^"]*"[^>]*><p>(.*?)</p>',
  132. webpage, 'list description')
  133. return self.playlist_result(entries, list_id, list_title, list_description)