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

Code before:

    def _extract_and_report_alerts(self, data, *args, **kwargs):
        return self._report_alerts(self._extract_alerts(data), *args, **kwargs)

    def _extract_badges(self, renderer: dict):
        badges = set()
        for badge in try_get(renderer, lambda x: x['badges'], list) or []:
            label = try_get(badge, lambda x: x['metadataBadgeRenderer']['label'], compat_str)
            if label:
                badges.add(label.lower())
        return badges

    @staticmethod
    def _get_text(data, getter=None, max_runs=None):
        for get in variadic(getter):
            d = try_get(data, get) or data
            text = try_get(d, lambda x: x['simpleText'], compat_str)
            if text:
                return text
            runs = try_get(d, lambda x: x['runs'], list) or []
            if not runs and isinstance(d, list):
                runs = d

            def get_runs(runs):
                for i in range(0, min(len(runs), max_runs or len(runs))):
                    yield try_get(runs[i], lambda x: x['text'], compat_str) or ''

            text = ''.join(get_runs(runs))
            if text:
                return text

    def _extract_response(self, item_id, query, note='Downloading API JSON', headers=None,
                          ytcfg=None, check_get_keys=None, ep='browse', fatal=True, api_hostname=None,
                          default_client='WEB'):
        response = None
        last_error = None
        count = -1
        retries = self.get_param('extractor_retries', 3)
        if check_get_keys is None:
            check_get_keys = []
        while count < retries:
            count += 1
            if last_error:
                self.report_warning('%s. Retrying ...' % last_error)
            try:
                response = self._call_api(
                    ep=ep, fatal=True, headers=headers,
                    video_id=item_id, query=query,
                    context=self._extract_context(ytcfg, default_client),
                    api_key=self._extract_api_key(ytcfg, default_client),
                    api_hostname=api_hostname, default_client=default_client,

Review comment (suggestion):
why not
```suggestion
                for run in runs[:max_runs]:
                    yield try_get(run, lambda x: x['text'], compat_str) or ''
```


Code after:
            else:
                warnings.append([alert_type, alert_message])

        for alert_type, alert_message in (warnings + errors[:-1]):
            self.report_warning('YouTube said: %s - %s' % (alert_type, alert_message))
        if errors:
            raise ExtractorError('YouTube said: %s' % errors[-1][1], expected=expected)

    def _extract_and_report_alerts(self, data, *args, **kwargs):
        return self._report_alerts(self._extract_alerts(data), *args, **kwargs)

    def _extract_badges(self, renderer: dict):
        badges = set()
        for badge in try_get(renderer, lambda x: x['badges'], list) or []:
            label = try_get(badge, lambda x: x['metadataBadgeRenderer']['label'], compat_str)
            if label:
                badges.add(label.lower())
        return badges

    @staticmethod
    def _get_text(data, getter=None, max_runs=None):
        for get in variadic(getter):
            d = try_get(data, get) if get is not None else data
            text = try_get(d, lambda x: x['simpleText'], compat_str)
            if text:
                return text
            runs = try_get(d, lambda x: x['runs'], list) or []
            if not runs and isinstance(d, list):
                runs = d

            def get_runs(runs):
                for run in runs[:min(len(runs), max_runs or len(runs))]:
                    yield try_get(run, lambda x: x['text'], compat_str) or ''

            text = ''.join(get_runs(runs))
            if text:
                return text

    def _extract_response(self, item_id, query, note='Downloading API JSON', headers=None,
                          ytcfg=None, check_get_keys=None, ep='browse', fatal=True, api_hostname=None,
                          default_client='WEB'):
        response = None
        last_error = None
        count = -1
        retries = self.get_param('extractor_retries', 3)
        if check_get_keys is None:
            check_get_keys = []
        while count < retries:
            count += 1
            if last_error:
                self.report_warning('%s. Retrying ...' % last_error)