Skip to content

Commit

Permalink
Add a filter to allow specific conversations to be targeted.
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
cfinke committed Dec 12, 2022
1 parent dceb388 commit b3a2b06
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ $ messages-exporter.php [-o|--output_directory output_directory]
Optionally, supply a timezone to use for any dates and times that are displayed. If none is supplied, times will be in UTC. For a list of valid timezones, see https://www.php.net/manual/en/timezones.php
[-p|--path-template "%Y-%m-%d - _CHAT_TITLE_"]
Optionally, supply a strftime-style format string to use for the exported chat files. **Use _CHAT_TITLE_ for the name of the chat.** For example, you can separate your chats into yearly files by using `--path-template "%Y - _CHAT_TITLE_"` or monthly files by using `--path-template "%Y-%m - _CHAT_TITLE_"`. You may also wish to use the date as a suffix so that chats from the same person are all organized together in Finder, in which case you might use `--path-template "_CHAT_TITLE_ - %Y-%m-%d"`
[--match "Conversation Title"]
Limit the output to conversations that include this argument somewhere in their title.
[--match_regex "/^Conversation Title$/"]
Limit the output to conversations whose titles match this regular expression.
```

Caveats
Expand Down
51 changes: 39 additions & 12 deletions messages-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
define( 'VERSION', 2 );

$options = getopt(
"o:fhrd:t:p:",
array(
"output_directory:",
"flush",
"help",
"rebuild",
"database:",
"date-start:",
"date-stop:",
"timezone:",
"path-template:",
)
"o:fhrd:t:p:",
array(
"output_directory:",
"flush",
"help",
"rebuild",
"database:",
"date-start:",
"date-stop:",
"timezone:",
"path-template:",
"match:",
"match_regex:",
)
);

if ( isset( $options['h'] ) || isset( $options['help'] ) ) {
Expand Down Expand Up @@ -68,6 +70,14 @@
. " [-p|--path-template \"%Y-%m-%d - _CHAT_TITLE_\"]\n"
. " Optionally, supply a strftime-style format string to use for the exported chat files. **Use _CHAT_TITLE_ for the name of the chat.** For example, you can separate your chats into yearly files by using `--path-template \"%Y - _CHAT_TITLE_\"` or monthly files by using `--path-template \"%Y-%m - _CHAT_TITLE_\"`. You may also wish to use the date as a suffix so that chats from the same person are all organized together in Finder, in which case you might use `--path-template \"_CHAT_TITLE_ - %Y-%m-%d\"`"
. "\n"

. " [--match \"Conversation Title\"]\n"
. " Limit the output to conversations that include this argument somewhere in their title. For example, to only back up chats with your friend Alex Smith, you'd specify `--match \"Alex Smith\"`."
. "\n"

. " [--match_regex \"/^Conversation Title$/\"]\n"
. " Limit the output to conversations whose titles match this regular expression. For example, to only back up one-on-one chats with your friend Alex Smith, you'd specify `--match \"/^Alex Smith$/\"`."
. "\n"

. "";
echo "\n";
Expand Down Expand Up @@ -113,6 +123,10 @@
$options['p'] = '_CHAT_TITLE_';
}

if ( isset( $options['m'] ) ) {
$options['match'] = $options['m'];
}

# Ensure a trailing slash on the output directory.
$options['o'] = rtrim( $options['o'], '/' ) . '/';

Expand Down Expand Up @@ -231,6 +245,19 @@
$chat_title = $contactNumber;
}

if ( isset( $options['match'] ) ) {
if ( stripos( $chat_title, $options['match'] ) === false ) {
continue;
}
}

if ( isset( $options['match_regex'] ) ) {
if ( ! preg_match( $options['match_regex'], $chat_title ) ) {
continue;
}
}


$statement = $db->prepare(
"SELECT
*,
Expand Down

0 comments on commit b3a2b06

Please sign in to comment.