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.

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