Skip to content

Instantly share code, notes, and snippets.

@zbycz
Created June 23, 2018 09:57
Show Gist options
  • Save zbycz/7e3eed67cdf0464f0c8f26dddf25de86 to your computer and use it in GitHub Desktop.
Save zbycz/7e3eed67cdf0464f0c8f26dddf25de86 to your computer and use it in GitHub Desktop.
Transforms all Facebook Messanger to CSV meta file for further analysis
<?php
// Open https://www.facebook.com/your_information/ and click "Download"
// then select JSON format (and optionaly low quality media) and hit "Create file"
$zip = zip_open("facebook-messages.zip");
$fw = fopen("messages.csv", "w");
if (is_resource($zip)) {
while ($zip_entry = zip_read($zip)) {
$filename = zip_entry_name($zip_entry);
if (substr($filename, -5) != ".json") continue;
echo "$filename \n";
if (zip_entry_open($zip, $zip_entry)) {
$content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
$json = json_decode($content);
foreach ($json->messages as $m) {
$out = array(
"thread" => $filename,
"sender" => isset($m->sender_name) ? ($m->sender_name) : "",
"timestamp" => date("Y-m-d H:i:s", $m->timestamp),
"date" => date("Y-m-d", $m->timestamp),
"hour" => date("H", $m->timestamp),
"sent" => 1,
"text_length" => isset($m->content) ? strlen($m->content) : 1,
);
fwrite($fw, join(",", $out) . "\n");
}
}
}
zip_close($zip);
}
fclose($fw);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment