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.

131 lines
5.1 KiB

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