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.

148 lines
5.5 KiB

  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. compat_urllib_request,
  8. compat_urlparse,
  9. unified_strdate,
  10. parse_duration,
  11. int_or_none,
  12. )
  13. class NiconicoIE(InfoExtractor):
  14. IE_NAME = 'niconico'
  15. IE_DESC = 'ニコニコ動画'
  16. _TEST = {
  17. 'url': 'http://www.nicovideo.jp/watch/sm22312215',
  18. 'md5': 'd1a75c0823e2f629128c43e1212760f9',
  19. 'info_dict': {
  20. 'id': 'sm22312215',
  21. 'ext': 'mp4',
  22. 'title': 'Big Buck Bunny',
  23. 'uploader': 'takuya0301',
  24. 'uploader_id': '2698420',
  25. 'upload_date': '20131123',
  26. 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
  27. 'duration': 33,
  28. },
  29. 'params': {
  30. 'username': 'ydl.niconico@gmail.com',
  31. 'password': 'youtube-dl',
  32. },
  33. }
  34. _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/((?:[a-z]{2})?[0-9]+)'
  35. _NETRC_MACHINE = 'niconico'
  36. # Determine whether the downloader used authentication to download video
  37. _AUTHENTICATED = False
  38. def _real_initialize(self):
  39. self._login()
  40. def _login(self):
  41. (username, password) = self._get_login_info()
  42. # No authentication to be performed
  43. if not username:
  44. return True
  45. # Log in
  46. login_form_strs = {
  47. 'mail': username,
  48. 'password': password,
  49. }
  50. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  51. # chokes on unicode
  52. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  53. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  54. request = compat_urllib_request.Request(
  55. 'https://secure.nicovideo.jp/secure/login', login_data)
  56. login_results = self._download_webpage(
  57. request, None, note='Logging in', errnote='Unable to log in')
  58. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  59. self._downloader.report_warning('unable to log in: bad username or password')
  60. return False
  61. # Successful login
  62. self._AUTHENTICATED = True
  63. return True
  64. def _real_extract(self, url):
  65. mobj = re.match(self._VALID_URL, url)
  66. video_id = mobj.group(1)
  67. # Get video webpage. We are not actually interested in it, but need
  68. # the cookies in order to be able to download the info webpage
  69. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  70. video_info = self._download_xml(
  71. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  72. note='Downloading video info page')
  73. if self._AUTHENTICATED:
  74. # Get flv info
  75. flv_info_webpage = self._download_webpage(
  76. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  77. video_id, 'Downloading flv info')
  78. else:
  79. # Get external player info
  80. ext_player_info = self._download_webpage(
  81. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  82. thumb_play_key = self._search_regex(
  83. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  84. # Get flv info
  85. flv_info_data = compat_urllib_parse.urlencode({
  86. 'k': thumb_play_key,
  87. 'v': video_id
  88. })
  89. flv_info_request = compat_urllib_request.Request(
  90. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  91. {'Content-Type': 'application/x-www-form-urlencoded'})
  92. flv_info_webpage = self._download_webpage(
  93. flv_info_request, video_id,
  94. note='Downloading flv info', errnote='Unable to download flv info')
  95. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  96. # Start extracting information
  97. title = video_info.find('.//title').text
  98. extension = video_info.find('.//movie_type').text
  99. video_format = extension.upper()
  100. thumbnail = video_info.find('.//thumbnail_url').text
  101. description = video_info.find('.//description').text
  102. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  103. view_count = int_or_none(video_info.find('.//view_counter').text)
  104. comment_count = int_or_none(video_info.find('.//comment_num').text)
  105. duration = parse_duration(video_info.find('.//length').text)
  106. webpage_url = video_info.find('.//watch_url').text
  107. if video_info.find('.//ch_id') is not None:
  108. uploader_id = video_info.find('.//ch_id').text
  109. uploader = video_info.find('.//ch_name').text
  110. elif video_info.find('.//user_id') is not None:
  111. uploader_id = video_info.find('.//user_id').text
  112. uploader = video_info.find('.//user_nickname').text
  113. else:
  114. uploader_id = uploader = None
  115. return {
  116. 'id': video_id,
  117. 'url': video_real_url,
  118. 'title': title,
  119. 'ext': extension,
  120. 'format': video_format,
  121. 'thumbnail': thumbnail,
  122. 'description': description,
  123. 'uploader': uploader,
  124. 'upload_date': upload_date,
  125. 'uploader_id': uploader_id,
  126. 'view_count': view_count,
  127. 'comment_count': comment_count,
  128. 'duration': duration,
  129. 'webpage_url': webpage_url,
  130. }