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.

92 lines
3.8 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. self._downloader.params.get('allsubtitles', False)])
  12. def _list_available_subtitles(self, video_id, webpage=None):
  13. """ outputs the available subtitles for the video """
  14. sub_lang_list = self._get_available_subtitles(video_id)
  15. auto_captions_list = self._get_available_automatic_caption(video_id, webpage)
  16. sub_lang = ",".join(list(sub_lang_list.keys()))
  17. self.to_screen(u'%s: Available subtitles for video: %s' %
  18. (video_id, sub_lang))
  19. auto_lang = ",".join(auto_captions_list.keys())
  20. self.to_screen(u'%s: Available automatic captions for video: %s' %
  21. (video_id, auto_lang))
  22. def extract_subtitles(self, video_id, video_webpage=None):
  23. """
  24. returns {sub_lang: sub} ,{} if subtitles not found or None if the
  25. subtitles aren't requested.
  26. """
  27. if not self._have_to_download_any_subtitles:
  28. return None
  29. available_subs_list = {}
  30. if self._downloader.params.get('writeautomaticsub', False):
  31. available_subs_list.update(self._get_available_automatic_caption(video_id, video_webpage))
  32. if self._downloader.params.get('writesubtitles', False) or self._downloader.params.get('allsubtitles', False):
  33. available_subs_list.update(self._get_available_subtitles(video_id))
  34. if not available_subs_list: # error, it didn't get the available subtitles
  35. return {}
  36. if self._downloader.params.get('allsubtitles', False):
  37. sub_lang_list = available_subs_list
  38. else:
  39. if self._downloader.params.get('subtitleslangs', False):
  40. requested_langs = self._downloader.params.get('subtitleslangs')
  41. elif 'en' in available_subs_list:
  42. requested_langs = ['en']
  43. else:
  44. requested_langs = [list(available_subs_list.keys())[0]]
  45. sub_lang_list = {}
  46. for sub_lang in requested_langs:
  47. if not sub_lang in available_subs_list:
  48. self._downloader.report_warning(u'no closed captions found in the specified language "%s"' % sub_lang)
  49. continue
  50. sub_lang_list[sub_lang] = available_subs_list[sub_lang]
  51. subtitles = {}
  52. for sub_lang, url in sub_lang_list.items():
  53. subtitle = self._request_subtitle_url(sub_lang, url)
  54. if subtitle:
  55. subtitles[sub_lang] = subtitle
  56. return subtitles
  57. def _request_subtitle_url(self, sub_lang, url):
  58. """ makes the http request for the subtitle """
  59. try:
  60. sub = self._download_webpage(url, None, note=False)
  61. except ExtractorError as err:
  62. self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
  63. return
  64. if not sub:
  65. self._downloader.report_warning(u'Did not fetch video subtitles')
  66. return
  67. return sub
  68. def _get_available_subtitles(self, video_id):
  69. """
  70. returns {sub_lang: url} or {} if not available
  71. Must be redefined by the subclasses
  72. """
  73. pass
  74. def _get_available_automatic_caption(self, video_id, webpage):
  75. """
  76. returns {sub_lang: url} or {} if not available
  77. Must be redefined by the subclasses that support automatic captions,
  78. otherwise it will return {}
  79. """
  80. self._downloader.report_warning(u'Automatic Captions not supported by this server')
  81. return {}