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.

188 lines
6.8 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..compat import (
  7. compat_urllib_parse,
  8. compat_urllib_request,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. int_or_none,
  14. parse_duration,
  15. unified_strdate,
  16. )
  17. class NiconicoIE(InfoExtractor):
  18. IE_NAME = 'niconico'
  19. IE_DESC = 'ニコニコ動画'
  20. _TEST = {
  21. 'url': 'http://www.nicovideo.jp/watch/sm22312215',
  22. 'md5': 'd1a75c0823e2f629128c43e1212760f9',
  23. 'info_dict': {
  24. 'id': 'sm22312215',
  25. 'ext': 'mp4',
  26. 'title': 'Big Buck Bunny',
  27. 'uploader': 'takuya0301',
  28. 'uploader_id': '2698420',
  29. 'upload_date': '20131123',
  30. 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
  31. 'duration': 33,
  32. },
  33. 'params': {
  34. 'username': 'ydl.niconico@gmail.com',
  35. 'password': 'youtube-dl',
  36. },
  37. }
  38. _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
  39. _NETRC_MACHINE = 'niconico'
  40. # Determine whether the downloader used authentication to download video
  41. _AUTHENTICATED = False
  42. def _real_initialize(self):
  43. self._login()
  44. def _login(self):
  45. (username, password) = self._get_login_info()
  46. # No authentication to be performed
  47. if not username:
  48. return True
  49. # Log in
  50. login_form_strs = {
  51. 'mail': username,
  52. 'password': password,
  53. }
  54. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  55. # chokes on unicode
  56. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  57. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  58. request = compat_urllib_request.Request(
  59. 'https://secure.nicovideo.jp/secure/login', login_data)
  60. login_results = self._download_webpage(
  61. request, None, note='Logging in', errnote='Unable to log in')
  62. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  63. self._downloader.report_warning('unable to log in: bad username or password')
  64. return False
  65. # Successful login
  66. self._AUTHENTICATED = True
  67. return True
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. # Get video webpage. We are not actually interested in it, but need
  71. # the cookies in order to be able to download the info webpage
  72. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  73. video_info = self._download_xml(
  74. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  75. note='Downloading video info page')
  76. if self._AUTHENTICATED:
  77. # Get flv info
  78. flv_info_webpage = self._download_webpage(
  79. 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id,
  80. video_id, 'Downloading flv info')
  81. else:
  82. # Get external player info
  83. ext_player_info = self._download_webpage(
  84. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  85. thumb_play_key = self._search_regex(
  86. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  87. # Get flv info
  88. flv_info_data = compat_urllib_parse.urlencode({
  89. 'k': thumb_play_key,
  90. 'v': video_id
  91. })
  92. flv_info_request = compat_urllib_request.Request(
  93. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  94. {'Content-Type': 'application/x-www-form-urlencoded'})
  95. flv_info_webpage = self._download_webpage(
  96. flv_info_request, video_id,
  97. note='Downloading flv info', errnote='Unable to download flv info')
  98. if 'deleted=' in flv_info_webpage:
  99. raise ExtractorError('The video has been deleted.',
  100. expected=True)
  101. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  102. # Start extracting information
  103. title = video_info.find('.//title').text
  104. extension = video_info.find('.//movie_type').text
  105. video_format = extension.upper()
  106. thumbnail = video_info.find('.//thumbnail_url').text
  107. description = video_info.find('.//description').text
  108. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  109. view_count = int_or_none(video_info.find('.//view_counter').text)
  110. comment_count = int_or_none(video_info.find('.//comment_num').text)
  111. duration = parse_duration(video_info.find('.//length').text)
  112. webpage_url = video_info.find('.//watch_url').text
  113. if video_info.find('.//ch_id') is not None:
  114. uploader_id = video_info.find('.//ch_id').text
  115. uploader = video_info.find('.//ch_name').text
  116. elif video_info.find('.//user_id') is not None:
  117. uploader_id = video_info.find('.//user_id').text
  118. uploader = video_info.find('.//user_nickname').text
  119. else:
  120. uploader_id = uploader = None
  121. return {
  122. 'id': video_id,
  123. 'url': video_real_url,
  124. 'title': title,
  125. 'ext': extension,
  126. 'format': video_format,
  127. 'thumbnail': thumbnail,
  128. 'description': description,
  129. 'uploader': uploader,
  130. 'upload_date': upload_date,
  131. 'uploader_id': uploader_id,
  132. 'view_count': view_count,
  133. 'comment_count': comment_count,
  134. 'duration': duration,
  135. 'webpage_url': webpage_url,
  136. }
  137. class NiconicoPlaylistIE(InfoExtractor):
  138. _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
  139. _TEST = {
  140. 'url': 'http://www.nicovideo.jp/mylist/27411728',
  141. 'info_dict': {
  142. 'id': '27411728',
  143. 'title': 'AKB48のオールナイトニッポン',
  144. },
  145. 'playlist_mincount': 225,
  146. }
  147. def _real_extract(self, url):
  148. list_id = self._match_id(url)
  149. webpage = self._download_webpage(url, list_id)
  150. entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
  151. webpage, 'entries')
  152. entries = json.loads(entries_json)
  153. entries = [{
  154. '_type': 'url',
  155. 'ie_key': NiconicoIE.ie_key(),
  156. 'url': ('http://www.nicovideo.jp/watch/%s' %
  157. entry['item_data']['video_id']),
  158. } for entry in entries]
  159. return {
  160. '_type': 'playlist',
  161. 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
  162. 'id': list_id,
  163. 'entries': entries,
  164. }