View Single Post
Old 04-09-2013, 08:27 AM   #1
sws
Enthusiast
sws began at the beginning.
 
sws's Avatar
 
Posts: 40
Karma: 10
Join Date: Oct 2012
Device: Pocket Book Touch HD3
[How To] Running Calibre server as a news server

Hi,

I have been trying to setup calibre as a stand alone server using the command line only (without the gui). It is to fetch newspapers on a daily basis, tagging them correctly and deleting newspapers which are older than 2 days. Here's my approach.

First I have to get news into calibre server database. Therefore I wrote a simple bash script that is run once a day in the morning as a cron job; for example "The Guardian":

Code:
#!/bin/sh
# The Guardian
cd <PATH whereever you store your calibre scripts>
rm guardian.epub
ebook-convert ./PATH_TO_CALIBRE_RECIPES/calibre-recipes/guardian.recipe guardian.epub --output-profile=kobo &&
ebook-meta guardian.epub --tag dailynews&&
calibredb add guardian.epub
First I remove the old epub file. Then I grap the new epub, in my case using output-profile for a Kobo Touch reader. Just change this to match your device. Then the new epub gets tagged for later retrieval. Finally the epub gets registrated into calibre database.

I can now use the web interface of the server to fetch my daily newspaper. I could also mail the newspaper to the device of my linking. Just put the following snippet at the end of the bash script above:

Code:
calibre-smtp -r <email server domain> -u <username email address> -p <secret_password> --port 25  <server email address> <recipient email address> 'Good morning! Here's your new Guardian. Have fun!' -a guardian.epub -s 'Your new Guardian'
To prevent the calibre server to overflood with old newspapers I delete all newspapers older than two days. Therefore I wrote a perl script:

Code:
#!/usr/bin/perl -w
#
##################
# file: cal_del_old_dailynews.pl
# author: Sebastian Singer
# version: 1.1
# disclaimer: No warranty whatsoever!
#
# What this script does:
# This script deletes daily newspapers from Calibre Server
# which are older than two days. Putting this script in crontab
# automates the process.
#
# How this script works:
# 1. Generate a list of newspapers older than two days
# 2. Extract the epub id's from this list
# 3. Hand over the id's to calibredb for removal
#
# Notes:
# For the script expected to work you have to tag the epubs. So if
# you fetch your daily newspaper for your calibre server on the command line
# don't forget tagging it. Otherwise you won't find it afterwards.
# Here is a possible way (a bash script) to fetch "The Guardian"
# on a daily basis. This script can also be put into crontab:
#
#  #!/bin/sh
#  rm guardian.epub
#  ebook-convert ../PATH_TO/calibre-recipes/guardian.recipe guardian.epub --output-profile=kobo &&
#  ebook-meta guardian.epub --tag dailynews &&
#  calibredb add guardian.epub
#
######################
#
# 1. Generate a list of newspapers older than two days
open(CALINDEX,">","old_dailynews.txt");
my @get_list = (" CALIBRE_OVERRIDE_LANG=en calibredb list --search 'date:<1daysago and tags:dailynews' > old_dailynews.txt");
my @get_list_exec = system(@get_list); 
close (CALINDEX);

# 2. Extract the epub id's from this list
# 2.1. extract id's:
open(CALINDEX,"old_dailynews.txt");
my $file = CALINDEX;
my @cal_index; 
foreach my $line ( <$file> ) {
	if ( $line =~ /^\d/) {
        push @cal_index, substr $line,0,4;
	}	
}
chomp @cal_index;

# 2.2 substitue empty spaces by commas:
open(DELINDEX,">","del_cal_index.txt");
print DELINDEX @cal_index;
close(DELINDEX);
open(DELINDEX,"del_cal_index.txt");
my $cal_ids = <DELINDEX>;
$cal_ids =~ s/\s/,/g;
# get rid of the last trailing comma:
$cal_ids =~ s/,$//;
chomp $cal_ids;
# print $cal_ids; # uncomment just for checking 
close (DELINDEX);
close(CALINDEX);

# 3. Hand over the id's to calibredb for removal
system("/usr/bin/calibredb","remove",$cal_ids) == 0
	or die "system failed: $?";
This perl script can be run daily via cron job again.

I am aware that calibre is written in python and so it would be suitable to write a python script instead of using perl. But on the one hand I don't understand python very well (sorry?!). So I better stuck with a language I had been working with before. On the other hand I am far from being a perl or bash programmer. So please don't mind the simple structure of the scripts.

I hope that someone will find this "cli approach" helpful. Hints to improve this way or to correct errors are always welcome.

Cheers,
Sebastian
sws is offline   Reply With Quote