View Single Post
Old 12-04-2012, 12:19 PM   #216
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
Custom Sync - working, but clunky

Okay, I've got a successful custom sync working, at least as a proof-of-concept version. It currently requires you to have access to your own web server, running PHP (I'm running this on my own Linux desktop) - this is because we need to generate a list of file URLS for the script on the Kobo to retrieve. If anyone knows how to e.g. get the built-in Calibre HTTP server to generate a plain text file of new files in the last X days, that would be even better. This has been tested only on the Kobo Glo with firmware 2.1.5.

Here's how it works:

Add to .kobo/koboplugins.ini:
Code:
CSync=Plugin::connectWifi()>>Plugin::executeSystemCmd("/mnt/onboard/run.sh &")
Note that this seems to force this entry in the menu to appear as "WiFi" - it looks like there's a bug in the plugin that forces the label to be "WiFi" whenever it's used, in a command sequence like this, or on its own. If you already have a WiFi entry in your menu, remember they appear alphabetically, so make sure you select the correct one.

Now create the following scripts in the Kobo root folder (i.e. the root folder when you access it as a USB drive):

run.sh
Code:
#!/bin/sh
touch /mnt/onboard/started_sync
nohup /mnt/onboard/sync.sh &
(I use this intermediate file to call the actual sync script, because if I try to call it directly, even with Plugin::executeSystemCmd("nohup sync.sh &"), the menu seems to freeze until the whole thing finishes.)


sync.sh
Code:
#!/bin/sh

# Add your HTTP server details here
LIST_URL="http://192.168.0.123/books/filelist.php";
PING_IP="192.168.0.123"

ATTEMPTS=30

#Uncomment the next line to always download files in the list (i.e. ignore 
existing downloaded file, save with unique name)
# NOTE: for testing only! Will create duplicate books on your device
#ALWAYS_DL=1

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

   
successes=0
LIST=`/usr/bin/wget -q -O - $LIST_URL`
IFS=$'\n'  # make the for loop break up the list on newlines

for url in $LIST; do
  filename=`echo $url|sed -e 's|.*/||g'`
  if [ $ALWAYS_DL ] ; then
    timestamp=`date +%Y%m%d-%k%M%S`
    if /usr/bin/wget "$url" -O "/mnt/onboard/$timestamp-$filename" >>/mnt/onboard/wget.log 2>&1 ; then
      successes=$(( $successes + 1 ))                                                                                                                 
      echo "Downloaded (forced) $url to $timestamp-$filename" >>/mnt/onboard/sync.log
    else
      timestamp=`date`                                                                                                                                
      echo "Failed to force download $url ($timestamp)" >>/mnt/onboard/sync.log                                                                
    fi
  else
    if /usr/bin/wget "$url" -O "/mnt/onboard/$filename"  >>/mnt/onboard/wget.log 2>&1 ; then 
      successes=$(( $successes + 1 ))
      echo "Downloaded $url" >>/mnt/onboard/sync.log
    else
      timestamp=`date`
      echo "Failed to download $url (file exists? $timestamp)" >>/mnt/onboard/sync.log
    fi
  fi
done

if [ "$successes" -gt "0" ] ; then 
  timestamp=`date`
  echo "Downloaded $successes files ($timestamp)" >>/mnt/onboard/sync.log
  nohup /mnt/onboard/reindex.sh &
fi
(You will need to put the correct address for your HTTP server at the appropriate place at the top of the script. Note that this script first checks that it can reach the server on the network. This is because the command sequence we defined in the menu turns on the Wifi but does not wait for the connection to be established before running the next command, i.e. this sync.sh script. So the script itself must keep checking and waiting until the network is up. It will give up after 30 attempts, about 60 seconds.

reindex.sh
Code:
#!/bin/sh
echo usb plug add >> /tmp/nickel-hardware-status 
sleep 10 
echo usb plug remove >> /tmp/nickel-hardware-status
This forces the Kobo to add any files downloaded to the Library by faking a USB connection and disconnection (found here: http://a3nm.net/blog/fnacbook_kobo_more_hacking.html). It works but requires you to tap 'connect' on the dialog it brings up on the screen, and then automatically disconnects and indexes a few seconds later. It works, but if anyone knows how to trigger a reindex in a less clunky and interactive way, please let me know.

These files all need to be set as executable, i.e. "chmod 755".

Here is the PHP script which will create the expected list of file URLs to download; the epub files must in the same directory as the script.
Code:
<?php
$path = substr($_SERVER[PHP_SELF], 0, strrpos($_SERVER[PHP_SELF], '/'));
$find = 'find . -iname \\*.epub'; //-mtime 0 ';

$ar = array();
exec($find, $ar);

foreach($ar as $file){
 $url = "http://" . $_SERVER['SERVER_NAME']  . $path . '/' . str_replace(' ', '%20', substr($file, 2));
 print "$url\n";
 //print "<a href=\"$url\">$url</a><br>";

}
?>
When you test this PHP script in your browser you should get a list of Epub file URLS, separated by newlines (they will all appear to be on the same line in your browser). If you don't see them, make sure you have some files in the same directory as the script, and check that they're accessible - uncomment the second "print" statement at the end to test that the links work.

Whew. Ok, once all that is in place, you should be able to automatically download files to your Kobo and have them automatically indexed (after the fake USB-connect sequence). The script will not re-download files that it has already saved to the Kobo unless you uncomment the ALWAYS_DL line in sync.sh, but note that this creates unique filenames each time it downloads a file, so you'll get multiple versions of the same book in your library.

The script does some logging to wget.log and sync.log in the same root directory as the scripts themselves.

TODO:

- Less interactive way to force checking/reindexing of new files. Anyone know if this can be done via a shell command?

- Neater way to make sure wifi is available before doing download

- Fix label bug that causes menu item to appear as "WiFi"

- Ideally it would be good to either serve the files from the Calibre server, if it can be made to generate a simple enough list of URLs of new files. Alternately, it would be good to be able to remotely host the files so they can be synced even when your own computer is off, i.e. Dropbox or similar; however with Dropbox there's no way to generate the list of file URLs, and no way to list the contents of a web folder to get all the files. If anyone can think of a generic alternative that would work in this case, that would be good.

- Text updates at the top of screen - currently it gets stuck on "Connecting", I think this is the Wifi command. Not sure how to change the this text.

- A way to extract emailed articles and save them as epub files, so they can be downloaded using this process, i.e. make use of browser extensions that allow you to "email this page to your kindle"... any ideas?


Attached is a zip file with the above scripts that need to be extracted into the root folder, and the PHP script for your server. The Kobo shell scripts will probably still need to have the correct execute permissions set - this will be hard if you haven't enabled telnet for the device.

Let me know how this works if you try it out.
Attached Files
File Type: zip kobo-sync-scripts.zip (1.7 KB, 243 views)

Last edited by textchimp; 12-04-2012 at 08:30 PM.
textchimp is offline   Reply With Quote