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.

147 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 uses authentication to download video
  37. _AUTHENTICATE = False
  38. def _real_initialize(self):
  39. if self._downloader.params.get('username', None) is not None:
  40. self._AUTHENTICATE = True
  41. if self._AUTHENTICATE:
  42. self._login()
  43. def _login(self):
  44. (username, password) = self._get_login_info()
  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. return True
  62. def _real_extract(self, url):
  63. mobj = re.match(self._VALID_URL, url)
  64. video_id = mobj.group(1)
  65. # Get video webpage. We are not actually interested in it, but need
  66. # the cookies in order to be able to download the info webpage
  67. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  68. video_info = self._download_xml(
  69. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  70. note='Downloading video info page')
  71. if self._AUTHENTICATE:
  72. # Get flv info
  73. flv_info_webpage = self._download_webpage(
  74. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  75. video_id, 'Downloading flv info')
  76. else:
  77. # Get external player info
  78. ext_player_info = self._download_webpage(
  79. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  80. thumb_play_key = self._search_regex(
  81. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  82. # Get flv info
  83. flv_info_data = compat_urllib_parse.urlencode({
  84. 'k': thumb_play_key,
  85. 'v': video_id
  86. })
  87. flv_info_request = compat_urllib_request.Request(
  88. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  89. {'Content-Type': 'application/x-www-form-urlencoded'})
  90. flv_info_webpage = self._download_webpage(
  91. flv_info_request, video_id,
  92. note='Downloading flv info', errnote='Unable to download flv info')
  93. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  94. # Start extracting information
  95. title = video_info.find('.//title').text
  96. extension = video_info.find('.//movie_type').text
  97. video_format = extension.upper()
  98. thumbnail = video_info.find('.//thumbnail_url').text
  99. description = video_info.find('.//description').text
  100. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  101. view_count = int_or_none(video_info.find('.//view_counter').text)
  102. comment_count = int_or_none(video_info.find('.//comment_num').text)
  103. duration = parse_duration(video_info.find('.//length').text)
  104. webpage_url = video_info.find('.//watch_url').text
  105. if video_info.find('.//ch_id') is not None:
  106. uploader_id = video_info.find('.//ch_id').text
  107. uploader = video_info.find('.//ch_name').text
  108. elif video_info.find('.//user_id') is not None:
  109. uploader_id = video_info.find('.//user_id').text
  110. uploader = video_info.find('.//user_nickname').text
  111. else:
  112. uploader_id = uploader = None
  113. return {
  114. 'id': video_id,
  115. 'url': video_real_url,
  116. 'title': title,
  117. 'ext': extension,
  118. 'format': video_format,
  119. 'thumbnail': thumbnail,
  120. 'description': description,
  121. 'uploader': uploader,
  122. 'upload_date': upload_date,
  123. 'uploader_id': uploader_id,
  124. 'view_count': view_count,
  125. 'comment_count': comment_count,
  126. 'duration': duration,
  127. 'webpage_url': webpage_url,
  128. }