You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
4.0 KiB

  1. from .common import InfoExtractor
  2. from ..utils import (
  3. compat_str,
  4. ExtractorError,
  5. )
  6. class SubtitlesInfoExtractor(InfoExtractor):
  7. @property
  8. def _have_to_download_any_subtitles(self):
  9. return any([self._downloader.params.get('writesubtitles', False),
  10. self._downloader.params.get('writeautomaticsub')])
  11. def _list_available_subtitles(self, video_id, webpage):
  12. """ outputs the available subtitles for the video """
  13. sub_lang_list = self._get_available_subtitles(video_id, webpage)
  14. auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
  15. sub_lang = ",".join(list(sub_lang_list.keys()))
  16. self.to_screen(u'%s: Available subtitles for video: %s' %
  17. (video_id, sub_lang))
  18. auto_lang = ",".join(auto_captions_list.keys())
  19. self.to_screen(u'%s: Available automatic captions for video: %s' %
  20. (video_id, auto_lang))
  21. def extract_subtitles(self, video_id, webpage):
  22. """
  23. returns {sub_lang: sub} ,{} if subtitles not found or None if the
  24. subtitles aren't requested.
  25. """
  26. if not self._have_to_download_any_subtitles:
  27. return None
  28. available_subs_list = {}
  29. if self._downloader.params.get('writeautomaticsub', False):
  30. available_subs_list.update(self._get_available_automatic_caption(video_id, webpage))
  31. if self._downloader.params.get('writesubtitles', False):
  32. available_subs_list.update(self._get_available_subtitles(video_id, webpage))
  33. if not available_subs_list: # error, it didn't get the available subtitles
  34. return {}
  35. if self._downloader.params.get('allsubtitles', False):
  36. sub_lang_list = available_subs_list
  37. else:
  38. if self._downloader.params.get('subtitleslangs', False):
  39. requested_langs = self._downloader.params.get('subtitleslangs')
  40. elif 'en' in available_subs_list:
  41. requested_langs = ['en']
  42. else:
  43. requested_langs = [list(available_subs_list.keys())[0]]
  44. sub_lang_list = {}
  45. for sub_lang in requested_langs:
  46. if not sub_lang in available_subs_list:
  47. self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
  48. continue
  49. sub_lang_list[sub_lang] = available_subs_list[sub_lang]
  50. subtitles = {}
  51. for sub_lang, url in sub_lang_list.items():
  52. subtitle = self._request_subtitle_url(sub_lang, url)
  53. if subtitle:
  54. subtitles[sub_lang] = subtitle
  55. return subtitles
  56. def _download_subtitle_url(self, sub_lang, url):
  57. return self._download_webpage(url, None, note=False)
  58. def _request_subtitle_url(self, sub_lang, url):
  59. """ makes the http request for the subtitle """
  60. try:
  61. sub = self._download_subtitle_url(sub_lang, url)
  62. except ExtractorError as err:
  63. self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
  64. return
  65. if not sub:
  66. self._downloader.report_warning(u'Did not fetch video subtitles')
  67. return
  68. return sub
  69. def _get_available_subtitles(self, video_id, webpage):
  70. """
  71. returns {sub_lang: url} or {} if not available
  72. Must be redefined by the subclasses
  73. """
  74. # By default, allow implementations to simply pass in the result
  75. assert isinstance(webpage, dict), \
  76. '_get_available_subtitles not implemented'
  77. return webpage
  78. def _get_available_automatic_caption(self, video_id, webpage):
  79. """
  80. returns {sub_lang: url} or {} if not available
  81. Must be redefined by the subclasses that support automatic captions,
  82. otherwise it will return {}
  83. """
  84. self._downloader.report_warning(u'Automatic Captions not supported by this server')
  85. return {}