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.

67 lines
2.5 KiB

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