Skip to content

Instantly share code, notes, and snippets.

@p0358
Last active October 23, 2018 23:24
Show Gist options
  • Save p0358/384cb294997e83884573633f77b145a2 to your computer and use it in GitHub Desktop.
Save p0358/384cb294997e83884573633f77b145a2 to your computer and use it in GitHub Desktop.
Very simple stats about Messenger conversation based on JSON data export from Facebook
<?php
// https://github.com/p0358
// Example output: https://imgur.com/UzAIAMH
function intformat($number) { return number_format($number, 0, '.', ' '); }
if (sizeof($argv) == 1) {
echo <<<'USAGE'
Usage:
1. Download your Facebook data from https://web.facebook.com/dyi/
a. Click "Download Your information"
b. Select JSON data format
c. Only select "Messages" below since we don't need anything else for this
d. Select media quality on low, because we don't need them either
e. Request download
2. Once complete and downloaded, enter the archive, open "messages", your desired thread and extract "message.json"
3. Open "message.json" with this program/script
USAGE;
passthru('pause');
}
if (!empty($argv[1])) {
$filename = $argv[1];
echo "Loading file...\n";
$file = file_get_contents($filename);
if (empty($file)) {
echo "Non-existent or empty file: $filename\n\n";
passthru('pause');
exit;
}
echo "Parsing JSON...\n";
$array = @json_decode($file, true);
unset($file);
if (empty($array)) {
echo "Invalid JSON in file: $filename\n\n";
passthru('pause');
exit;
}
if (isset($array['messages']) && isset($array['title']) && isset($array['is_still_participant']) && isset($array['participants']) && isset($array['thread_type'])) {
echo "\n== Thread info ==\n\n";
echo "Title: ${array['title']}\n";
if (sizeof($array['participants'])>1 || @$array['participants'][0] != $array['title'])
echo "Participants: ".implode(', ', $array['participants'])."\n";
if (sizeof($array['participants'])>1 || !$array['is_still_participant'])
echo "Is still participant: " . ($array['is_still_participant']?'yes':'no') . "\n";
echo "Thread type: ${array['thread_type']}\n";
echo "\n";
echo "Total messages: " . intformat(sizeof($array['messages'])) . "\n";
echo "\n== Details ==\n";
$messages_count_by_type = [];
$messages_count_by_sender = [];
$lowest_timestamp = PHP_INT_MAX;
$highest_timestamp = 0;
foreach ($array['messages'] as &$message) {
@$messages_count_by_type[$message['type']]++;
@$messages_count_by_sender[$message['sender_name']]++;
@$words_by_sender[$message['sender_name']] += str_word_count($message['content']);
if ($message['timestamp'] < $lowest_timestamp) $lowest_timestamp = $message['timestamp'];
if ($message['timestamp'] > $highest_timestamp) $highest_timestamp = $message['timestamp'];
}
echo "\nMessage types:\n";
foreach ($messages_count_by_type as $type => $count) {
$count = intformat($count);
echo "- $type => $count\n";
}
echo "\nMessages by sender:\n";
foreach ($messages_count_by_sender as $sender => $count) {
$count = intformat($count);
echo "- $sender => $count\n";
}
echo "\nWords by sender:\n";
foreach ($words_by_sender as $sender => $count) {
$count = intformat($count);
echo "- $sender => $count\n";
}
echo "\nMessages data range:\n";
echo "oldest: " . date('Y-m-d H:i:s', $lowest_timestamp) . "\n";
echo "newest: " . date('Y-m-d H:i:s', $highest_timestamp) . "\n";
passthru('pause');
exit;
} else {
echo "Invalid JSON file contents: $filename\nMake sure you have provided correct \"message.json\" file from Facebook data export.\n\n";
passthru('pause');
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment