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

Code before:
        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,
            # it's possible there may be another, so we can check. It's a little awkward, but it works.
            if(len(r) < self._RESULTS_PER_PAGE or pageNumber == self._MAX_NUMBER_OF_PAGES):
                break

            pageNumber += 1

    @classmethod
    def _make_valid_url(cls):
        return cls._VALID_URL

    def _real_extract(self, url):

        u = compat_urllib_parse_urlparse(url)
        query = u.path.split("/")[2]

        return self.playlist_result(self._entries(query), query, query)
        # return self.playlist_result(self._get_n_results(query, self._MAX_RESULTS), playlist_id=query, playlist_title=query)


Review comment (suggestion):
```suggestion
            link = '%s?page=%s&start=%s&end=%s&sort=f&order=d' % (url, pageNumber, startDate, endDate)
```
prefer using formatting instead of concatting, or use f-strings if that'd look cleaner

Code after:
            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',
            'title': 'a'
        },
        'playlist_mincount': 1610,
    }]

    _START_DATE = datetime.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 datetime.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)