View Single Post
Old 12-04-2012, 12:19 PM   #216
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:
CSync=Plugin::connectWifi()>>Plugin::executeSystemCmd("/mnt/onboard/ &")
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):
touch /mnt/onboard/started_sync
nohup /mnt/onboard/ &
(I use this intermediate file to call the actual sync script, because if I try to call it directly, even with Plugin::executeSystemCmd("nohup &"), the menu seems to freeze until the whole thing finishes.)

# Add your HTTP server details here


#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

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

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
      echo "Failed to force download $url ($timestamp)" >>/mnt/onboard/sync.log                                                                
    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
      echo "Failed to download $url (file exists? $timestamp)" >>/mnt/onboard/sync.log

if [ "$successes" -gt "0" ] ; then 
  echo "Downloaded $successes files ($timestamp)" >>/mnt/onboard/sync.log
  nohup /mnt/onboard/ &
(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 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.
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: 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.
$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, 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.


- 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 (1.7 KB, 114 views)

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