View Single Post
Old 08-14-2025, 02:41 PM   #1
sup
Zealot
sup began at the beginning.
 
Posts: 106
Karma: 10
Join Date: Sep 2013
Device: Kindle Paperwhite (2012)
How to download news automatically even when you do not keep Calibre always running

The scheduled news downloads only really works when you keep Calibre and your computer running all the time. I do not so until now I would always have to trigger the downloads manually. No more!

This guide assumes you are running linux and systemd. It downloads The Economist, adjust for your recipes.

1) First in calibre, go to Preferences>Sharing>Sharing over the net and
a) check "Run server automatically when calibre starts"
b) in Advanced tab, check "Allow un-authenticated local connections to make changes". Not that this allows any local programs to delete your library over HTTPS (they are allowed to do that already by running something like "rm ~/Library", so not a security hole).

This will ensure a calibre web server is running whenever calibre is running. Calibre does not allow books to be added to it via command line when it is running, unless it is done through the server. This protects the database from corruption.

2) In ~/.config/systemd/user/, create "download_economist.timer"

Code:
[Unit]
Description="Download the economist once a week"

[Timer]
OnCalendar=Thu 18:00:00
Persistent=true

[Install]
WantedBy=timers.target
This will cause the download to occur every week on Thursday at 6 PM. Adjust for your timezone. When your computer is off at that time, it will automatically run when you turn it off.

3) In ~/.config/systemd/user/, create "download_economist.service" (the name before ".service" must be the same as for the timer in step 2))
Code:
[Unit]
Description=Script that downloads the economist into Calibre 

[Service]
Type=oneshot
ExecStart=~/.local/bin/download_economist.sh
Restart=on-failure
RestartSec=900
This will run "~/.local/bin/download_economist.sh" and when there is a failure (economist servers are sometimes overloaded, or if you were offline), it will try again in fifteen minutes until it suceeds (or you turn off the computer, in which case it will not download, but that is unlikely in my setup).

4) The script to download ~/.local/bin/download_economist.sh
Code:
#!/bin/bash
TMP_FILE=/tmp/economist$(date +%Y-%m-%d).kepub.epub
echo $TMP_FILE
if ebook-convert "The Economist".recipe $TMP_FILE --output-profile kobo ; then
        if pgrep -f "/usr/bin/python3 /usr/bin/calibre" >/dev/null; then
                calibredb add $TMP_FILE  --with-library http://127.0.0.1:8080#E-knihy --authors "The Economist"
        else
        calibredb add $TMP_FILE --authors "The Economist"
        fi
        rm $TMP_FILE 
else
    exit 1
fi
This tries to download the news, when it suceeds, checks whether calibre is running. If yes, it uses the server, if not, it adds to the database directly (I have only one database, so I do not specify it). When it does not suceed, it emits an error telling systemd to start again in 15 minutes as per step 3.

If you want another recipe, list them with "ebook-convert --list-recipes", the ebook-convert command needs them to be in the form listed by this command.

It changes the author field to "The Economist". I love calibre, but I just do not understand why it insists it is the author of the news when it is not.

My Library is called E-knihy. Find out what is the name of yours by "calibredb add --with-library http://127.0.0.1:8080#-"

Do not forget to make the file executable (chmod +x).

5) Enable and start everything:
Code:
systemctl --user enable --now download_economist.timer
"systemctl --user list-timers" should now list your unit and tell you how long before it is run. "journalctl --user -u download_economist.service" is useful for debugging as it will show output of your command.

Last edited by sup; Yesterday at 05:06 AM. Reason: add that it will run even when the computer is off at the given time
sup is offline   Reply With Quote