naver.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_parse,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. clean_html,
  11. )
  12. class NaverIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:m\.)?tvcast\.naver\.com/v/(?P<id>\d+)'
  14. _TEST = {
  15. 'url': 'http://tvcast.naver.com/v/81652',
  16. 'info_dict': {
  17. 'id': '81652',
  18. 'ext': 'mp4',
  19. 'title': '[9월 모의고사 해설강의][수학_김상희] 수학 A형 16~20번',
  20. 'description': '합격불변의 법칙 메가스터디 | 메가스터디 수학 김상희 선생님이 9월 모의고사 수학A형 16번에서 20번까지 해설강의를 공개합니다.',
  21. 'upload_date': '20130903',
  22. },
  23. }
  24. def _real_extract(self, url):
  25. video_id = self._match_id(url)
  26. webpage = self._download_webpage(url, video_id)
  27. m_id = re.search(r'var rmcPlayer = new nhn.rmcnmv.RMCVideoPlayer\("(.+?)", "(.+?)"',
  28. webpage)
  29. if m_id is None:
  30. m_error = re.search(
  31. r'(?s)<div class="nation_error">\s*(?:<!--.*?-->)?\s*<p class="[^"]+">(?P<msg>.+?)</p>\s*</div>',
  32. webpage)
  33. if m_error:
  34. raise ExtractorError(clean_html(m_error.group('msg')), expected=True)
  35. raise ExtractorError('couldn\'t extract vid and key')
  36. vid = m_id.group(1)
  37. key = m_id.group(2)
  38. query = compat_urllib_parse.urlencode({'vid': vid, 'inKey': key, })
  39. query_urls = compat_urllib_parse.urlencode({
  40. 'masterVid': vid,
  41. 'protocol': 'p2p',
  42. 'inKey': key,
  43. })
  44. info = self._download_xml(
  45. 'http://serviceapi.rmcnmv.naver.com/flash/videoInfo.nhn?' + query,
  46. video_id, 'Downloading video info')
  47. urls = self._download_xml(
  48. 'http://serviceapi.rmcnmv.naver.com/flash/playableEncodingOption.nhn?' + query_urls,
  49. video_id, 'Downloading video formats info')
  50. formats = []
  51. for format_el in urls.findall('EncodingOptions/EncodingOption'):
  52. domain = format_el.find('Domain').text
  53. f = {
  54. 'url': domain + format_el.find('uri').text,
  55. 'ext': 'mp4',
  56. 'width': int(format_el.find('width').text),
  57. 'height': int(format_el.find('height').text),
  58. }
  59. if domain.startswith('rtmp'):
  60. f.update({
  61. 'ext': 'flv',
  62. 'rtmp_protocol': '1', # rtmpt
  63. })
  64. formats.append(f)
  65. self._sort_formats(formats)
  66. return {
  67. 'id': video_id,
  68. 'title': info.find('Subject').text,
  69. 'formats': formats,
  70. 'description': self._og_search_description(webpage),
  71. 'thumbnail': self._og_search_thumbnail(webpage),
  72. 'upload_date': info.find('WriteDate').text.replace('.', ''),
  73. 'view_count': int(info.find('PlayCount').text),
  74. }