- video_id = self._extract_id(url)
-
- # Get video webpage
- self.report_video_webpage_download(video_id)
- url = 'http://www.nicovideo.jp/watch/' + video_id
- request = compat_urllib_request.Request(url)
- try:
- video_webpage = compat_urllib_request.urlopen(request).read()
- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
- raise ExtractorError(u'Unable to download video webpage: %s' % compat_str(err))
-
- # Get video info
- self.report_video_info_webpage_download(video_id)
- url = 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id
- request = compat_urllib_request.Request(url)
- try:
- video_info_webpage = compat_urllib_request.urlopen(request).read()
- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
- raise ExtractorError(u'Unable to download video info webpage: %s' % compat_str(err))
-
- # Get flv info
- self.report_flv_info_webpage_download(video_id)
- url = 'http://flapi.nicovideo.jp/api/getflv?v=' + video_id
- request = compat_urllib_request.Request(url)
- try:
- flv_info_webpage = compat_urllib_request.urlopen(request).read()
- except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
- raise ExtractorError(u'Unable to download flv info webpage: %s' % compat_str(err))
+ video_id = self._match_id(url)
+
+ # Get video webpage. We are not actually interested in it, but need
+ # the cookies in order to be able to download the info webpage
+ self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
+
+ video_info = self._download_xml(
+ 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
+ note='Downloading video info page')
+
+ if self._AUTHENTICATED:
+ # Get flv info
+ flv_info_webpage = self._download_webpage(
+ 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
+ video_id, 'Downloading flv info')
+ else:
+ # Get external player info
+ ext_player_info = self._download_webpage(
+ 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
+ thumb_play_key = self._search_regex(
+ r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
+
+ # Get flv info
+ flv_info_data = compat_urllib_parse.urlencode({
+ 'k': thumb_play_key,
+ 'v': video_id
+ })
+ flv_info_request = compat_urllib_request.Request(
+ 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
+ {'Content-Type': 'application/x-www-form-urlencoded'})
+ flv_info_webpage = self._download_webpage(
+ flv_info_request, video_id,
+ note='Downloading flv info', errnote='Unable to download flv info')
+
+ if 'deleted=' in flv_info_webpage:
+ raise ExtractorError('The video has been deleted.',
+ expected=True)
+ video_real_url = compat_urlparse.parse_qs(flv_info_webpage)['url'][0]