Last active
December 27, 2015 11:09
-
-
Save ajslaghu/7316855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// written by AJ Slaghuis 2013 | |
define('DS', DIRECTORY_SEPARATOR); | |
$basepath = './program_info'; | |
$fields = array(); | |
$fields["prid"] = "prid"; | |
$data = array(); | |
function validate_end($file) { | |
$arr = array(");\n//e", ");\n//ep",");\n//epc"); | |
foreach ($arr as $str) { | |
if (substr($file, - strlen($str) ) == $str){ | |
return strlen($str); | |
} | |
} | |
return -1; | |
} | |
function validate_begin($file) { | |
$str = 'parseMetadata('; | |
if (substr($file, 0, strlen($str)) != $str) { | |
return -1; | |
} | |
return strlen($str); | |
} | |
@unlink('output.csv'); | |
$entries = scandir($basepath); //sorted alphabetically | |
foreach ($entries as $entry) { | |
if ($entry == '.' || $entry == '..' || is_dir($entry) || substr($entry, -5) != '.json') | |
continue; // | |
$prid = substr($entry, 0, -5); | |
if ($prid == null) | |
continue; | |
$file = file_get_contents($basepath . DS . $entry); | |
$end_trim = validate_end($file); | |
$begin_trim = validate_begin($file); | |
if ($end_trim < 1 || $begin_trim < 1) { | |
if (strcmp($file, "parseMetadata({\"STATUS\":\"ERROR\",\"VERSION\":\"1.9.3\",\"code\":440,\"error\":\"De opgevraagde aflevering is niet gevonden.\"});") != 0) { | |
echo("$entry FILE FORMAT ERROR \n"); | |
echo($file . "\n\n"); | |
die(-1); | |
}// niet gevonden | |
continue; | |
} | |
$fields["prid"] = "prid"; | |
$data[$prid]["prid"] = $prid; | |
$file_sub = substr((substr($file, $begin_trim)), 0, - ( $end_trim )); | |
// if (strlen($file_sub) > 100) | |
// continue; | |
// echo $file; | |
// die(); | |
$json = json_decode($file_sub); | |
foreach ($json as $key => $value) { | |
if ($key == 'images' || $key == 'pubopties' || $key == 'sitestat') | |
continue; | |
if (is_object($value)) { | |
foreach ($value as $subkey => $subvalue) { | |
$fields["$key" . "_" . "$subkey"] = "$key" . "_" . "$subkey"; | |
$data[$prid]["$key" . "_" . "$subkey"] = str_replace(array("\n", "\r", "\t"), '', html_entity_decode($subvalue) | |
); | |
} | |
} else if (is_array($value)) { | |
for ($i = 0; $i < count($value); $i++) { | |
foreach ($value[$i] as $vkey => $vvalue) { | |
$fields[$key . "_" . $vkey . "_" . $vvalue] | |
= $key . "_" . $vkey . "_" . $vvalue; | |
$data[$prid][$key . "_" . $vkey . "_" . $vvalue] | |
= str_replace(array("\n", "\r", "\t"), '' | |
, html_entity_decode($vvalue)); | |
} | |
} | |
} else {// key value pair | |
$fields[$key] = $key; | |
$data[$prid][$key] = str_replace(array("\n", "\r", "\t") | |
, '', html_entity_decode($value)); | |
} | |
} | |
} | |
$outputheader = ""; | |
$outputheader .= implode("\t ", $fields); | |
$outputheader.= "\n"; | |
file_put_contents('output.csv', $outputheader); | |
foreach ($data as $item) { | |
$outputrow = ''; | |
foreach ($fields as $field) { | |
if (isset($item[$field])) { | |
$outputrow .= $item[$field]; | |
} else { | |
$outputrow .= ""; //\"\""; | |
} | |
$outputrow .= "\t "; | |
} | |
$outputrow = substr($outputrow, 0, -2); | |
$outputrow .= "\n"; | |
file_put_contents('output.csv', $outputrow, FILE_APPEND | LOCK_EX); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment