2 from __future__ import unicode_literals
7 from .common import InfoExtractor
10 compat_urllib_request,
23 class NiconicoIE(InfoExtractor):
28 'url': 'http://www.nicovideo.jp/watch/sm22312215',
29 'md5': 'd1a75c0823e2f629128c43e1212760f9',
33 'title': 'Big Buck Bunny',
34 'uploader': 'takuya0301',
35 'uploader_id': '2698420',
36 'upload_date': '20131123',
37 'timestamp': 1385182762,
38 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
42 # File downloaded with and without credentials are different, so omit
44 'url': 'http://www.nicovideo.jp/watch/nm14296458',
48 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
49 'description': 'md5:689f066d74610b3b22e0f1739add0f58',
51 'uploader_id': '18822557',
52 'upload_date': '20110429',
53 'timestamp': 1304065916,
57 # 'video exists but is marked as "deleted"
58 'url': 'http://www.nicovideo.jp/watch/sm10000',
59 'md5': '38e53c9aad548f3ecf01ca7680b59b08',
62 'ext': 'unknown_video',
63 'description': 'deleted',
64 'title': 'ドラえもんエターナル第3話「決戦第3新東京市」<前編>',
68 _VALID_URL = r'https?://(?:www\.|secure\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
69 _NETRC_MACHINE = 'niconico'
70 # Determine whether the downloader used authentication to download video
71 _AUTHENTICATED = False
73 def _real_initialize(self):
77 (username, password) = self._get_login_info()
78 # No authentication to be performed
87 # Convert to UTF-8 *before* urlencode because Python 2.x's urlencode
89 login_form = dict((k.encode('utf-8'), v.encode('utf-8')) for k, v in login_form_strs.items())
90 login_data = compat_urllib_parse.urlencode(login_form).encode('utf-8')
91 request = compat_urllib_request.Request(
92 'https://secure.nicovideo.jp/secure/login', login_data)
93 login_results = self._download_webpage(
94 request, None, note='Logging in', errnote='Unable to log in')
95 if re.search(r'(?i)<h1 class="mb8p4">Log in error</h1>', login_results) is not None:
96 self._downloader.report_warning('unable to log in: bad username or password')
99 self._AUTHENTICATED = True
102 def _real_extract(self, url):
103 video_id = self._match_id(url)
105 # Get video webpage. We are not actually interested in it for normal
106 # cases, but need the cookies in order to be able to download the
108 webpage = self._download_webpage('http://www.nicovideo.jp/watch/' + video_id, video_id)
110 video_info = self._download_xml(
111 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id, video_id,
112 note='Downloading video info page')
114 if self._AUTHENTICATED:
116 flv_info_webpage = self._download_webpage(
117 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
118 video_id, 'Downloading flv info')
120 # Get external player info
121 ext_player_info = self._download_webpage(
122 'http://ext.nicovideo.jp/thumb_watch/' + video_id, video_id)
123 thumb_play_key = self._search_regex(
124 r'\'thumbPlayKey\'\s*:\s*\'(.*?)\'', ext_player_info, 'thumbPlayKey')
127 flv_info_data = compat_urllib_parse.urlencode({
131 flv_info_request = compat_urllib_request.Request(
132 'http://ext.nicovideo.jp/thumb_watch', flv_info_data,
133 {'Content-Type': 'application/x-www-form-urlencoded'})
134 flv_info_webpage = self._download_webpage(
135 flv_info_request, video_id,
136 note='Downloading flv info', errnote='Unable to download flv info')
138 flv_info = compat_urlparse.parse_qs(flv_info_webpage)
139 if 'url' not in flv_info:
140 if 'deleted' in flv_info:
141 raise ExtractorError('The video has been deleted.',
144 raise ExtractorError('Unable to find video URL')
146 video_real_url = flv_info['url'][0]
148 # Start extracting information
149 title = xpath_text(video_info, './/title')
151 title = self._og_search_title(webpage, default=None)
153 title = self._html_search_regex(
154 r'<span[^>]+class="videoHeaderTitle"[^>]*>([^<]+)</span>',
155 webpage, 'video title')
157 extension = xpath_text(video_info, './/movie_type')
159 extension = determine_ext(video_real_url)
160 video_format = extension.upper()
161 thumbnail = xpath_text(video_info, './/thumbnail_url')
162 description = xpath_text(video_info, './/description')
163 timestamp = parse_iso8601(xpath_text(video_info, './/first_retrieve'))
164 view_count = int_or_none(xpath_text(video_info, './/view_counter'))
165 comment_count = int_or_none(xpath_text(video_info, './/comment_num'))
166 duration = parse_duration(xpath_text(video_info, './/length'))
167 webpage_url = xpath_text(video_info, './/watch_url')
169 if video_info.find('.//ch_id') is not None:
170 uploader_id = video_info.find('.//ch_id').text
171 uploader = video_info.find('.//ch_name').text
172 elif video_info.find('.//user_id') is not None:
173 uploader_id = video_info.find('.//user_id').text
174 uploader = video_info.find('.//user_nickname').text
176 uploader_id = uploader = None
180 'url': video_real_url,
183 'format': video_format,
184 'thumbnail': thumbnail,
185 'description': description,
186 'uploader': uploader,
187 'timestamp': timestamp,
188 'uploader_id': uploader_id,
189 'view_count': view_count,
190 'comment_count': comment_count,
191 'duration': duration,
192 'webpage_url': webpage_url,
194 return dict((k, v) for k, v in ret.items() if v is not None)
197 class NiconicoPlaylistIE(InfoExtractor):
198 _VALID_URL = r'https?://www\.nicovideo\.jp/mylist/(?P<id>\d+)'
201 'url': 'http://www.nicovideo.jp/mylist/27411728',
204 'title': 'AKB48のオールナイトニッポン',
206 'playlist_mincount': 225,
209 def _real_extract(self, url):
210 list_id = self._match_id(url)
211 webpage = self._download_webpage(url, list_id)
213 entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
215 entries = json.loads(entries_json)
218 'ie_key': NiconicoIE.ie_key(),
219 'url': ('http://www.nicovideo.jp/watch/%s' %
220 entry['item_data']['video_id']),
221 } for entry in entries]
225 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),