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.

205 lines
7.4 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. _TESTS = [{
  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. 'url': 'http://www.nicovideo.jp/watch/nm14296458',
  39. 'md5': '8db08e0158457cf852a31519fceea5bc',
  40. 'info_dict': {
  41. 'id': 'nm14296458',
  42. 'ext': 'swf',
  43. 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
  44. 'description': 'md5:',
  45. 'uploader': 'りょうた',
  46. 'uploader_id': '18822557',
  47. 'upload_date': '20110429',
  48. 'duration': 209,
  49. },
  50. 'params': {
  51. 'username': 'ydl.niconico@gmail.com',
  52. 'password': 'youtube-dl',
  53. },
  54. }]
  55. _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
  56. _NETRC_MACHINE = 'niconico'
  57. # Determine whether the downloader used authentication to download video
  58. _AUTHENTICATED = False
  59. def _real_initialize(self):
  60. self._login()
  61. def _login(self):
  62. (username, password) = self._get_login_info()
  63. # No authentication to be performed
  64. if not username:
  65. return True
  66. # Log in
  67. login_form_strs = {
  68. 'mail': username,
  69. 'password': password,
  70. }
  71. # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
  72. # chokes on unicode
  73. login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
  74. login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
  75. request = compat_urllib_request.Request(
  76. 'https://secure.nicovideo.jp/secure/login', login_data)
  77. login_results = self._download_webpage(
  78. request, None, note='Logging in', errnote='Unable to log in')
  79. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  80. self._downloader.report_warning('unable to log in: bad username or password')
  81. return False
  82. # Successful login
  83. self._AUTHENTICATED = True
  84. return True
  85. def _real_extract(self, url):
  86. video_id = self._match_id(url)
  87. # Get video webpage. We are not actually interested in it, but need
  88. # the cookies in order to be able to download the info webpage
  89. self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
  90. video_info = self._download_xml(
  91. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  92. note='Downloading video info page')
  93. if self._AUTHENTICATED:
  94. # Get flv info
  95. flv_info_webpage = self._download_webpage(
  96. 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
  97. video_id, 'Downloading flv info')
  98. else:
  99. # Get external player info
  100. ext_player_info = self._download_webpage(
  101. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  102. thumb_play_key = self._search_regex(
  103. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  104. # Get flv info
  105. flv_info_data = compat_urllib_parse.urlencode({
  106. 'k': thumb_play_key,
  107. 'v': video_id
  108. })
  109. flv_info_request = compat_urllib_request.Request(
  110. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  111. {'Content-Type': 'application/x-www-form-urlencoded'})
  112. flv_info_webpage = self._download_webpage(
  113. flv_info_request, video_id,
  114. note='Downloading flv info', errnote='Unable to download flv info')
  115. if 'deleted=' in flv_info_webpage:
  116. raise ExtractorError('The video has been deleted.',
  117. expected=True)
  118. video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]
  119. # Start extracting information
  120. title = video_info.find('.//title').text
  121. extension = video_info.find('.//movie_type').text
  122. video_format = extension.upper()
  123. thumbnail = video_info.find('.//thumbnail_url').text
  124. description = video_info.find('.//description').text
  125. upload_date = unified_strdate(video_info.find('.//first_retrieve').text.split('+')[0])
  126. view_count = int_or_none(video_info.find('.//view_counter').text)
  127. comment_count = int_or_none(video_info.find('.//comment_num').text)
  128. duration = parse_duration(video_info.find('.//length').text)
  129. webpage_url = video_info.find('.//watch_url').text
  130. if video_info.find('.//ch_id') is not None:
  131. uploader_id = video_info.find('.//ch_id').text
  132. uploader = video_info.find('.//ch_name').text
  133. elif video_info.find('.//user_id') is not None:
  134. uploader_id = video_info.find('.//user_id').text
  135. uploader = video_info.find('.//user_nickname').text
  136. else:
  137. uploader_id = uploader = None
  138. return {
  139. 'id': video_id,
  140. 'url': video_real_url,
  141. 'title': title,
  142. 'ext': extension,
  143. 'format': video_format,
  144. 'thumbnail': thumbnail,
  145. 'description': description,
  146. 'uploader': uploader,
  147. 'upload_date': upload_date,
  148. 'uploader_id': uploader_id,
  149. 'view_count': view_count,
  150. 'comment_count': comment_count,
  151. 'duration': duration,
  152. 'webpage_url': webpage_url,
  153. }
  154. class NiconicoPlaylistIE(InfoExtractor):
  155. _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
  156. _TEST = {
  157. 'url': 'http://www.nicovideo.jp/mylist/27411728',
  158. 'info_dict': {
  159. 'id': '27411728',
  160. 'title': 'AKB48のオールナイトニッポン',
  161. },
  162. 'playlist_mincount': 225,
  163. }
  164. def _real_extract(self, url):
  165. list_id = self._match_id(url)
  166. webpage = self._download_webpage(url, list_id)
  167. entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
  168. webpage, 'entries')
  169. entries = json.loads(entries_json)
  170. entries = [{
  171. '_type': 'url',
  172. 'ie_key': NiconicoIE.ie_key(),
  173. 'url': ('http://www.nicovideo.jp/watch/%s' %
  174. entry['item_data']['video_id']),
  175. } for entry in entries]
  176. return {
  177. '_type': 'playlist',
  178. 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
  179. 'id': list_id,
  180. 'entries': entries,
  181. }