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.

127 lines
4.8 KiB

  1. # encoding: utf-8
  2. import re
  3. import socket
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_http_client,
  7. compat_urllib_error,
  8. compat_urllib_parse,
  9. compat_urllib_request,
  10. compat_urlparse,
  11. compat_str,
  12. ExtractorError,
  13. unified_strdate,
  14. )
  15. class NiconicoIE(InfoExtractor):
  16. IE_NAME = u'niconico'
  17. IE_DESC = u'ニコニコ動画'
  18. _TEST = {
  19. u'url': u'http://www.nicovideo.jp/watch/sm22312215',
  20. u'file': u'sm22312215.mp4',
  21. u'md5': u'd1a75c0823e2f629128c43e1212760f9',
  22. u'info_dict': {
  23. u'title': u'Big Buck Bunny',
  24. u'uploader': u'takuya0301',
  25. u'uploader_id': u'2698420',
  26. u'upload_date': u'20131123',
  27. u'description': u'(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
  28. },
  29. u'params': {
  30. u'username': u'ydl.niconico@gmail.com',
  31. u'password': u'youtube-dl',
  32. },
  33. }
  34. _VALID_URL = r'^https?://(?:www\.|secure\.)?nicovideo\.jp/watch/([a-z][a-z][0-9]+)(?:.*)$'
  35. _NETRC_MACHINE = 'niconico'
  36. # If True it will raise an error if no login info is provided
  37. _LOGIN_REQUIRED = True
  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 username is None:
  44. if self._LOGIN_REQUIRED:
  45. raise ExtractorError(u'No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  46. return False
  47. # Log in
  48. login_form_strs = {
  49. u'mail': username,
  50. u'password': password,
  51. }
  52. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  53. # chokes on unicode
  54. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k,v in login_form_strs.items())
  55. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  56. request = compat_urllib_request.Request(
  57. u'https://secure.nicovideo.jp/secure/login', login_data)
  58. login_results = self._download_webpage(
  59. request, u'', note=u'Logging in', errnote=u'Unable to log in')
  60. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  61. self._downloader.report_warning(u'unable to log in: bad username or password')
  62. return False
  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=u'Downloading video info page')
  73. # Get flv info
  74. flv_info_webpage = self._download_webpage(
  75. u'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  76. video_id, u'Downloading flv info')
  77. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  78. # Start extracting information
  79. video_title = video_info.find('.//title').text
  80. video_extension = video_info.find('.//movie_type').text
  81. video_format = video_extension.upper()
  82. video_thumbnail = video_info.find('.//thumbnail_url').text
  83. video_description = video_info.find('.//description').text
  84. video_uploader_id = video_info.find('.//user_id').text
  85. video_upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  86. video_view_count = video_info.find('.//view_counter').text
  87. video_webpage_url = video_info.find('.//watch_url').text
  88. # uploader
  89. video_uploader = video_uploader_id
  90. url = 'http://seiga.nicovideo.jp/api/user/info?id=' + video_uploader_id
  91. try:
  92. user_info = self._download_xml(
  93. url, video_id, note=u'Downloading user information')
  94. video_uploader = user_info.find('.//nickname').text
  95. except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
  96. self._downloader.report_warning(u'Unable to download user info webpage: %s' % compat_str(err))
  97. return {
  98. 'id': video_id,
  99. 'url': video_real_url,
  100. 'title': video_title,
  101. 'ext': video_extension,
  102. 'format': video_format,
  103. 'thumbnail': video_thumbnail,
  104. 'description': video_description,
  105. 'uploader': video_uploader,
  106. 'upload_date': video_upload_date,
  107. 'uploader_id': video_uploader_id,
  108. 'view_count': video_view_count,
  109. 'webpage_url': video_webpage_url,
  110. }