rottentomatoes.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. clean_html,
  5. float_or_none,
  6. get_element_by_class,
  7. join_nonempty,
  8. traverse_obj,
  9. url_or_none,
  10. )
  11. class RottenTomatoesIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?rottentomatoes\.com/m/(?P<playlist>[^/]+)(?:/(?P<tr>trailers)(?:/(?P<id>\w+))?)?'
  13. _TESTS = [{
  14. 'url': 'http://www.rottentomatoes.com/m/toy_story_3/trailers/11028566/',
  15. 'info_dict': {
  16. 'id': '11028566',
  17. 'ext': 'mp4',
  18. 'title': 'Toy Story 3',
  19. 'description': 'From the creators of the beloved TOY STORY films, comes a story that will reunite the gang in a whole new way.',
  20. },
  21. 'skip': 'No longer available',
  22. }, {
  23. 'url': 'https://www.rottentomatoes.com/m/toy_story_3/trailers/VycaVoBKhGuk',
  24. 'info_dict': {
  25. 'id': 'VycaVoBKhGuk',
  26. 'ext': 'mp4',
  27. 'title': 'Toy Story 3: Trailer 2',
  28. 'description': '',
  29. 'thumbnail': r're:^https?://.*\.jpg$',
  30. 'duration': 149.941,
  31. },
  32. }, {
  33. 'url': 'http://www.rottentomatoes.com/m/toy_story_3',
  34. 'info_dict': {
  35. 'id': 'toy_story_3',
  36. 'title': 'Toy Story 3',
  37. },
  38. 'playlist_mincount': 4,
  39. }, {
  40. 'url': 'http://www.rottentomatoes.com/m/toy_story_3/trailers',
  41. 'info_dict': {
  42. 'id': 'toy_story_3-trailers',
  43. },
  44. 'playlist_mincount': 5,
  45. }]
  46. def _extract_videos(self, data, display_id):
  47. for video in traverse_obj(data, (lambda _, v: v['publicId'] and v['file'] and v['type'] == 'hls')):
  48. yield {
  49. 'formats': self._extract_m3u8_formats(
  50. video['file'], display_id, 'mp4', m3u8_id='hls', fatal=False),
  51. **traverse_obj(video, {
  52. 'id': 'publicId',
  53. 'title': 'title',
  54. 'description': 'description',
  55. 'duration': ('durationInSeconds', {float_or_none}),
  56. 'thumbnail': ('image', {url_or_none}),
  57. }),
  58. }
  59. def _real_extract(self, url):
  60. playlist_id, trailers, video_id = self._match_valid_url(url).group('playlist', 'tr', 'id')
  61. playlist_id = join_nonempty(playlist_id, trailers)
  62. webpage = self._download_webpage(url, playlist_id)
  63. data = self._search_json(
  64. r'<script[^>]+\bid=["\'](?:heroV|v)ideos["\'][^>]*>', webpage,
  65. 'data', playlist_id, contains_pattern=r'\[{(?s:.+)}\]')
  66. if video_id:
  67. video_data = traverse_obj(data, lambda _, v: v['publicId'] == video_id)
  68. if not video_data:
  69. raise ExtractorError('Unable to extract video from webpage')
  70. return next(self._extract_videos(video_data, video_id))
  71. return self.playlist_result(
  72. self._extract_videos(data, playlist_id), playlist_id,
  73. clean_html(get_element_by_class('scoreboard__title', webpage)))