01 #!/usr/local/bin/perl -w 02 use strict; 03 use local::lib qw(/home/mschilli/perl5); 04 use Log::Log4perl qw(:easy); 05 use POSIX; 06 use DBI; 07 08 my $run_as = "mschilli"; 09 my $clip_path = 10 "/media/Kindle/documents/My Clippings.txt"; 11 12 BEGIN { 13 use FindBin qw($RealBin); 14 chdir $RealBin; 15 } 16 use ClippingsParser; 17 18 my $logfile = "/var/log/kindle.log"; 19 20 Log::Log4perl->easy_init({ 21 file => ">>$logfile", 22 level => $DEBUG, 23 }); 24 25 my ( $name, $passwd, $uid, $gid ) = 26 getpwnam( $run_as); 27 28 chown $uid, $gid, $logfile or 29 LOGDIE "Cannot chown $logfile: $!"; 30 chmod 0644, $logfile or 31 LOGDIE "Cannot chmod $logfile: $!"; 32 33 POSIX::setuid( $uid ); 34 35 my $pid = fork(); 36 die "fork failed" if !defined $pid; 37 exit 0 if $pid; # parent 38 39 # wait until kindle root dir is mounted 40 for( 1..10) { 41 if( -f $clip_path) { 42 last; 43 } else { 44 DEBUG "Waiting for $clip_path"; 45 sleep 5; 46 next; 47 } 48 } 49 50 LOGDIE "$clip_path not found" 51 if !-f $clip_path; 52 53 my $dbh = DBI->connect( 54 "dbi:SQLite:highlights.sqlite", "", "", 55 { RaiseError => 1, PrintError => 0 } 56 ); 57 58 open my $fh, "<", $clip_path or 59 LOGDIE "Cannot open $clip_path ($!)"; 60 61 my $cp = ClippingsParser->new(); 62 63 my $items_added = 0; 64 65 $cp->parse_fh( $fh, sub { 66 my( $type, $loc, $author, $title, 67 $when, $text ) = @_; 68 69 my $sth = $dbh->prepare( 70 "INSERT INTO seen VALUES (?, ?, ?)"); 71 72 eval { 73 $sth->execute( $type, $loc, $title ); 74 }; 75 76 return if $@; # most likely a dupe 77 78 $sth = $dbh->prepare( "INSERT INTO " . 79 "highlights VALUES (?, ?, ?, ?, ?, ?)"); 80 $sth->execute( $type, $loc, $author, 81 $title, $when, $text ); 82 $items_added++; 83 } ); 84 85 INFO "$items_added items added"; 86 close $fh;