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.

182 lines
6.6 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_urllib_parse,
  8. compat_urllib_request,
  9. compat_urlparse,
  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 used authentication to download video
  38. _AUTHENTICATED = False
  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 not username:
  45. return True
  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. # Successful login
  63. self._AUTHENTICATED = True
  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 = self._download_xml(
  72. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  73. note='Downloading video info page')
  74. if self._AUTHENTICATED:
  75. # Get flv info
  76. flv_info_webpage = self._download_webpage(
  77. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  78. video_id, 'Downloading flv info')
  79. else:
  80. # Get external player info
  81. ext_player_info = self._download_webpage(
  82. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  83. thumb_play_key = self._search_regex(
  84. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  85. # Get flv info
  86. flv_info_data = compat_urllib_parse.urlencode({
  87. 'k': thumb_play_key,
  88. 'v': video_id
  89. })
  90. flv_info_request = compat_urllib_request.Request(
  91. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  92. {'Content-Type': 'application/x-www-form-urlencoded'})
  93. flv_info_webpage = self._download_webpage(
  94. flv_info_request, video_id,
  95. note='Downloading flv info', errnote='Unable to download flv info')
  96. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  97. # Start extracting information
  98. title = video_info.find('.//title').text
  99. extension = video_info.find('.//movie_type').text
  100. video_format = extension.upper()
  101. thumbnail = video_info.find('.//thumbnail_url').text
  102. description = video_info.find('.//description').text
  103. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  104. view_count = int_or_none(video_info.find('.//view_counter').text)
  105. comment_count = int_or_none(video_info.find('.//comment_num').text)
  106. duration = parse_duration(video_info.find('.//length').text)
  107. webpage_url = video_info.find('.//watch_url').text
  108. if video_info.find('.//ch_id') is not None:
  109. uploader_id = video_info.find('.//ch_id').text
  110. uploader = video_info.find('.//ch_name').text
  111. elif video_info.find('.//user_id') is not None:
  112. uploader_id = video_info.find('.//user_id').text
  113. uploader = video_info.find('.//user_nickname').text
  114. else:
  115. uploader_id = uploader = None
  116. return {
  117. 'id': video_id,
  118. 'url': video_real_url,
  119. 'title': title,
  120. 'ext': extension,
  121. 'format': video_format,
  122. 'thumbnail': thumbnail,
  123. 'description': description,
  124. 'uploader': uploader,
  125. 'upload_date': upload_date,
  126. 'uploader_id': uploader_id,
  127. 'view_count': view_count,
  128. 'comment_count': comment_count,
  129. 'duration': duration,
  130. 'webpage_url': webpage_url,
  131. }
  132. class NiconicoPlaylistIE(InfoExtractor):
  133. _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
  134. _TEST = {
  135. 'url': 'http://www.nicovideo.jp/mylist/27411728',
  136. 'info_dict': {
  137. 'id': '27411728',
  138. 'title': 'AKB48のオールナイトニッポン',
  139. },
  140. 'playlist_mincount': 225,
  141. }
  142. def _real_extract(self, url):
  143. list_id = self._match_id(url)
  144. webpage = self._download_webpage(url, list_id)
  145. entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
  146. webpage, 'entries')
  147. entries = json.loads(entries_json)
  148. entries = [{
  149. '_type': 'url',
  150. 'ie_key': NiconicoIE.ie_key(),
  151. 'url': 'http://www.nicovideo.jp/watch/%s' % entry['item_id'],
  152. } for entry in entries]
  153. return {
  154. '_type': 'playlist',
  155. 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
  156. 'id': list_id,
  157. 'entries': entries,
  158. }