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.

125 lines
5.0 KiB

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