Tfe

Ongi etorri tfe-ren webgunera...

Blog/jokoak_euskaraz/shadow_tactics/jsons_to_csv_vo.php

(Deskargatu)
<?php

if (!function_exists('str_putcsv')) {
    function str_putcsv($input, $delimiter = ',', $enclosure = '"') {
        $fp = fopen('php://temp', 'r+b');
        fputcsv($fp, $input, $delimiter, $enclosure);
        rewind($fp);
        $data = rtrim(stream_get_contents($fp), "\n");
        fclose($fp);
        return $data;
    }
}

$jsons= glob("jsons/*.json");
foreach($jsons as $json)
{
    if(preg_match("/vo_/", $json))
    {
        $data = file_get_contents($json);
        $decoded = json_decode($data,true);
        array_unshift($decoded, $json, $decoded["m_Name"]);

        file_put_contents("/tmp/test.csv", $decoded["m_Script"]);

        if (($handle = fopen("/tmp/test.csv", "r")) !== FALSE){
            while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
                $es = @$data[10];

                $data[11] = "$es SOURCE_TRANSLATION";
                if($es!="Translation" && preg_match("/[a-zA-Z]{2,}/", $es))
                {
                    //$data[10] = shell_exec('timeout 10s curl -s \'https://api.euskadi.eus/itzuli/es2eu/v2/translate\' -X POST -H \'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:131.0) Gecko/20100101 Firefox/131.0\' -H \'Accept: application/json, text/javascript, */*; q=0.01\' -H \'Accept-Language: eu,es;q=0.8,fr;q=0.5,en-US;q=0.3\' -H \'Accept-Encoding: gzip, deflate, br, zstd\' -H \'Content-Type: application/json\' -H \'Origin: https://www.euskadi.eus\' -H \'DNT: 1\' -H \'Connection: keep-alive\' -H \'Referer: https://www.euskadi.eus/\' -H \'Sec-Fetch-Dest: empty\' -H \'Sec-Fetch-Mode: cors\' -H \'Sec-Fetch-Site: same-site\' -H \'Pragma: no-cache\' -H \'Cache-Control: no-cache\' --data-raw \'{"mkey":"8d9016025eb0a44215c7f69c2e10861d","text":'.json_encode($es).',"model":"generic_es2eu"}\' | jq -r ".translation" 2>&1 ');

                    array_unshift($data, $json, $decoded["m_Name"]);
                    print str_putcsv($data)."\n";
                }
                //$str= join("\t", $data)."\n";
                //$str = preg_replace("/\n+$/","",$str);
                //print $str."\n";
            }
        }
    }
}