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.

128 lines
5.2 KiB

10 years ago
10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. update_url_query,
  8. )
  9. class NaverIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:m\.)?tv(?:cast)?\.naver\.com/v/(?P<id>\d+)'
  11. _TESTS = [{
  12. 'url': 'http://tv.naver.com/v/81652',
  13. 'info_dict': {
  14. 'id': '81652',
  15. 'ext': 'mp4',
  16. 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
  17. 'description': '합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
  18. 'upload_date': '20130903',
  19. },
  20. }, {
  21. 'url': 'http://tv.naver.com/v/395837',
  22. 'md5': '638ed4c12012c458fefcddfd01f173cd',
  23. 'info_dict': {
  24. 'id': '395837',
  25. 'ext': 'mp4',
  26. 'title': '9년이 지나도 아픈 기억, 전효성의 아버지',
  27. 'description': 'md5:5bf200dcbf4b66eb1b350d1eb9c753f7',
  28. 'upload_date': '20150519',
  29. },
  30. 'skip': 'Georestricted',
  31. }, {
  32. 'url': 'http://tvcast.naver.com/v/81652',
  33. 'only_matching': True,
  34. }]
  35. def _real_extract(self, url):
  36. video_id = self._match_id(url)
  37. webpage = self._download_webpage(url, video_id)
  38. vid = self._search_regex(
  39. r'videoId["\']\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
  40. 'video id', fatal=None, group='value')
  41. in_key = self._search_regex(
  42. r'inKey["\']\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
  43. 'key', default=None, group='value')
  44. if not vid or not in_key:
  45. error = self._html_search_regex(
  46. r'(?s)<div class="(?:nation_error|nation_box|error_box)">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
  47. webpage, 'error', default=None)
  48. if error:
  49. raise ExtractorError(error, expected=True)
  50. raise ExtractorError('couldn\'t extract vid and key')
  51. video_data = self._download_json(
  52. 'http://play.rmcnmv.naver.com/vod/play/v2.0/' + vid,
  53. video_id, query={
  54. 'key': in_key,
  55. })
  56. meta = video_data['meta']
  57. title = meta['subject']
  58. formats = []
  59. def extract_formats(streams, stream_type, query={}):
  60. for stream in streams:
  61. stream_url = stream.get('source')
  62. if not stream_url:
  63. continue
  64. stream_url = update_url_query(stream_url, query)
  65. encoding_option = stream.get('encodingOption', {})
  66. bitrate = stream.get('bitrate', {})
  67. formats.append({
  68. 'format_id': '%s_%s' % (stream.get('type') or stream_type, encoding_option.get('id') or encoding_option.get('name')),
  69. 'url': stream_url,
  70. 'width': int_or_none(encoding_option.get('width')),
  71. 'height': int_or_none(encoding_option.get('height')),
  72. 'vbr': int_or_none(bitrate.get('video')),
  73. 'abr': int_or_none(bitrate.get('audio')),
  74. 'filesize': int_or_none(stream.get('size')),
  75. 'protocol': 'm3u8_native' if stream_type == 'HLS' else None,
  76. })
  77. extract_formats(video_data.get('videos', {}).get('list', []), 'H264')
  78. for stream_set in video_data.get('streams', []):
  79. query = {}
  80. for param in stream_set.get('keys', []):
  81. query[param['name']] = param['value']
  82. stream_type = stream_set.get('type')
  83. videos = stream_set.get('videos')
  84. if videos:
  85. extract_formats(videos, stream_type, query)
  86. elif stream_type == 'HLS':
  87. stream_url = stream_set.get('source')
  88. if not stream_url:
  89. continue
  90. formats.extend(self._extract_m3u8_formats(
  91. update_url_query(stream_url, query), video_id,
  92. 'mp4', 'm3u8_native', m3u8_id=stream_type, fatal=False))
  93. self._sort_formats(formats)
  94. subtitles = {}
  95. for caption in video_data.get('captions', {}).get('list', []):
  96. caption_url = caption.get('source')
  97. if not caption_url:
  98. continue
  99. subtitles.setdefault(caption.get('language') or caption.get('locale'), []).append({
  100. 'url': caption_url,
  101. })
  102. upload_date = self._search_regex(
  103. r'<span[^>]+class="date".*?(\d{4}\.\d{2}\.\d{2})',
  104. webpage, 'upload date', fatal=False)
  105. if upload_date:
  106. upload_date = upload_date.replace('.', '')
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'formats': formats,
  111. 'subtitles': subtitles,
  112. 'description': self._og_search_description(webpage),
  113. 'thumbnail': meta.get('cover', {}).get('source') or self._og_search_thumbnail(webpage),
  114. 'view_count': int_or_none(meta.get('count')),
  115. 'upload_date': upload_date,
  116. }