2 from __future__ import unicode_literals
7 from .common import InfoExtractor
26 class NiconicoIE(InfoExtractor):
31 'url': 'http://www.nicovideo.jp/watch/sm22312215',
32 'md5': 'd1a75c0823e2f629128c43e1212760f9',
36 'title': 'Big Buck Bunny',
37 'thumbnail': r're:https?://.*',
38 'uploader': 'takuya0301',
39 'uploader_id': '2698420',
40 'upload_date': '20131123',
41 'timestamp': 1385182762,
42 'description': '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org',
47 'skip': 'Requires an account',
49 # File downloaded with and without credentials are different, so omit
51 'url': 'http://www.nicovideo.jp/watch/nm14296458',
55 'title': '【鏡音リン】Dance on media【オリジナル】take2!',
56 'description': 'md5:689f066d74610b3b22e0f1739add0f58',
57 'thumbnail': r're:https?://.*',
59 'uploader_id': '18822557',
60 'upload_date': '20110429',
61 'timestamp': 1304065916,
64 'skip': 'Requires an account',
66 # 'video exists but is marked as "deleted"
68 'url': 'http://www.nicovideo.jp/watch/sm10000',
71 'ext': 'unknown_video',
72 'description': 'deleted',
73 'title': 'ドラえもんエターナル第3話「決戦第3新東京市」<前編>',
74 'thumbnail': r're:https?://.*',
75 'upload_date': '20071224',
76 'timestamp': int, # timestamp field has different value if logged in
80 'skip': 'Requires an account',
82 'url': 'http://www.nicovideo.jp/watch/so22543406',
86 'title': '【第1回】RADIOアニメロミックス ラブライブ!~のぞえりRadio Garden~',
87 'description': 'md5:b27d224bb0ff53d3c8269e9f8b561cf1',
88 'thumbnail': r're:https?://.*',
89 'timestamp': 1388851200,
90 'upload_date': '20140104',
91 'uploader': 'アニメロチャンネル',
94 'skip': 'The viewing period of the video you were searching for has expired.',
96 # video not available via `getflv`
97 'url': 'http://www.nicovideo.jp/watch/sm1151009',
101 'title': 'マスターシステム本体内蔵のスペハリのメインテーマ(PSG版)',
102 'description': 'md5:6ee077e0581ff5019773e2e714cdd0b7',
103 'thumbnail': r're:https?://.*',
105 'timestamp': 1190868283,
106 'upload_date': '20070927',
107 'uploader': 'denden2',
108 'uploader_id': '1392194',
110 'comment_count': int,
112 'skip': 'Requires an account',
114 'url': 'http://sp.nicovideo.jp/watch/sm28964488?ss_pos=1&cp_in=wt_tg',
115 'only_matching': True,
118 _VALID_URL = r'https?://(?:www\.|secure\.|sp\.)?nicovideo\.jp/watch/(?P<id>(?:[a-z]{2})?[0-9]+)'
119 _NETRC_MACHINE = 'niconico'
121 def _real_initialize(self):
125 (username, password) = self._get_login_info()
126 # No authentication to be performed
133 'mail_tel': username,
134 'password': password,
136 urlh = self._request_webpage(
137 'https://account.nicovideo.jp/api/v1/login', None,
138 note='Logging in', errnote='Unable to log in',
139 data=urlencode_postdata(login_form_strs))
143 parts = compat_urlparse.urlparse(urlh.geturl())
144 if compat_parse_qs(parts.query).get('message', [None])[0] == 'cant_login':
147 self._downloader.report_warning('unable to log in: bad username or password')
150 def _real_extract(self, url):
151 video_id = self._match_id(url)
153 # Get video webpage. We are not actually interested in it for normal
154 # cases, but need the cookies in order to be able to download the
156 webpage, handle = self._download_webpage_handle(
157 'http://www.nicovideo.jp/watch/' + video_id, video_id)
158 if video_id.startswith('so'):
159 video_id = self._match_id(handle.geturl())
161 api_data = self._parse_json(self._html_search_regex(
162 'data-api-data="([^"]+)"', webpage,
163 'API data', default='{}'), video_id)
164 video_real_url = try_get(
165 api_data, lambda x: x['video']['smileInfo']['url'])
168 def get_video_info(items):
169 return dict_get(api_data['video'], items)
172 flv_info_webpage = self._download_webpage(
173 'http://flapi.nicovideo.jp/api/getflv/' + video_id + '?as3=1',
174 video_id, 'Downloading flv info')
176 flv_info = compat_urlparse.parse_qs(flv_info_webpage)
177 if 'url' not in flv_info:
178 if 'deleted' in flv_info:
179 raise ExtractorError('The video has been deleted.',
181 elif 'closed' in flv_info:
182 raise ExtractorError('Niconico videos now require logging in',
184 elif 'error' in flv_info:
185 raise ExtractorError('%s reports error: %s' % (
186 self.IE_NAME, flv_info['error'][0]), expected=True)
188 raise ExtractorError('Unable to find video URL')
190 video_real_url = flv_info['url'][0]
192 video_info_xml = self._download_xml(
193 'http://ext.nicovideo.jp/api/getthumbinfo/' + video_id,
194 video_id, note='Downloading video info page')
196 def get_video_info(items):
197 if not isinstance(items, list):
200 ret = xpath_text(video_info_xml, './/' + item)
204 # Start extracting information
205 title = get_video_info('title')
207 title = self._og_search_title(webpage, default=None)
209 title = self._html_search_regex(
210 r'<span[^>]+class="videoHeaderTitle"[^>]*>([^<]+)</span>',
211 webpage, 'video title')
213 watch_api_data_string = self._html_search_regex(
214 r'<div[^>]+id="watchAPIDataContainer"[^>]+>([^<]+)</div>',
215 webpage, 'watch api data', default=None)
216 watch_api_data = self._parse_json(watch_api_data_string, video_id) if watch_api_data_string else {}
217 video_detail = watch_api_data.get('videoDetail', {})
219 extension = get_video_info(['movie_type', 'movieType'])
221 extension = determine_ext(video_real_url)
224 get_video_info(['thumbnail_url', 'thumbnailURL']) or
225 self._html_search_meta('image', webpage, 'thumbnail', default=None) or
226 video_detail.get('thumbnail'))
228 description = get_video_info('description')
230 timestamp = (parse_iso8601(get_video_info('first_retrieve')) or
231 unified_timestamp(get_video_info('postedDateTime')))
233 match = self._html_search_meta('datePublished', webpage, 'date published', default=None)
235 timestamp = parse_iso8601(match.replace('+', ':00+'))
236 if not timestamp and video_detail.get('postedAt'):
237 timestamp = parse_iso8601(
238 video_detail['postedAt'].replace('/', '-'),
239 delimiter=' ', timezone=datetime.timedelta(hours=9))
241 view_count = int_or_none(get_video_info(['view_counter', 'viewCount']))
243 match = self._html_search_regex(
244 r'>Views: <strong[^>]*>([^<]+)</strong>',
245 webpage, 'view count', default=None)
247 view_count = int_or_none(match.replace(',', ''))
248 view_count = view_count or video_detail.get('viewCount')
250 comment_count = (int_or_none(get_video_info('comment_num')) or
251 video_detail.get('commentCount') or
252 try_get(api_data, lambda x: x['thread']['commentCount']))
253 if not comment_count:
254 match = self._html_search_regex(
255 r'>Comments: <strong[^>]*>([^<]+)</strong>',
256 webpage, 'comment count', default=None)
258 comment_count = int_or_none(match.replace(',', ''))
260 duration = (parse_duration(
261 get_video_info('length') or
262 self._html_search_meta(
263 'video:duration', webpage, 'video duration', default=None)) or
264 video_detail.get('length') or
265 get_video_info('duration'))
267 webpage_url = get_video_info('watch_url') or url
269 owner = api_data.get('owner', {})
270 uploader_id = get_video_info(['ch_id', 'user_id']) or owner.get('id')
271 uploader = get_video_info(['ch_name', 'user_nickname']) or owner.get('nickname')
275 'url': video_real_url,
278 'format_id': 'economy' if video_real_url.endswith('low') else 'normal',
279 'thumbnail': thumbnail,
280 'description': description,
281 'uploader': uploader,
282 'timestamp': timestamp,
283 'uploader_id': uploader_id,
284 'view_count': view_count,
285 'comment_count': comment_count,
286 'duration': duration,
287 'webpage_url': webpage_url,
291 class NiconicoPlaylistIE(InfoExtractor):
292 _VALID_URL = r'https?://(?:www\.)?nicovideo\.jp/mylist/(?P<id>\d+)'
295 'url': 'http://www.nicovideo.jp/mylist/27411728',
298 'title': 'AKB48のオールナイトニッポン',
300 'playlist_mincount': 225,
303 def _real_extract(self, url):
304 list_id = self._match_id(url)
305 webpage = self._download_webpage(url, list_id)
307 entries_json = self._search_regex(r'Mylist\.preload\(\d+, (\[.*\])\);',
309 entries = json.loads(entries_json)
312 'ie_key': NiconicoIE.ie_key(),
313 'url': ('http://www.nicovideo.jp/watch/%s' %
314 entry['item_data']['video_id']),
315 } for entry in entries]
319 'title': self._search_regex(r'\s+name: "(.*?)"', webpage, 'title'),