niconico.py 5.6 KB

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