niconico.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. def _real_initialize(self):
  38. self._login()
  39. def _login(self):
  40. (username, password) = self._get_login_info()
  41. if username is None:
  42. # Login is required
  43. raise ExtractorError('No login info available, needed for using %s.' % self.IE_NAME, expected=True)
  44. # Log in
  45. login_form_strs = {
  46. 'mail': username,
  47. 'password': password,
  48. }
  49. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  50. # chokes on unicode
  51. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  52. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  53. request = compat_urllib_request.Request(
  54. 'https://secure.nicovideo.jp/secure/login', login_data)
  55. login_results = self._download_webpage(
  56. request, None, note='Logging in', errnote='Unable to log in')
  57. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  58. self._downloader.report_warning('unable to log in: bad username or password')
  59. return False
  60. return True
  61. def _real_extract(self, url):
  62. mobj = re.match(self._VALID_URL, url)
  63. video_id = mobj.group(1)
  64. # Get video webpage. We are not actually interested in it, but need
  65. # the cookies in order to be able to download the info webpage
  66. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  67. video_info = self._download_xml(
  68. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  69. note='Downloading video info page')
  70. # Get flv info
  71. flv_info_webpage = self._download_webpage(
  72. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  73. video_id, 'Downloading flv info')
  74. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  75. # Start extracting information
  76. title = video_info.find('.//title').text
  77. extension = video_info.find('.//movie_type').text
  78. video_format = extension.upper()
  79. thumbnail = video_info.find('.//thumbnail_url').text
  80. description = video_info.find('.//description').text
  81. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  82. view_count = int_or_none(video_info.find('.//view_counter').text)
  83. comment_count = int_or_none(video_info.find('.//comment_num').text)
  84. duration = parse_duration(video_info.find('.//length').text)
  85. webpage_url = video_info.find('.//watch_url').text
  86. if video_info.find('.//ch_id') is not None:
  87. uploader_id = video_info.find('.//ch_id').text
  88. uploader = video_info.find('.//ch_name').text
  89. elif video_info.find('.//user_id') is not None:
  90. uploader_id = video_info.find('.//user_id').text
  91. uploader = video_info.find('.//user_nickname').text
  92. else:
  93. uploader_id = uploader = None
  94. return {
  95. 'id': video_id,
  96. 'url': video_real_url,
  97. 'title': title,
  98. 'ext': extension,
  99. 'format': video_format,
  100. 'thumbnail': thumbnail,
  101. 'description': description,
  102. 'uploader': uploader,
  103. 'upload_date': upload_date,
  104. 'uploader_id': uploader_id,
  105. 'view_count': view_count,
  106. 'comment_count': comment_count,
  107. 'duration': duration,
  108. 'webpage_url': webpage_url,
  109. }