Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/niconico.py (Python)

Code before:
            'id': 'sm9',
            'title': 'sm9'
        },
        'playlist_mincount': 320,
    }]

    _API_HEADERS = {
        'X-Frontend-ID': '6',
        'X-Frontend-Version': '0'
    }

    def _entries(self, query):
        """Get a specified number of results for a query"""
        currDate = datetime.datetime.now().date()

        search_url = "http://www.nicovideo.jp/search/%s" % query
        spans = self._get_spans_for_range(search_url, query, self._START_DATE, currDate)

        for (start_date, end_date) in spans:
            videos = self._get_entries_for_date(search_url, query, start_date, endDate=end_date)
            for video in videos:
                yield video

    def _get_spans_for_range(self, url, query, startDate, endDate):
        page_50_results = self._get_entries_for_date(url, query, startDate, endDate=endDate, pageNumber=50)
        page_50_length = sum(1 for i in page_50_results)

        # If the page 50 results return 32 videos, we need to break down the query interval to ensure we've captured all videos
        if (page_50_length == self._RESULTS_PER_PAGE and startDate != endDate):
            midpoint = startDate + (endDate - startDate) / 2
            right = self._get_spans_for_range(url, query, startDate, midpoint)
            left = self._get_spans_for_range(url, query, midpoint, endDate)
            spans = left + right
        else:
            spans = [[startDate, endDate]]

        return spans

    def _get_entries_for_date(self, url, query, startDate, endDate=None, pageNumber=1):
        if endDate is None:
            endDate = startDate

        while True:
            link = url + "?page=" + str(pageNumber) + "&start=" + str(startDate) + "&end=" + str(endDate) + "&sort=f&order=d"
            results = self._download_webpage(link, "None", query={"Search_key": query}, note='Extracting results from page %s for date %s to %s' % (pageNumber, startDate, endDate))
            r = re.findall(r'(?<=data-video-id=)["\']?(?P<videoid>.*?)(?=["\'])', results)

            for item in r:
                yield self.url_result("http://www.nicovideo.jp/watch/" + item, 'Niconico', item)

            # each page holds a maximum of 32 entries. If we've seen 32 entries on the current page,

Review comment (performance):
```suggestion
        page_50_length = sum(1 for _ in self._get_entries_for_date(url, query, startDate, endDate=endDate, pageNumber=50))
```
could also just use `len(list(generator()))`, performance benefit (if any) doesn't really matter in this context.

Code after:
            '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 _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'http://www.nicovideo.jp/watch/{item}', 'Niconico', item)
            if not results:
                break

    def _real_extract(self, url):
        query = self._match_id(url)
        return self.playlist_result(self._entries(url, query), query, query)


class NicovideoSearchIE(SearchInfoExtractor, NicovideoSearchURLIE):
    IE_DESC = 'Nico video searches'
    _MAX_RESULTS = float('inf')
    IE_NAME = NicovideoSearchIE_NAME
    _SEARCH_KEY = 'nicosearch'
    _TESTS = []

    def _get_n_results(self, query, n):
        entries = self._entries(self._proto_relative_url(f'//www.nicovideo.jp/search/{query}'), query)
        if n < float('inf'):
            entries = itertools.islice(entries, 0, n)
        return self.playlist_result(entries, query, query)


class NicovideoSearchDateIE(NicovideoSearchIE):
    IE_DESC = 'Nico video searches, newest first'
    IE_NAME = f'{NicovideoSearchIE_NAME}:date'
    _SEARCH_KEY = 'nicosearchdate'
    _TESTS = [{
        'url': 'nicosearchdateall:a',
        'info_dict': {
            'id': 'a',