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.

73 lines
2.8 KiB

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