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.

91 lines
3.7 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=None):
  12. """ outputs the available subtitles for the video """
  13. sub_lang_list = self._get_available_subtitles(video_id)
  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, video_webpage=None):
  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, video_webpage))
  31. if self._downloader.params.get('writesubtitles', False):
  32. available_subs_list.update(self._get_available_subtitles(video_id))
  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 _request_subtitle_url(self, sub_lang, url):
  57. """ makes the http request for the subtitle """
  58. try:
  59. sub = self._download_webpage(url, None, note=False)
  60. except ExtractorError as err:
  61. self._downloader.report_warning(u'unable to download video subtitles for %s: %s' % (sub_lang, compat_str(err)))
  62. return
  63. if not sub:
  64. self._downloader.report_warning(u'Did not fetch video subtitles')
  65. return
  66. return sub
  67. def _get_available_subtitles(self, video_id):
  68. """
  69. returns {sub_lang: url} or {} if not available
  70. Must be redefined by the subclasses
  71. """
  72. pass
  73. def _get_available_automatic_caption(self, video_id, webpage):
  74. """
  75. returns {sub_lang: url} or {} if not available
  76. Must be redefined by the subclasses that support automatic captions,
  77. otherwise it will return {}
  78. """
  79. self._downloader.report_warning(u'Automatic Captions not supported by this server')
  80. return {}