Blog/jokoak_euskaraz/translate.php
(Deskargatu)
<?php
if(count($argv)<2 || !file_exists($argv[1]))
{
die($argv[0]." : Missing filename.txt argument.\n");
}
$newFile = preg_replace("/\.[^\.]+$/","",$argv[1])."-euskaraz.csv";
$newFile_fp = fopen($newFile, 'w');
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;
}
}
$newCsv=[];
if (($handle = fopen($argv[1], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = system('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($data[1]).',"model":"generic_es2eu"}\' | jq -r ".translation"');
print "OK ".json_encode($data)."\n";
fputs($newFile_fp, str_putcsv($data)."\n");
}
fclose($handle);
}