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.

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