View Single Post
Old 12-06-2012, 04:24 PM   #222
textchimp
Member
textchimp has a complete set of Star Wars action figures.textchimp has a complete set of Star Wars action figures.textchimp has a complete set of Star Wars action figures.
 
Posts: 15
Karma: 250
Join Date: Nov 2012
Device: Kobo Glo
Sending highlights/annotations to remote server

Ha, ok yes, wget again. I've got this to work, again as a proof-of-concept, but the version of wget on the Kobo is limited - it does not have the --post-file=FILE command line option, but it does have (undocumented) the --post-data="DATA" option. This means you can send some post data, but only via the command line, and it seems to limit how much data you can send. (Anyone got a better idea?)

Anyway, here's a working example to grab all the annotation data from /mnt/onboard/Digital\ Editions/Annotations and post it to a PHP script on a server.

First create a menu entry in koboplugins.php like:

Code:
CSync=Plugin::connectWifi()>>Plugin::executeSystemCmd("/mnt/onboard/run-notesync.sh &")
Then as with the sync script above, we use an intermediate script to prevent the menu freezing up:

run-notesync.sh:
Code:
#!/bin/sh
touch /mnt/onboard/started_notesync
nohup /mnt/onboard/notesync.sh &
notesync.sh:
Code:
#!/bin/sh

ATTEMPTS=40

# change the addresses below for your server
REMOTE_URL="http://serveraddress/path-to/post.php"
PING_ADDRESS="serveraddress"
POSTDATA=""

# wait for wifi network to come online; give up after $ATTEMPTS tries
tries=1
while ! ping -c 1 -q $PING_ADDRESS; do
   if [ "$tries" -gt "$ATTEMPTS" ] ; then
      timestamp=`date`
      echo "sync: aborted after $ATTEMPTS connection attempts ($timestamp)" >>/mnt/onboard/notesync.log
      exit; 
   fi
   sleep 2
   tries=$(( $tries + 1 ))
done


POSTDATA=""
FILES=`find "/mnt/onboard/Digital Editions/Annotations/" -iname \*.annot -exec grep -l '<text>' {} \;`
IFS=$'\n'
for file in $FILES; do
  POSTDATA=${POSTDATA}`awk '/<dc:title>/ ||  /<dc:creator>/ || /<text>/ || /<\/text>/' "$file"`
  POSTDATA="${POSTDATA}\n\n"
done

#echo -e "$POSTDATA"

wget $REMOTE_URL -O /mnt/onboard/notesync.log --post-data="content=$POSTDATA"
timestamp=`date`
echo -e "\n\nSent on $timestamp \n" >>/mnt/onboard/notesync.log
This will extract just the title, creator and text tags from all the annotation data, in the interest of saving upload size via the limited wget command line option. As with the download script in my sync script post, it will first test the network connection until it successfully pings the remote host, otherwise it will quit.

(NOTE: it looks like the way the notes are saved in these .annot files ignores paragraph breaks - it just runs text from different paragraphs together. Annoying! In any case, the awk filtering done in the script will ignore lines without a <text> or </text> tag; needs a better multiline regex...)

On the server end, you need a simple PHP script like this:
post.php
Code:
<?php
if(isset($_POST['content'])) {
  file_put_contents('notes.html', $_POST['content']);
  exit;
}
?>
Then you can see your notes at http://server-address/path-to/notes.html.

Last edited by textchimp; 12-06-2012 at 09:25 PM.
textchimp is offline   Reply With Quote