niconico.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import datetime
  6. from .common import InfoExtractor
  7. from ..compat import (
  8. compat_urllib_parse_urlencode,
  9. compat_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. int_or_none,
  14. parse_duration,
  15. parse_iso8601,
  16. sanitized_Request,
  17. xpath_text,
  18. determine_ext,
  19. )
  20. class NiconicoIE(InfoExtractor):
  21. IE_NAME = 'niconico'
  22. IE_DESC = 'ニコニコ動画'
  23. _TESTS = [{
  24. 'url': 'http://www.nicovideo.jp/watch/sm22312215',
  25. 'md5': 'd1a75c0823e2f629128c43e1212760f9',
  26. 'info_dict': {
  27. 'id': 'sm22312215',
  28. 'ext': 'mp4',
  29. 'title': 'Big Buck Bunny',
  30. 'uploader': 'takuya0301',
  31. 'uploader_id': '2698420',
  32. 'upload_date': '20131123',
  33. 'timestamp': 1385182762,
  34. 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
  35. 'duration': 33,
  36. },
  37. }, {
  38. # File downloaded with and without credentials are different, so omit
  39. # the md5 field
  40. 'url': 'http://www.nicovideo.jp/watch/nm14296458',
  41. 'info_dict': {
  42. 'id': 'nm14296458',
  43. 'ext': 'swf',
  44. 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
  45. 'description': 'md5:689f066d74610b3b22e0f1739add0f58',
  46. 'uploader': 'りょうた',
  47. 'uploader_id': '18822557',
  48. 'upload_date': '20110429',
  49. 'timestamp': 1304065916,
  50. 'duration': 209,
  51. },
  52. }, {
  53. # 'video exists but is marked as "deleted"
  54. # md5 is unstable
  55. 'url': 'http://www.nicovideo.jp/watch/sm10000',
  56. 'info_dict': {
  57. 'id': 'sm10000',
  58. 'ext': 'unknown_video',
  59. 'description': 'deleted',
  60. 'title': 'ドラえもんエターナル第3話「決戦第3新東京市」<前編>',
  61. 'upload_date': '20071224',
  62. 'timestamp': 1198527840, # timestamp field has different value if logged in
  63. 'duration': 304,
  64. },
  65. }, {
  66. 'url': 'http://www.nicovideo.jp/watch/so22543406',
  67. 'info_dict': {
  68. 'id': '1388129933',
  69. 'ext': 'mp4',
  70. 'title': '【第1回】RADIOアニメロミックス ラブライブ!~のぞえりRadio Garden~',
  71. 'description': 'md5:b27d224bb0ff53d3c8269e9f8b561cf1',
  72. 'timestamp': 1388851200,
  73. 'upload_date': '20140104',
  74. 'uploader': 'アニメロチャンネル',
  75. 'uploader_id': '312',
  76. }
  77. }]
  78. _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
  79. _NETRC_MACHINE = 'niconico'
  80. # Determine whether the downloader used authentication to download video
  81. _AUTHENTICATED = False
  82. def _real_initialize(self):
  83. self._login()
  84. def _login(self):
  85. (username, password) = self._get_login_info()
  86. # No authentication to be performed
  87. if not username:
  88. return True
  89. # Log in
  90. login_form_strs = {
  91. 'mail': username,
  92. 'password': password,
  93. }
  94. login_data = compat_urllib_parse_urlencode(login_form_strs).encode('utf-8')
  95. request = sanitized_Request(
  96. 'https://secure.nicovideo.jp/secure/login', login_data)
  97. login_results = self._download_webpage(
  98. request, None, note='Logging in', errnote='Unable to log in')
  99. if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
  100. self._downloader.report_warning('unable to log in: bad username or password')
  101. return False
  102. # Successful login
  103. self._AUTHENTICATED = True
  104. return True
  105. def _real_extract(self, url):
  106. video_id = self._match_id(url)
  107. # Get video webpage. We are not actually interested in it for normal
  108. # cases, but need the cookies in order to be able to download the
  109. # info webpage
  110. webpage, handle = self._download_webpage_handle(
  111. 'http://www.nicovideo.jp/watch/' + video_id, video_id)
  112. if video_id.startswith('so'):
  113. video_id = self._match_id(handle.geturl())
  114. video_info = self._download_xml(
  115. 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
  116. note='Downloading video info page')
  117. if self._AUTHENTICATED:
  118. # Get flv info
  119. flv_info_webpage = self._download_webpage(
  120. 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
  121. video_id, 'Downloading flv info')
  122. else:
  123. # Get external player info
  124. ext_player_info = self._download_webpage(
  125. 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
  126. thumb_play_key = self._search_regex(
  127. r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
  128. # Get flv info
  129. flv_info_data = compat_urllib_parse_urlencode({
  130. 'k': thumb_play_key,
  131. 'v': video_id
  132. })
  133. flv_info_request = sanitized_Request(
  134. 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
  135. {'Content-Type': 'application/x-www-form-urlencoded'})
  136. flv_info_webpage = self._download_webpage(
  137. flv_info_request, video_id,
  138. note='Downloading flv info', errnote='Unable to download flv info')
  139. flv_info = compat_urlparse.parse_qs(flv_info_webpage)
  140. if 'url' not in flv_info:
  141. if 'deleted' in flv_info:
  142. raise ExtractorError('The video has been deleted.',
  143. expected=True)
  144. else:
  145. raise ExtractorError('Unable to find video URL')
  146. video_real_url = flv_info['url'][0]
  147. # Start extracting information
  148. title = xpath_text(video_info, './/title')
  149. if not title:
  150. title = self._og_search_title(webpage, default=None)
  151. if not title:
  152. title = self._html_search_regex(
  153. r'<span[^>]+class="videoHeaderTitle"[^>]*>([^<]+)</span>',
  154. webpage, 'video title')
  155. watch_api_data_string = self._html_search_regex(
  156. r'<div[^>]+id="watchAPIDataContainer"[^>]+>([^<]+)</div>',
  157. webpage, 'watch api data', default=None)
  158. watch_api_data = self._parse_json(watch_api_data_string, video_id) if watch_api_data_string else {}
  159. video_detail = watch_api_data.get('videoDetail', {})
  160. extension = xpath_text(video_info, './/movie_type')
  161. if not extension:
  162. extension = determine_ext(video_real_url)
  163. thumbnail = (
  164. xpath_text(video_info, './/thumbnail_url') or
  165. self._html_search_meta('image', webpage, 'thumbnail', default=None) or
  166. video_detail.get('thumbnail'))
  167. description = xpath_text(video_info, './/description')
  168. timestamp = parse_iso8601(xpath_text(video_info, './/first_retrieve'))
  169. if not timestamp:
  170. match = self._html_search_meta('datePublished', webpage, 'date published', default=None)
  171. if match:
  172. timestamp = parse_iso8601(match.replace('+', ':00+'))
  173. if not timestamp and video_detail.get('postedAt'):
  174. timestamp = parse_iso8601(
  175. video_detail['postedAt'].replace('/', '-'),
  176. delimiter=' ', timezone=datetime.timedelta(hours=9))
  177. view_count = int_or_none(xpath_text(video_info, './/view_counter'))
  178. if not view_count:
  179. match = self._html_search_regex(
  180. r'>Views: <strong[^>]*>([^<]+)</strong>',
  181. webpage, 'view count', default=None)
  182. if match:
  183. view_count = int_or_none(match.replace(',', ''))
  184. view_count = view_count or video_detail.get('viewCount')
  185. comment_count = int_or_none(xpath_text(video_info, './/comment_num'))
  186. if not comment_count:
  187. match = self._html_search_regex(
  188. r'>Comments: <strong[^>]*>([^<]+)</strong>',
  189. webpage, 'comment count', default=None)
  190. if match:
  191. comment_count = int_or_none(match.replace(',', ''))
  192. comment_count = comment_count or video_detail.get('commentCount')
  193. duration = (parse_duration(
  194. xpath_text(video_info, './/length') or
  195. self._html_search_meta(
  196. 'video:duration', webpage, 'video duration', default=None)) or
  197. video_detail.get('length'))
  198. webpage_url = xpath_text(video_info, './/watch_url') or url
  199. if video_info.find('.//ch_id') is not None:
  200. uploader_id = video_info.find('.//ch_id').text
  201. uploader = video_info.find('.//ch_name').text
  202. elif video_info.find('.//user_id') is not None:
  203. uploader_id = video_info.find('.//user_id').text
  204. uploader = video_info.find('.//user_nickname').text
  205. else:
  206. uploader_id = uploader = None
  207. return {
  208. 'id': video_id,
  209. 'url': video_real_url,
  210. 'title': title,
  211. 'ext': extension,
  212. 'format_id': 'economy' if video_real_url.endswith('low') else 'normal',
  213. 'thumbnail': thumbnail,
  214. 'description': description,
  215. 'uploader': uploader,
  216. 'timestamp': timestamp,
  217. 'uploader_id': uploader_id,
  218. 'view_count': view_count,
  219. 'comment_count': comment_count,
  220. 'duration': duration,
  221. 'webpage_url': webpage_url,
  222. }
  223. class NiconicoPlaylistIE(InfoExtractor):
  224. _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
  225. _TEST = {
  226. 'url': 'http://www.nicovideo.jp/mylist/27411728',
  227. 'info_dict': {
  228. 'id': '27411728',
  229. 'title': 'AKB48のオールナイトニッポン',
  230. },
  231. 'playlist_mincount': 225,
  232. }
  233. def _real_extract(self, url):
  234. list_id = self._match_id(url)
  235. webpage = self._download_webpage(url, list_id)
  236. entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
  237. webpage, 'entries')
  238. entries = json.loads(entries_json)
  239. entries = [{
  240. '_type': 'url',
  241. 'ie_key': NiconicoIE.ie_key(),
  242. 'url': ('http://www.nicovideo.jp/watch/%s' %
  243. entry['item_data']['video_id']),
  244. } for entry in entries]
  245. return {
  246. '_type': 'playlist',
  247. 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),
  248. 'id': list_id,
  249. 'entries': entries,
  250. }