naver.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. f = {
  45. 'url': domain + format_el.find('uri').text,
  46. 'ext': 'mp4',
  47. 'width': int(format_el.find('width').text),
  48. 'height': int(format_el.find('height').text),
  49. }
  50. if domain.startswith('rtmp'):
  51. f.update({
  52. 'ext': 'flv',
  53. 'rtmp_protocol': '1', # rtmpt
  54. })
  55. formats.append(f)
  56. self._sort_formats(formats)
  57. return {
  58. 'id': video_id,
  59. 'title': info.find('Subject').text,
  60. 'formats': formats,
  61. 'description': self._og_search_description(webpage),
  62. 'thumbnail': self._og_search_thumbnail(webpage),
  63. 'upload_date': info.find('WriteDate').text.replace('.', ''),
  64. 'view_count': int(info.find('PlayCount').text),
  65. }