+class NiconicoSeriesIE(InfoExtractor):
+ IE_NAME = 'niconico:series'
+ _VALID_URL = r'https?://(?:(?:www\.|sp\.)?nicovideo\.jp(?:/user/\d+)?|nico\.ms)/series/(?P<id>\d+)'
+
+ _TESTS = [{
+ 'url': 'https://www.nicovideo.jp/user/44113208/series/110226',
+ 'info_dict': {
+ 'id': '110226',
+ 'title': 'ご立派ァ!のシリーズ',
+ },
+ 'playlist_mincount': 10,
+ }, {
+ 'url': 'https://www.nicovideo.jp/series/12312/',
+ 'info_dict': {
+ 'id': '12312',
+ 'title': 'バトルスピリッツ お勧めカード紹介(調整中)',
+ },
+ 'playlist_mincount': 103,
+ }, {
+ 'url': 'https://nico.ms/series/203559',
+ 'only_matching': True,
+ }]
+
+ def _real_extract(self, url):
+ list_id = self._match_id(url)
+ webpage = self._download_webpage(url, list_id)
+
+ title = self._search_regex(
+ (r'<title>「(.+)(全',
+ r'<div class="TwitterShareButton"\s+data-text="(.+)\s+https:'),
+ webpage, 'title', fatal=False)
+ if title:
+ title = unescapeHTML(title)
+ json_data = next(self._yield_json_ld(webpage, None, fatal=False))
+ return self.playlist_from_matches(
+ traverse_obj(json_data, ('itemListElement', ..., 'url')), list_id, title, ie=NiconicoIE)
+
+
+class NiconicoHistoryIE(NiconicoPlaylistBaseIE):
+ IE_NAME = 'niconico:history'
+ IE_DESC = 'NicoNico user history or likes. Requires cookies.'
+ _VALID_URL = r'https?://(?:www\.|sp\.)?nicovideo\.jp/my/(?P<id>history(?:/like)?)'
+
+ _TESTS = [{
+ 'note': 'PC page, with /video',
+ 'url': 'https://www.nicovideo.jp/my/history/video',
+ 'only_matching': True,
+ }, {
+ 'note': 'PC page, without /video',
+ 'url': 'https://www.nicovideo.jp/my/history',
+ 'only_matching': True,
+ }, {
+ 'note': 'mobile page, with /video',
+ 'url': 'https://sp.nicovideo.jp/my/history/video',
+ 'only_matching': True,
+ }, {
+ 'note': 'mobile page, without /video',
+ 'url': 'https://sp.nicovideo.jp/my/history',
+ 'only_matching': True,
+ }, {
+ 'note': 'PC page',
+ 'url': 'https://www.nicovideo.jp/my/history/like',
+ 'only_matching': True,
+ }, {
+ 'note': 'Mobile page',
+ 'url': 'https://sp.nicovideo.jp/my/history/like',
+ 'only_matching': True,
+ }]
+
+ def _call_api(self, list_id, resource, query):
+ path = 'likes' if list_id == 'history/like' else 'watch/history'
+ return self._download_json(
+ f'https://nvapi.nicovideo.jp/v1/users/me/{path}', list_id,
+ f'Downloading {resource}', query=query, headers=self._API_HEADERS)['data']
+
+ def _real_extract(self, url):
+ list_id = self._match_id(url)
+ try:
+ mylist = self._call_api(list_id, 'list', {'pageSize': 1})
+ except ExtractorError as e:
+ if isinstance(e.cause, HTTPError) and e.cause.status == 401:
+ self.raise_login_required('You have to be logged in to get your history')
+ raise
+ return self.playlist_result(self._entries(list_id), list_id, **self._parse_owner(mylist))
+
+
+class NicovideoSearchBaseIE(InfoExtractor):
+ _SEARCH_TYPE = 'search'
+
+ def _entries(self, url, item_id, query=None, note='Downloading page %(page)s'):
+ query = query or {}
+ pages = [query['page']] if 'page' in query else itertools.count(1)
+ for page_num in pages:
+ query['page'] = str(page_num)
+ webpage = self._download_webpage(url, item_id, query=query, note=note % {'page': page_num})
+ results = re.findall(r'(?<=data-video-id=)["\']?(?P<videoid>.*?)(?=["\'])', webpage)
+ for item in results:
+ yield self.url_result(f'https://www.nicovideo.jp/watch/{item}', 'Niconico', item)
+ if not results:
+ break
+
+ def _search_results(self, query):
+ return self._entries(
+ self._proto_relative_url(f'//www.nicovideo.jp/{self._SEARCH_TYPE}/{query}'), query)
+
+
+class NicovideoSearchIE(NicovideoSearchBaseIE, SearchInfoExtractor):
+ IE_DESC = 'Nico video search'
+ IE_NAME = 'nicovideo:search'
+ _SEARCH_KEY = 'nicosearch'
+
+
+class NicovideoSearchURLIE(NicovideoSearchBaseIE):
+ IE_NAME = f'{NicovideoSearchIE.IE_NAME}_url'
+ IE_DESC = 'Nico video search URLs'
+ _VALID_URL = r'https?://(?:www\.)?nicovideo\.jp/search/(?P<id>[^?#&]+)?'
+ _TESTS = [{
+ 'url': 'http://www.nicovideo.jp/search/sm9',
+ 'info_dict': {
+ 'id': 'sm9',
+ 'title': 'sm9',
+ },
+ 'playlist_mincount': 40,
+ }, {
+ 'url': 'https://www.nicovideo.jp/search/sm9?sort=h&order=d&end=2020-12-31&start=2020-01-01',
+ 'info_dict': {
+ 'id': 'sm9',
+ 'title': 'sm9',
+ },
+ 'playlist_count': 31,
+ }]
+
+ def _real_extract(self, url):
+ query = self._match_id(url)
+ return self.playlist_result(self._entries(url, query), query, query)
+
+
+class NicovideoSearchDateIE(NicovideoSearchBaseIE, SearchInfoExtractor):
+ IE_DESC = 'Nico video search, newest first'
+ IE_NAME = f'{NicovideoSearchIE.IE_NAME}:date'
+ _SEARCH_KEY = 'nicosearchdate'
+ _TESTS = [{
+ 'url': 'nicosearchdateall:a',
+ 'info_dict': {
+ 'id': 'a',
+ 'title': 'a',
+ },
+ 'playlist_mincount': 1610,
+ }]
+
+ _START_DATE = dt.date(2007, 1, 1)
+ _RESULTS_PER_PAGE = 32
+ _MAX_PAGES = 50
+
+ def _entries(self, url, item_id, start_date=None, end_date=None):
+ start_date, end_date = start_date or self._START_DATE, end_date or dt.datetime.now().date()
+
+ # If the last page has a full page of videos, we need to break down the query interval further
+ last_page_len = len(list(self._get_entries_for_date(
+ url, item_id, start_date, end_date, self._MAX_PAGES,
+ note=f'Checking number of videos from {start_date} to {end_date}')))
+ if (last_page_len == self._RESULTS_PER_PAGE and start_date != end_date):
+ midpoint = start_date + ((end_date - start_date) // 2)
+ yield from self._entries(url, item_id, midpoint, end_date)
+ yield from self._entries(url, item_id, start_date, midpoint)
+ else:
+ self.to_screen(f'{item_id}: Downloading results from {start_date} to {end_date}')
+ yield from self._get_entries_for_date(
+ url, item_id, start_date, end_date, note=' Downloading page %(page)s')
+
+ def _get_entries_for_date(self, url, item_id, start_date, end_date=None, page_num=None, note=None):
+ query = {
+ 'start': str(start_date),
+ 'end': str(end_date or start_date),
+ 'sort': 'f',
+ 'order': 'd',