Repository: yt-dlp/yt-dlp
File: yt_dlp/downloader/youtube_live_chat.py (Python)

Code before:
        start_time = int(time.time() * 1000)

        def dl_fragment(url, data=None, headers=None):
            http_headers = info_dict.get('http_headers', {})
            if headers:
                http_headers = http_headers.copy()
                http_headers.update(headers)
            return self._download_fragment(ctx, url, info_dict, http_headers, data)

        def parse_actions_replay(live_chat_continuation):
            offset = continuation_id = None
            processed_fragment = bytearray()
            for action in live_chat_continuation.get('actions', []):
                if 'replayChatItemAction' in action:
                    replay_chat_item_action = action['replayChatItemAction']
                    offset = int(replay_chat_item_action['videoOffsetTimeMsec'])
                processed_fragment.extend(
                    json.dumps(action, ensure_ascii=False).encode('utf-8') + b'\n')
            if offset is not None:
                continuation_id = try_get(
                    live_chat_continuation,
                    lambda x: x['continuations'][0]['liveChatReplayContinuationData']['continuation'])
            self._append_fragment(ctx, processed_fragment)
            return continuation_id, offset

        def parse_live_timestamp(action):
            action_content = (
                try_get(action, lambda x: x['addChatItemAction'], dict)
                or try_get(action, lambda x: x['addLiveChatTickerItemAction'], dict)
                or try_get(action, lambda x: x['addBannerToLiveChatCommand'], dict)
            )
            if action_content is None or type(action_content) != dict:
                return None
            item = (
                try_get(action_content, lambda x: x['item'], dict)
                or try_get(action_content, lambda x: x['bannerRenderer'], dict)
            )
            if item is None:
                return None
            renderer = (
                # text
                try_get(item, lambda x: x['liveChatTextMessageRenderer'], dict)
                or try_get(item, lambda x: x['liveChatPaidMessageRenderer'], dict)
                or try_get(item, lambda x: x['liveChatMembershipItemRenderer'], dict)
                or try_get(item, lambda x: x['liveChatPaidStickerRenderer'], dict)
                # ticker
                or try_get(item, lambda x: x['liveChatTickerPaidMessageItemRenderer'], dict)
                or try_get(item, lambda x: x['liveChatTickerSponsorItemRenderer'], dict)
                # banner
                or try_get(item, lambda x: x['liveChatBannerRenderer'], dict)
            )

Review comment (suggestion):
Try to put the functions outside `real_download` where possible (ie, when it doesn't actually make use of the scope). This one especially sticks out since it should really be a static function of the class imo

Code after:
        ie = YT_BaseIE(self.ydl)

        start_time = int(time.time() * 1000)

        def dl_fragment(url, data=None, headers=None):
            http_headers = info_dict.get('http_headers', {})
            if headers:
                http_headers = http_headers.copy()
                http_headers.update(headers)
            return self._download_fragment(ctx, url, info_dict, http_headers, data)

        def parse_actions_replay(live_chat_continuation):
            offset = continuation_id = None
            processed_fragment = bytearray()
            for action in live_chat_continuation.get('actions', []):
                if 'replayChatItemAction' in action:
                    replay_chat_item_action = action['replayChatItemAction']
                    offset = int(replay_chat_item_action['videoOffsetTimeMsec'])
                processed_fragment.extend(
                    json.dumps(action, ensure_ascii=False).encode('utf-8') + b'\n')
            if offset is not None:
                continuation_id = try_get(
                    live_chat_continuation,
                    lambda x: x['continuations'][0]['liveChatReplayContinuationData']['continuation'])
            self._append_fragment(ctx, processed_fragment)
            return continuation_id, offset

        live_offset = 0

        def parse_actions_live(live_chat_continuation):
            nonlocal live_offset
            continuation_id = None
            processed_fragment = bytearray()
            for action in live_chat_continuation.get('actions', []):
                timestamp = self.parse_live_timestamp(action)
                if timestamp is not None:
                    live_offset = timestamp - start_time
                # compatibility with replay format
                pseudo_action = {
                    'replayChatItemAction': {'actions': [action]},
                    'videoOffsetTimeMsec': str(live_offset),
                    'isLive': True,
                }
                processed_fragment.extend(
                    json.dumps(pseudo_action, ensure_ascii=False).encode('utf-8') + b'\n')
            continuation_data_getters = [
                lambda x: x['continuations'][0]['invalidationContinuationData'],
                lambda x: x['continuations'][0]['timedContinuationData'],
            ]
            continuation_data = try_get(live_chat_continuation, continuation_data_getters, dict)
            if continuation_data: