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

Code before:
        for page_number in range(1, num_pages + 1):
            time.sleep(2)

            json_str =  self._download_json(
                api_url + "&pn=%s" % page_number,'None',  query={"Search_key": query},
                note='Extracting results from page %s of %s' % (page_number, num_pages))

            # Ascending by publish date
            video_list = sorted(parsed_json['data']['archives'], key=lambda video: video['pubdate'])
            video_list_processed = list(map(lambda video: self.url_result("https://www.bilibili.com/video/%s" % video['bvid'],
                                                        'BiliBili', video['bvid']), video_list))
            for video in video_list_processed:
                num_extracted += 1
                yield video

                if num_extracted >= BilibiliCategoryIE._MAX_RESULTS:
                    return

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

    def _real_extract(self, url):
        u = compat_urllib_parse_urlparse(url)
        category = u.path.split('/')[2]
        subcategory = u.path.split('/')[3]
        query = "%s: %s" % (category, subcategory)

        return self.playlist_result(self._entries(category, subcategory, query), query, query)



class BiliBiliSearchIE(SearchInfoExtractor):
    IE_DESC = 'Bilibili video search, "bilisearch" keyword'
    _MAX_RESULTS = 100000
    _SEARCH_KEY = 'bilisearch'
    MAX_NUMBER_OF_RESULTS = 1000

    def _get_n_results(self, query, n):
        """Get a specified number of results for a query"""

        entries = []
        pageNumber = 0
        while True:
            pageNumber += 1
            # FIXME
            api_url = "https://api.bilibili.com/x/web-interface/search/type?context=&page=%s&order=pubdate&keyword=%s&duration=0&tids_2=&__refresh__=true&search_type=video&tids=0&highlight=1" % (pageNumber, query)
            json_str = self._download_webpage(
                api_url, "None", query={"Search_key": query},
                note='Extracting results from page %s' % pageNumber)
            data = json.loads(json_str)['data']

Review comment (suggestion):
```suggestion
        u = compat_urllib_parse_urlparse(url)
        category, subcategory = u.path.split('/')[2:3]
```
or should use regex groups

Code after:
            raise ExtractorError('The subcategory, %s, isn\'t supported for this category. Supported subcategories: %s' % (subcategory, list(rid_map[category].keys())))

        rid_value = rid_map[category][subcategory]

        api_url = 'https://api.bilibili.com/x/web-interface/newlist?rid=%d&type=1&ps=20&jsonp=jsonp' % rid_value
        page_json = self._download_json(api_url, query, query={'Search_key': query, 'pn': '1'})
        page_data = try_get(page_json, lambda x: x['data']['page'], dict)
        count, size = int_or_none(page_data.get('count')), int_or_none(page_data.get('size'))
        if count is None or not size:
            raise ExtractorError('Failed to calculate either page count or size')

        num_pages = math.ceil(count / size)

        return OnDemandPagedList(functools.partial(
            self._fetch_page, api_url, num_pages, query), size)

    def _real_extract(self, url):
        u = compat_urllib_parse_urlparse(url)
        category, subcategory = u.path.split('/')[2:4]
        query = '%s: %s' % (category, subcategory)

        return self.playlist_result(self._entries(category, subcategory, query), query, query)


class BiliBiliSearchIE(SearchInfoExtractor):
    IE_DESC = 'Bilibili video search, "bilisearch" keyword'
    _MAX_RESULTS = 100000
    _SEARCH_KEY = 'bilisearch'
    MAX_NUMBER_OF_RESULTS = 1000

    def _get_n_results(self, query, n):
        """Get a specified number of results for a query"""

        entries = []
        pageNumber = 0
        while True:
            pageNumber += 1
            # FIXME
            api_url = "https://api.bilibili.com/x/web-interface/search/type?context=&page=%s&order=pubdate&keyword=%s&duration=0&tids_2=&__refresh__=true&search_type=video&tids=0&highlight=1" % (pageNumber, query)
            json_str = self._download_webpage(
                api_url, "None", query={"Search_key": query},
                note='Extracting results from page %s' % pageNumber)
            data = json.loads(json_str)['data']

            # FIXME: this is hideous
            if "result" not in data:
                return {
                    '_type': 'playlist',
                    'id': query,
                    'entries': entries[:n]
                }