Skip to content

Instantly share code, notes, and snippets.

@avinash
Created May 23, 2017 05:19
Show Gist options
  • Save avinash/9a0d38bc3d7fdb3dcb42f7c6ba826da2 to your computer and use it in GitHub Desktop.
Save avinash/9a0d38bc3d7fdb3dcb42f7c6ba826da2 to your computer and use it in GitHub Desktop.
kindle "My Clippings.txt" splitter
#!/usr/bin/php
<?php
if (empty($argv[2])) {
throw new Exception('usage: <this file> "/path/to/My Clippings.txt" /path/to/ouputfiles');
}
$myClippingsFile = $argv[1];
$splitDir = $argv[2];
$bookAndNotes = array();
// read lines
$notes = explode("==========", file_get_contents($myClippingsFile));
if (empty($notes)) {
throw new Exception("empty or wrong file specified");
}
foreach ($notes as $note) {
// split title and text
$lines = array_slice(explode("\n", $note), 1);
$title = array_shift($lines);
$title = preg_replace('/([^a-z0-9- ])/i', '', $title);
$text = implode("\n", $lines);
// append to file name if the same note does not exist already
$fileName = __DIR__ . "/notes/notes_$title";
if ($text && strpos(@file_get_contents($fileName), $text) === false) {
file_put_contents($fileName, $text, FILE_APPEND);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment