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.

81 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urllib_parse,
  7. ExtractorError,
  8. clean_html,
  9. )
  10. class NaverIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
  12. _TEST = {
  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. def _real_extract(self, url):
  23. mobj = re.match(self._VALID_URL, url)
  24. video_id = mobj.group(1)
  25. webpage = self._download_webpage(url, video_id)
  26. m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
  27. webpage)
  28. if m_id is None:
  29. m_error = re.search(
  30. r'(?s)<div class="nation_error">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
  31. webpage)
  32. if m_error:
  33. raise ExtractorError(clean_html(m_error.group('msg')), expected=True)
  34. raise ExtractorError('couldn\'t extract vid and key')
  35. vid = m_id.group(1)
  36. key = m_id.group(2)
  37. query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key,})
  38. query_urls = compat_urllib_parse.urlencode({
  39. 'masterVid': vid,
  40. 'protocol': 'p2p',
  41. 'inKey': key,
  42. })
  43. info = self._download_xml(
  44. 'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
  45. video_id, 'Downloading video info')
  46. urls = self._download_xml(
  47. 'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
  48. video_id, 'Downloading video formats info')
  49. formats = []
  50. for format_el in urls.findall('EncodingOptions/EncodingOption'):
  51. domain = format_el.find('Domain').text
  52. f = {
  53. 'url': domain + format_el.find('uri').text,
  54. 'ext': 'mp4',
  55. 'width': int(format_el.find('width').text),
  56. 'height': int(format_el.find('height').text),
  57. }
  58. if domain.startswith('rtmp'):
  59. f.update({
  60. 'ext': 'flv',
  61. 'rtmp_protocol': '1', # rtmpt
  62. })
  63. formats.append(f)
  64. self._sort_formats(formats)
  65. return {
  66. 'id': video_id,
  67. 'title': info.find('Subject').text,
  68. 'formats': formats,
  69. 'description': self._og_search_description(webpage),
  70. 'thumbnail': self._og_search_thumbnail(webpage),
  71. 'upload_date': info.find('WriteDate').text.replace('.', ''),
  72. 'view_count': int(info.find('PlayCount').text),
  73. }