<commit_before>﻿class PxerPrinter{
    constructor(config){
        
        /**
         * 计算得到的下载地址
         * @type {string[]} 
         * */
        this.address =[];
        /**计算得到的任务信息*/
        this.taskInfo ='';
        /**剥离的动图参数*/
        this.ugoiraFrames ={};

        /**配置信息*/
        this.config =PxerPrinter.defaultConfig();
        config &&this.setConfig(config);

    };

    /**
     * 设置配置信息
     * @param {string|Object} key - 要设置的key或是一个将被遍历合并的对象
     * @param {string} [value] - 要设置的value
     * */
    setConfig(key ,value){
        if(arguments.length ===1 && typeof key ==='object'){
            let obj =key;
            for(let objKey in obj){
                if(objKey in this.config) this.config[objKey] =obj[objKey];
                else console.warn(`PxerPrinter.setConfig: skip key "${objKey}"`);
            };
        }else{
            if(!(key in this.config)) throw new Error(`PxerPrinter.setConfig: ${key} is not in config`);
            this.config[key]=value;
        }
        return this;
    };
};

/**
 * 根据作品列表将下载地址填充到PxerPrinter#address
 * @param {PxerWorks[]} worksList
 * @return {void}
 * */
PxerPrinter.prototype['fillAddress'] =function(worksList){
    for(let works of worksList){
        let configKey =PxerPrinter.getWorksKey(works);
        if(configKey==='ugoira_zip' &&this.config['ugoira_frames']==='yes'){
            this.ugoiraFrames[works.id] =works.frames
        }
        if(!(configKey in this.config)) throw new Error(`PxerPrinter.fillAddress: ${configKey} in not in config`);
        if(this.config[configKey]==='no') continue;
        this.address.push(...PxerPrinter.countAddress(works,this.config[configKey]));
    }
};

/**
 * 根据作品将可读的下载信息填充到PxerPrinter#taskInfo
 * @param {PxerWorks[]} worksList
 * @return {void}
 * */
PxerPrinter.prototype['fillTaskInfo'] =function(worksList){
    var [manga,ugoira,illust,multiple,single,worksNum,address] =new Array(20).fill(0);
    for(let works of worksList){
        let configKey =PxerPrinter.getWorksKey(works);
        if(this.config[configKey]==='no') continue;

        worksNum++;

        switch(works.type){
            case 'manga':
                manga++;
                break;
            case 'ugoira':
                ugoira++;
                break;
            case 'illust':
                illust++;
                break;
            default:
                console.error(works);
                throw new Error(`PxerPrinter.fillTaskInfo: works.type illegal`);
                break;
        };

        if(works instanceof PxerMultipleWorks){
            multiple++;
            address +=works.multiple;
        }else if(works instanceof PxerWorks){//动图
            address++;
            single++;
        }else{
            console.error(works);
            throw new Error(`PxerPrinter.fillTaskInfo: works instanceof illegal`);
        };
    }


    this.taskInfo =`
共计${worksNum}个作品，${address}个下载地址。<br />
单张图片作品占 ${(single/worksNum*100).toFixed(1)}%<br />
多张图片作品占 ${(multiple/worksNum*100).toFixed(1)}%<br />
`.trim();
};
/**
 * 将结果输出
 * 确保下载地址和任务信息已被填充
 * */
PxerPrinter.prototype['print'] =function(){

    /**判断输出动图参数*/
    if((this.config['ugoira_frames'] ==="yes")&&(Object.keys(this.ugoiraFrames).length !==0)){
        let win =window.open(document.URL ,'_blank');
        if(!win){
            alert('Pxer:\n浏览器拦截了弹出窗口，请检查浏览器提示，设置允许此站点的弹出式窗口。');
            return;
        };
        let str =[
            '<pre>',
            '/** 这个页面是动图压缩包的动画参数，目前Pxer还无法将动图压缩包打包成GIF，请寻找其他第三方软件 */',
            JSON.stringify(this.ugoiraFrames ,null ,4),<commit_msg>ffmpeg batch-style ugoira param<commit_after>﻿class PxerPrinter{
    constructor(config){
        
        /**
         * 计算得到的下载地址
         * @type {string[]} 
         * */
        this.address =[];
        /**计算得到的任务信息*/
        this.taskInfo ='';
        /**剥离的动图参数*/
        this.ugoiraFrames ={};

        /**配置信息*/
        this.config =PxerPrinter.defaultConfig();
        config &&this.setConfig(config);

    };

    /**
     * 设置配置信息
     * @param {string|Object} key - 要设置的key或是一个将被遍历合并的对象
     * @param {string} [value] - 要设置的value
     * */
    setConfig(key ,value){
        if(arguments.length ===1 && typeof key ==='object'){
            let obj =key;
            for(let objKey in obj){
                if(objKey in this.config) this.config[objKey] =obj[objKey];
                else console.warn(`PxerPrinter.setConfig: skip key "${objKey}"`);
            };
        }else{
            if(!(key in this.config)) throw new Error(`PxerPrinter.setConfig: ${key} is not in config`);
            this.config[key]=value;
        }
        return this;
    };
};

/**
 * 根据作品列表将下载地址填充到PxerPrinter#address
 * @param {PxerWorks[]} worksList
 * @return {void}
 * */
PxerPrinter.prototype['fillAddress'] =function(worksList){
    for(let works of worksList){
        let configKey =PxerPrinter.getWorksKey(works);
        if(configKey==='ugoira_zip' &&this.config['ugoira_frames']==='yes'){
            this.ugoiraFrames[works.id] =works.frames
        }
        if(!(configKey in this.config)) throw new Error(`PxerPrinter.fillAddress: ${configKey} in not in config`);
        if(this.config[configKey]==='no') continue;
        this.address.push(...PxerPrinter.countAddress(works,this.config[configKey]));
    }
};

/**
 * 根据作品将可读的下载信息填充到PxerPrinter#taskInfo
 * @param {PxerWorks[]} worksList
 * @return {void}
 * */
PxerPrinter.prototype['fillTaskInfo'] =function(worksList){
    var [manga,ugoira,illust,multiple,single,worksNum,address] =new Array(20).fill(0);
    for(let works of worksList){
        let configKey =PxerPrinter.getWorksKey(works);
        if(this.config[configKey]==='no') continue;

        worksNum++;

        switch(works.type){
            case 'manga':
                manga++;
                break;
            case 'ugoira':
                ugoira++;
                break;
            case 'illust':
                illust++;
                break;
            default:
                console.error(works);
                throw new Error(`PxerPrinter.fillTaskInfo: works.type illegal`);
                break;
        };

        if(works instanceof PxerMultipleWorks){
            multiple++;
            address +=works.multiple;
        }else if(works instanceof PxerWorks){//动图
            address++;
            single++;
        }else{
            console.error(works);
            throw new Error(`PxerPrinter.fillTaskInfo: works instanceof illegal`);
        };
    }


    this.taskInfo =`
共计${worksNum}个作品，${address}个下载地址。<br />
单张图片作品占 ${(single/worksNum*100).toFixed(1)}%<br />
多张图片作品占 ${(multiple/worksNum*100).toFixed(1)}%<br />
`.trim();
};
/**
 * 将结果输出
 * 确保下载地址和任务信息已被填充
 * */
PxerPrinter.prototype['print'] =function(){

    /**判断输出动图参数*/
    if((this.config['ugoira_frames'] ==="yes")&&(Object.keys(this.ugoiraFrames).length !==0)){
        let win =window.open(document.URL ,'_blank');
        if(!win){
            alert('Pxer:\n浏览器拦截了弹出窗口，请检查浏览器提示，设置允许此站点的弹出式窗口。');
            return;
        };
        
        var lines=[];
        var resstring;
        switch (this.config.ugoira_zip) {
            case "max": resstring = "1920x1080"; break;
            case "600p": resstring = "600x338"; break;
        }

        var slashstr = "";
        switch (navigator.platform) {
            case "Win32":
                slashstr="^";
                lines.push("@echo off");
                lines.push("set /p ext=请输入输出文件扩展名(mp4/gif/...):");
                break;
            default:
                slashstr="\\";
                lines.push("#!/bin/bash");
                lines.push("");
                lines.push("read -p '请输入输出文件扩展名(mp4/gif/...):' ext");
                break;
        }
        for (key in this.ugoiraFrames) {
            var foldername = key + "_ugoira" + resstring;
            var confpath = foldername + "/config.txt";
            lines.push("del "+ foldername + "\\config.txt");
            for (frame of this.ugoiraFrames[key]) {
                lines.push("echo file "+slashstr+"'" + frame['file']+ slashstr +"' >> "+confpath);
                lines.push("echo duration " + frame['delay']/1000 + " >> "+ confpath);
            }
            lines.push("echo file "+ slashstr + "'" +this.ugoiraFrames[key][this.ugoiraFrames[key].length-1]['file'] + slashstr + "' >> "+confpath);
            lines.push("ffmpeg -f concat -i "+confpath+" -framerate 30 -vsync -1 -s "+resstring+" "+foldername+"/remux." + (navigator.platform==="Win32"? "%ext%":"$ext"));
        }
        switch (navigator.platform) {
            case "Win32":   lines.push("pause"); break;
            default: lines.push("read  -n 1 -p \"按任意键退出\" m && echo"); break;
        }

        let str =[
            '<pre>',
            '/** 这个页面是动图压缩包的动画参数，目前Pxer还无法将动图压缩包打包成GIF，请寻找其他第三方软件 */',
            ...lines,
{'fraction_non_alphanumeric': 0.15940560621411684, 'fraction_numerical': 0.00686704942024091, 'mean_word_length': 2.0435080507022954, 'pattern_counts': {'":': 5, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1538542', 'n_tokens_mistral': 3153, 'n_tokens_neox': 3191, 'n_words': 481}