2 from __future__ import unicode_literals
8 from .common import InfoExtractor
24 class NiconicoIE(InfoExtractor):
29 'url': 'http://www.nicovideo.jp/watch/sm22312215',
30 'md5': 'd1a75c0823e2f629128c43e1212760f9',
34 'title': 'Big Buck Bunny',
35 'uploader': 'takuya0301',
36 'uploader_id': '2698420',
37 'upload_date': '20131123',
38 'timestamp': 1385182762,
39 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
42 'skip': 'Requires an account',
44 # File downloaded with and without credentials are different, so omit
46 'url': 'http://www.nicovideo.jp/watch/nm14296458',
50 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
51 'description': 'md5:689f066d74610b3b22e0f1739add0f58',
53 'uploader_id': '18822557',
54 'upload_date': '20110429',
55 'timestamp': 1304065916,
58 'skip': 'Requires an account',
60 # 'video exists but is marked as "deleted"
62 'url': 'http://www.nicovideo.jp/watch/sm10000',
65 'ext': 'unknown_video',
66 'description': 'deleted',
67 'title': 'ドラえもんエターナル第3話「決戦第3新東京市」<前編>',
68 'upload_date': '20071224',
69 'timestamp': int, # timestamp field has different value if logged in
72 'skip': 'Requires an account',
74 'url': 'http://www.nicovideo.jp/watch/so22543406',
78 'title': '【第1回】RADIOアニメロミックス ラブライブ!~のぞえりRadio Garden~',
79 'description': 'md5:b27d224bb0ff53d3c8269e9f8b561cf1',
80 'timestamp': 1388851200,
81 'upload_date': '20140104',
82 'uploader': 'アニメロチャンネル',
85 'skip': 'The viewing period of the video you were searching for has expired.',
88 _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
89 _NETRC_MACHINE = 'niconico'
90 # Determine whether the downloader used authentication to download video
91 _AUTHENTICATED = False
93 def _real_initialize(self):
97 (username, password) = self._get_login_info()
98 # No authentication to be performed
105 'password': password,
107 login_data = urlencode_postdata(login_form_strs)
108 request = sanitized_Request(
109 'https://secure.nicovideo.jp/secure/login', login_data)
110 login_results = self._download_webpage(
111 request, None, note='Logging in', errnote='Unable to log in')
112 if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
113 self._downloader.report_warning('unable to log in: bad username or password')
116 self._AUTHENTICATED = True
119 def _real_extract(self, url):
120 video_id = self._match_id(url)
122 # Get video webpage. We are not actually interested in it for normal
123 # cases, but need the cookies in order to be able to download the
125 webpage, handle = self._download_webpage_handle(
126 'http://www.nicovideo.jp/watch/' + video_id, video_id)
127 if video_id.startswith('so'):
128 video_id = self._match_id(handle.geturl())
130 video_info = self._download_xml(
131 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
132 note='Downloading video info page')
134 if self._AUTHENTICATED:
136 flv_info_webpage = self._download_webpage(
137 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
138 video_id, 'Downloading flv info')
140 raise ExtractorError('Niconico videos now require logging in', expected=True)
142 flv_info = compat_urlparse.parse_qs(flv_info_webpage)
143 if 'url' not in flv_info:
144 if 'deleted' in flv_info:
145 raise ExtractorError('The video has been deleted.',
148 raise ExtractorError('Unable to find video URL')
150 video_real_url = flv_info['url'][0]
152 # Start extracting information
153 title = xpath_text(video_info, './/title')
155 title = self._og_search_title(webpage, default=None)
157 title = self._html_search_regex(
158 r'<span[^>]+class="videoHeaderTitle"[^>]*>([^<]+)</span>',
159 webpage, 'video title')
161 watch_api_data_string = self._html_search_regex(
162 r'<div[^>]+id="watchAPIDataContainer"[^>]+>([^<]+)</div>',
163 webpage, 'watch api data', default=None)
164 watch_api_data = self._parse_json(watch_api_data_string, video_id) if watch_api_data_string else {}
165 video_detail = watch_api_data.get('videoDetail', {})
167 extension = xpath_text(video_info, './/movie_type')
169 extension = determine_ext(video_real_url)
172 xpath_text(video_info, './/thumbnail_url') or
173 self._html_search_meta('image', webpage, 'thumbnail', default=None) or
174 video_detail.get('thumbnail'))
176 description = xpath_text(video_info, './/description')
178 timestamp = parse_iso8601(xpath_text(video_info, './/first_retrieve'))
180 match = self._html_search_meta('datePublished', webpage, 'date published', default=None)
182 timestamp = parse_iso8601(match.replace('+', ':00+'))
183 if not timestamp and video_detail.get('postedAt'):
184 timestamp = parse_iso8601(
185 video_detail['postedAt'].replace('/', '-'),
186 delimiter=' ', timezone=datetime.timedelta(hours=9))
188 view_count = int_or_none(xpath_text(video_info, './/view_counter'))
190 match = self._html_search_regex(
191 r'>Views: <strong[^>]*>([^<]+)</strong>',
192 webpage, 'view count', default=None)
194 view_count = int_or_none(match.replace(',', ''))
195 view_count = view_count or video_detail.get('viewCount')
197 comment_count = int_or_none(xpath_text(video_info, './/comment_num'))
198 if not comment_count:
199 match = self._html_search_regex(
200 r'>Comments: <strong[^>]*>([^<]+)</strong>',
201 webpage, 'comment count', default=None)
203 comment_count = int_or_none(match.replace(',', ''))
204 comment_count = comment_count or video_detail.get('commentCount')
206 duration = (parse_duration(
207 xpath_text(video_info, './/length') or
208 self._html_search_meta(
209 'video:duration', webpage, 'video duration', default=None)) or
210 video_detail.get('length'))
212 webpage_url = xpath_text(video_info, './/watch_url') or url
214 if video_info.find('.//ch_id') is not None:
215 uploader_id = video_info.find('.//ch_id').text
216 uploader = video_info.find('.//ch_name').text
217 elif video_info.find('.//user_id') is not None:
218 uploader_id = video_info.find('.//user_id').text
219 uploader = video_info.find('.//user_nickname').text
221 uploader_id = uploader = None
225 'url': video_real_url,
228 'format_id': 'economy' if video_real_url.endswith('low') else 'normal',
229 'thumbnail': thumbnail,
230 'description': description,
231 'uploader': uploader,
232 'timestamp': timestamp,
233 'uploader_id': uploader_id,
234 'view_count': view_count,
235 'comment_count': comment_count,
236 'duration': duration,
237 'webpage_url': webpage_url,
241 class NiconicoPlaylistIE(InfoExtractor):
242 _VALID_URL = r'https?://(?:www\.)?nicovideo\.jp/mylist/(?P<id>\d+)'
245 'url': 'http://www.nicovideo.jp/mylist/27411728',
248 'title': 'AKB48のオールナイトニッポン',
250 'playlist_mincount': 225,
253 def _real_extract(self, url):
254 list_id = self._match_id(url)
255 webpage = self._download_webpage(url, list_id)
257 entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
259 entries = json.loads(entries_json)
262 'ie_key': NiconicoIE.ie_key(),
263 'url': ('http://www.nicovideo.jp/watch/%s' %
264 entry['item_data']['video_id']),
265 } for entry in entries]
269 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),