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.

122 lines
4.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. compat_str,
  10. ExtractorError,
  11. unified_strdate,
  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. },
  28. 'params': {
  29. 'username': 'ydl.niconico@gmail.com',
  30. 'password': 'youtube-dl',
  31. },
  32. }
  33. _VALID_URL = r'^https?://(?:www\.|secure\.)?nicovideo\.jp/watch/([a-z][a-z][0-9]+)(?:.*)$'
  34. _NETRC_MACHINE = 'niconico'
  35. def _real_initialize(self):
  36. self._login()
  37. def _login(self):
  38. (username, password) = self._get_login_info()
  39. if username is None:
  40. # Login is required
  41. raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  42. # Log in
  43. login_form_strs = {
  44. 'mail': username,
  45. 'password': password,
  46. }
  47. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  48. # chokes on unicode
  49. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  50. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  51. request = compat_urllib_request.Request(
  52. 'https://secure.nicovideo.jp/secure/login', login_data)
  53. login_results = self._download_webpage(
  54. request, None, note='Logging in', errnote='Unable to log in')
  55. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  56. self._downloader.report_warning('unable to log in: bad username or password')
  57. return False
  58. return True
  59. def _real_extract(self, url):
  60. mobj = re.match(self._VALID_URL, url)
  61. video_id = mobj.group(1)
  62. # Get video webpage. We are not actually interested in it, but need
  63. # the cookies in order to be able to download the info webpage
  64. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  65. video_info = self._download_xml(
  66. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  67. note='Downloading video info page')
  68. # Get flv info
  69. flv_info_webpage = self._download_webpage(
  70. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  71. video_id, 'Downloading flv info')
  72. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  73. # Start extracting information
  74. video_title = video_info.find('.//title').text
  75. video_extension = video_info.find('.//movie_type').text
  76. video_format = video_extension.upper()
  77. video_thumbnail = video_info.find('.//thumbnail_url').text
  78. video_description = video_info.find('.//description').text
  79. video_uploader_id = video_info.find('.//user_id').text
  80. video_upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  81. video_view_count = video_info.find('.//view_counter').text
  82. video_webpage_url = video_info.find('.//watch_url').text
  83. # uploader
  84. video_uploader = video_uploader_id
  85. url = 'http://seiga.nicovideo.jp/api/user/info?id=' + video_uploader_id
  86. try:
  87. user_info = self._download_xml(
  88. url, video_id, note='Downloading user information')
  89. video_uploader = user_info.find('.//nickname').text
  90. except ExtractorError as err:
  91. self._downloader.report_warning('Unable to download user info webpage: %s' % compat_str(err))
  92. return {
  93. 'id': video_id,
  94. 'url': video_real_url,
  95. 'title': video_title,
  96. 'ext': video_extension,
  97. 'format': video_format,
  98. 'thumbnail': video_thumbnail,
  99. 'description': video_description,
  100. 'uploader': video_uploader,
  101. 'upload_date': video_upload_date,
  102. 'uploader_id': video_uploader_id,
  103. 'view_count': video_view_count,
  104. 'webpage_url': video_webpage_url,
  105. }