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.

122 lines
4.9 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\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
  12. _TESTS = [{
  13. 'url': 'http://tvcast.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://tvcast.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. def _real_extract(self, url):
  34. video_id = self._match_id(url)
  35. webpage = self._download_webpage(url, video_id)
  36. m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
  37. webpage)
  38. if m_id is None:
  39. error = self._html_search_regex(
  40. r'(?s)<div class="(?:nation_error|nation_box|error_box)">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
  41. webpage, 'error', default=None)
  42. if error:
  43. raise ExtractorError(error, expected=True)
  44. raise ExtractorError('couldn\'t extract vid and key')
  45. video_data = self._download_json(
  46. 'http://play.rmcnmv.naver.com/vod/play/v2.0/' + m_id.group(1),
  47. video_id, query={
  48. 'key': m_id.group(2),
  49. })
  50. meta = video_data['meta']
  51. title = meta['subject']
  52. formats = []
  53. def extract_formats(streams, stream_type, query={}):
  54. for stream in streams:
  55. stream_url = stream.get('source')
  56. if not stream_url:
  57. continue
  58. stream_url = update_url_query(stream_url, query)
  59. encoding_option = stream.get('encodingOption', {})
  60. bitrate = stream.get('bitrate', {})
  61. formats.append({
  62. 'format_id': '%s_%s' % (stream.get('type') or stream_type, encoding_option.get('id') or encoding_option.get('name')),
  63. 'url': stream_url,
  64. 'width': int_or_none(encoding_option.get('width')),
  65. 'height': int_or_none(encoding_option.get('height')),
  66. 'vbr': int_or_none(bitrate.get('video')),
  67. 'abr': int_or_none(bitrate.get('audio')),
  68. 'filesize': int_or_none(stream.get('size')),
  69. 'protocol': 'm3u8_native' if stream_type == 'HLS' else None,
  70. })
  71. extract_formats(video_data.get('videos', {}).get('list', []), 'H264')
  72. for stream_set in video_data.get('streams', []):
  73. query = {}
  74. for param in stream_set.get('keys', []):
  75. query[param['name']] = param['value']
  76. stream_type = stream_set.get('type')
  77. videos = stream_set.get('videos')
  78. if videos:
  79. extract_formats(videos, stream_type, query)
  80. elif stream_type == 'HLS':
  81. stream_url = stream_set.get('source')
  82. if not stream_url:
  83. continue
  84. formats.extend(self._extract_m3u8_formats(
  85. update_url_query(stream_url, query), video_id,
  86. 'mp4', 'm3u8_native', m3u8_id=stream_type, fatal=False))
  87. self._sort_formats(formats)
  88. subtitles = {}
  89. for caption in video_data.get('captions', {}).get('list', []):
  90. caption_url = caption.get('source')
  91. if not caption_url:
  92. continue
  93. subtitles.setdefault(caption.get('language') or caption.get('locale'), []).append({
  94. 'url': caption_url,
  95. })
  96. upload_date = self._search_regex(
  97. r'<span[^>]+class="date".*?(\d{4}\.\d{2}\.\d{2})',
  98. webpage, 'upload date', fatal=False)
  99. if upload_date:
  100. upload_date = upload_date.replace('.', '')
  101. return {
  102. 'id': video_id,
  103. 'title': title,
  104. 'formats': formats,
  105. 'subtitles': subtitles,
  106. 'description': self._og_search_description(webpage),
  107. 'thumbnail': meta.get('cover', {}).get('source') or self._og_search_thumbnail(webpage),
  108. 'view_count': int_or_none(meta.get('count')),
  109. 'upload_date': upload_date,
  110. }