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.

80 lines
3.2 KiB

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