subtitles.py 3.4 KB

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