Self hosted convert sync service

About half a year ago my girlfriend needed a ebook reader for her PHD studies. Being the FOSS nerd I am, I started looking for a reader that played nicely with self-hosted services. I could not find any readers that fit the FOSS bill perfect, but ended up going for one of the readers from Onyx. It is based on Android with a Onyx skin. Later, she bought one for christmas for me to. I am pretty happy with it. Great battery time, support all document formats, can install .apk files. Onyx has also been pretty good with updating their past products.
The big thing bugging me is the solution for syncing documents to the thing. Being a pretty small company Onyx don’t want to host their own syncing server, so they rely on sending files directly from your phone to the device. This has the obvious flaw that you can’t send files to your device if it’s not on the same network, and the device has to be turned on. Pretty tedius if you find a PDF while browsing on your computer, and want to send it to your device.
I think most people use dropbox to get around this problem. Dropbox has two way sync, and works pretty good. I am not a daily Dropbox user, and don’t plan to start now. Having a couple of serves running, it feels dumb needing Dropbox for this, so I built my own. I also tried to doing it even better, with auto convert from PDF to epub. To acheive this, I also learnt some new linux tricks, pretty neat, ey?

Auto converting PDF to epub

Let me start saying that this does not work for all PDFs. Some gets wonky formatting, and with PDFs that is mostly pictures it is better staying with the orginal format.

That being said, 80% of the time, it works every time.
Calibre is a great piece of software, not only does it have a powerfull ebook managing system, it also comes with some really powerful text based scripts. One of these is ebook-convert. It is almost too simple. Install calibre on your linux host (tip: ditch the recommended extra packages if you are running on a vps, or a container) , then run

ebook-convert  

You can read about ebook-convert supported formats here.

Autorun convert

I want to convert all .pdf files that is uploaded to the PDF directory, then move the .epub file to another directory. I opted to keep the orginal file, so I later can decide what format works best on the device.
To monitor the directory, and kick of the script I discovered inotifywatch. __ A program I did not know about before trying to solve this problem. To use inotifywatch you simply point the program to the directory, use a while loop to read the file name, then run what ever script you want at it. This is the complete script.

#!/bin/bash
TARGET=/tank/booksync/fredrik/pdf/
PROCESSED=/tank/booksync/fredrik/epub/
inotifywait -m -e create -e moved_to --format "%f" $TARGET 
        | while read FILENAME
                do
    echo Detected $FILENAME, converting and copying
    ebook-convert "$TARGET/$FILENAME" "$PROCESSED/$FILENAME.epub"
                done

The two first lines define what the target and processed directories are, the rest is inotifywait kicking off ebook-convert.

Making script autorun after restart

To achieve this I used a systemd startup script. Note that I defined the user and group, you should probably change does, if you are copying it.

[Unit]
Description=Script to convert pdf to epub
DefaultDependencies=no
After=network.target
[Service]
Type=simple
User=booksync
Group=booksync
ExecStart=/tank/booksync/autoconvert-fredrik.sh
TimeoutStartSec=0
RemainAfterExit=yes
[Install]
WantedBy=default.target

Don’t forget to start and autostart it.

systemctl enable pdf-epub.service
systemctl start pdf-epub.service

Syncing files to the device

Searching for a solution for this was a rollercoaster. I used a bit of time searching for apps that could sync folders over ssh to the device. The Play Store has a lot off options, and they are mostly junk, filled with ads, or pretty expensive. I ended up searching in the F-droid app store, and found Syncopoli. Syncpoli is great, you can either sync over ssh/sftp or rsync. I opted for ssh, and set it up. I opted for one way sync, from the server to the device. This way I don’t have to worry about missclicking and deleting files from the e-reader, and having them disappear from my library. You can set up a timer for when the app sync, but I find it better to just run the app manually, when I need it. Thay way I don’t have to wake up the e-readers wifi, just to check for files.

Try out the solution for some month, and don’t forget to donate to the projects if it works for you. Donate to Calibre here, and syncopoli here.
Big thanks to calibre and syncopoli for making this possible. Also thanks for the e-reader Tea .


Posted

in

by

Tags: