Repository: yt-dlp/yt-dlp
File: youtube_dlc/downloader/external.py (Python)

Code before:
        return cli_valueless_option(self.params, command_option, param, expected_value)

    def _configuration_args(self, default=[]):
        return cli_configuration_args(
            self.params, 'external_downloader_args', self.get_basename(), default)[0]

    def _call_downloader(self, tmpfilename, info_dict):
        """ Either overwrite this or implement _make_cmd """
        cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]

        self._debug_cmd(cmd)

        p = subprocess.Popen(
            cmd, stderr=subprocess.PIPE)
        _, stderr = process_communicate_or_kill(p)
        if p.returncode != 0:
            self.to_stderr(stderr.decode('utf-8', 'replace'))

        if 'url_list' in info_dict:
            file_list = []
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.frag' % (tmpfilename, i)
                file_list.append(tmpsegmentname)
            with open(tmpfilename, 'wb') as dest:
                for i in file_list:
                    if 'decrypt_info' in info_dict:
                        decrypt_info = info_dict['decrypt_info']
                        with open(i, 'rb') as src:
                            if decrypt_info['METHOD'] == 'AES-128':
                                iv = decrypt_info.get('IV')
                                decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen(
                                    self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read()
                                encrypted_data = src.read()
                                decrypted_data = AES.new(
                                    decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data)
                                dest.write(decrypted_data)
                            else:
                                shutil.copyfileobj(open(i, 'rb'), dest)
                    else:
                        shutil.copyfileobj(open(i, 'rb'), dest)
            if not self.params.get('keep_fragments', False):
                for file_path in file_list:
                    try:
                        os.remove(file_path)
                    except OSError as e:
                        print("Error: %s : %s" % (file_path, e.strerror))
                try:
                    file_path = '%s.frag.urls' % tmpfilename
                    os.remove(file_path)
                except OSError as e:
                    print("Error: %s : %s" % (file_path, e.strerror))

Review comment (suggestion):
Ideally this should obey `--continue`/`--no-overwrite`, but we can ignore it for now if implementing it is too complicated

Code after:

    def _valueless_option(self, command_option, param, expected_value=True):
        return cli_valueless_option(self.params, command_option, param, expected_value)

    def _configuration_args(self, default=[]):
        return cli_configuration_args(
            self.params, 'external_downloader_args', self.get_basename(), default)[0]

    def _call_downloader(self, tmpfilename, info_dict):
        """ Either overwrite this or implement _make_cmd """
        cmd = [encodeArgument(a) for a in self._make_cmd(tmpfilename, info_dict)]

        self._debug_cmd(cmd)

        p = subprocess.Popen(
            cmd, stderr=subprocess.PIPE)
        _, stderr = process_communicate_or_kill(p)
        if p.returncode != 0:
            self.to_stderr(stderr.decode('utf-8', 'replace'))

        if 'url_list' in info_dict:
            file_list = []
            for [i, url] in enumerate(info_dict['url_list']):
                tmpsegmentname = '%s_%s.frag' % (tmpfilename, i)
                file_list.append(tmpsegmentname)
            with open(tmpfilename, 'wb') as dest:
                for i in file_list:
                    if 'decrypt_info' in info_dict:
                        decrypt_info = info_dict['decrypt_info']
                        with open(i, 'rb') as src:
                            if decrypt_info['METHOD'] == 'AES-128':
                                iv = decrypt_info.get('IV')
                                decrypt_info['KEY'] = decrypt_info.get('KEY') or self.ydl.urlopen(
                                    self._prepare_url(info_dict, info_dict.get('_decryption_key_url') or decrypt_info['URI'])).read()
                                encrypted_data = src.read()
                                decrypted_data = AES.new(
                                    decrypt_info['KEY'], AES.MODE_CBC, iv).decrypt(encrypted_data)
                                dest.write(decrypted_data)
                            else:
                                shutil.copyfileobj(open(i, 'rb'), dest)
                    else:
                        shutil.copyfileobj(open(i, 'rb'), dest)
            if not self.params.get('keep_fragments', False):
                for file_path in file_list:
                    try:
                        os.remove(file_path)
                    except OSError as ose:
                        self.report_error("Unable to delete file %s; %s" % (file_path, error_to_compat_str(ose)))
                try:
                    file_path = '%s.frag.urls' % tmpfilename
                    os.remove(file_path)