Multiple Calibre Servers under Mac OS X

This is an old post!

This post is over 2 years old. Solutions referenced in this article may no longer be valid. Please consider this when utilizing any information referenced here.

So there’s this program out there called Calibre which, despite it’s pretty terrible UI, is pretty much the gold standard for managing eBooks. Seriously, it’s such a great program whose only fault is its terrible engineer UI.

One of the nice things that Calibre includes is a built-in web server that can serve books via OPDS. If you have an OPDS-compatible reader (I use Marvin), you can browse and download from your library directly on your device, basically creating your own private eBook cloud.

But, this presents a little bit of an issue. Namely, I don’t want all of my books to be publicly available, while still providing a subset of my library for visitors to browse and use. But I still want to be able to access them myself from my “private reserve collection.”

Fortunately, with a little bit of work, you can do that under Calibre.

Initial Setup

Start by creating two (or more) virtual libraries within Calibre.

One thing to note, any library other than the initial one should have no spaces in the name.

Next, set up your first server using the GUI. To do this, go to Preferences menu, then Preferences (again). In the Preferences window, find “Sharing over the net” and open that preference pane.

Note the server port (default is 8081), because you’ll need that in just a few minutes. Find the “Virtual Library to Apply” setting and select your first library. Then click “Start Server.” Wait a few seconds, then click “Test Server.” This should open a browser window and you should see a page that lets you navigate only your first library.

So far so good. On to the next step.

Multiple Servers and Libraries

Now it gets tricky (if you’re not used to the shell, that is). Open a Terminal window and type:

cd /Applications/calibre.app/Contents/MacOS/
ls

You should see something that looks like:

calibre           calibre-customize  calibre-parallel  calibre-smtp  ebook-convert  ebook-edit  ebook-polish  fetch-ebook-metadata  lrfviewer  markdown-calibre  web2disk
calibre-complete  calibre-debug      calibre-server    calibredb     ebook-device   ebook-meta  ebook-viewer  lrf2lrs               lrs2lrf    qt-plugins

The one we want here is calibre-server. Type the following command:

./calibre-server -p 8082 --virtual-library=Library2

Where Library2 is the name of the second virtual library you created above. Note that I incremented the port number from the initial server above.

Open a browser to http://localhost:8082 and you should see your second library. You can do this for as many libraries as you like. Just be sure to increment the port number for each additional server you run. Press Ctrl+C to end the server.

Launching on Reboot

So now you have this server, but you don’t want to keep a shell window open with it just to keep the server running, and you want it to start back up when you reboot.

Now, for the first library, I just have Mac OS X launching Calibre automatically, from the Login Items preferences in Users and Groups. I also use Calibre’s monitoring of a folder, so I need the actual program itself to launch at boot, so that takes care of the first library. But what about the second?

Well, this is where Mac OS X’s launchd comes in.

Back in your terminal window, type:

cd ~/Library/LaunchAgents/

Create a com.username.calibre-server.plist file, replacing username with your username. You can use vi, nano or any other editor of your choice. Put the following in the file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.username.calibre-server</string>
    <key>ProgramArguments</key>
    <array>
	    <string>/Applications/calibre.app/Contents/MacOS/calibre-server</string>
        <string>-p 8082</string>
        <string>--virtual-library=Library2</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Note that all we did here is recreate the command we ran above within the plist. Save that and exit your editor, then type:

launchctl load com.username.calibre-server.plist

And navigate to http://localhost:8082 in your browser. If everything worked, you should now see your second calibre library running. And this should load automatically after you reboot.

Bonus Setup: Pretty URLs!

Because I like pretty URLs, we’re going to set up pretty URLs for this one too. Because calibre.internal.domain is much nicer.

Mac OS X already has Apache installed, so no need to install it yourself. You just need to make a few quick changes.

cd /etc/apache2

Now, edit the httpd.conf file using vi, nano or any other editor of your choice. Be sure to use sudo because these files are owned by root. Find and uncomment the following lines:

# Find:
#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

# Change To:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

# Find:
#Include /private/etc/apache2/extra/httpd-vhosts.conf

# Change To:
Include /private/etc/apache2/extra/httpd-vhosts.conf

Close and save. Now go to:

cd /etc/apache2/extra/

Now, edit the httpd-vhosts.conf file using vi, nano or any other editor of your choice. Be sure to use sudo because these files are owned by root. Add the following vhost:

<VirtualHost *:80>
    ServerName calibre.internal.domain

    ProxyRequests Off
    ProxyPreserveHost On

    ProxyPass / http://localhost:8081/
    ProxyPassReverse / http://localhost:8081/

   <Proxy *>
        Order deny,allow
        Allow from all
        Require all granted
    </Proxy>
</VirtualHost>

You’ll recognize what we’re doing here if you read my earlier article on pretty URLs. Basically, we’re proxying requests to the actual server on the correct port.

You can optionally create a new vhost for each library you run. If you do create additional vhosts, be sure you’re pointing each one at the correct ports. Once you’re finished, type

sudo apachectl restart

And navigate to calibre.internal.domain. If everything went okay, you should now see your second library, without any of the :8081 nastiness.

Comments (0)

Interested in why you can't leave comments on my blog? Read the article about why comments are uniquely terrible and need to die. If you are still interested in commenting on this article, feel free to reach out to me directly and/or share it on social media.

Contact Me
Share It
Apache
I’ve been using Google Chrome as my primary browser for the last few months. Sorry, Firefox, but with all the stuff I need to work installed, you’re so slow as to be unusable. Up to and including having to force-quit at the end of the day. Chrome starts and stops quickly But that’s not the purpose of this entry. The purpose is how to live with self-signed SSL certificates and Google Chrome. Let’s say you have a server with a self-signed HTTP SSL certificate. Every time you hit a page, you get a nasty error message. You ignore it once and it’s fine for that browsing session. But when you restart, it’s back. Unlike Firefox, there’s no easy way to say “yes, I know what I’m doing, ignore this.” This is an oversight I wish Chromium would correct, but until they do, we have to hack our way around it. Caveat: these instructions are written for Mac OS X. PC instructions will be slightly different at PCs don’t have a keychain, and Google Chrome (unlike Firefox) uses the system keychain. So here’s how to get Google Chrome to play nicely with your self-signed SSL certificate: On your web server, copy the crt file (in my case, server.crt) over to your Macintosh. I scp'd it to my Desktop for ease of work. ** These directions has been updated. Thanks to Josh below for pointing out a slightly easier way.** In the address bar, click the little lock with the X. This will bring up a small information screen. Click the button that says “Certificate Information.” Click and drag the image to your desktop. It looks like a little certificate. Double-click it. This will bring up the Keychain Access utility. Enter your password to unlock it. Be sure you add the certificate to the System keychain, not the login keychain. Click “Always Trust,” even though this doesn’t seem to do anything. After it has been added, double-click it. You may have to authenticate again. Expand the “Trust” section. “When using this certificate,” set to “Always Trust” That’s it! Close Keychain Access and restart Chrome, and your self-signed certificate should be recognized now by the browser. This is one thing I hope Google/Chromium fixes soon as it should not be this difficult. Self-signed SSL certificates are used **a lot **in the business world, and there should be an easier way for someone who knows what they are doing to be able to ignore this error than copying certificates around and manually adding them to the system keychain.
Read More
Apple
Download I recently added a Mac mini to my setup at home, that I’m using to drive my in-home “video on demand” system. With many of the TV’s in the house on AppleTVs, any TV in the house can watch any movie in the library at any time. I put the mini (headless) in the closet, along with the Drobo and a printer. But, the new Mac mini lacks an optical drive. So, how to continue ripping the DVDs I already own? The solution, it turns out, is to continue doing the actual work on my iMac when it comes to ripping, filtering the files through iDentify and MetaX. But I don’t want to have to go to screen sharing on the Mini and add a file to iTunes. I want that to happen automatically. That’s where Automator - one of the most underrated pieces of software that comes with every Mac - comes in. With Automator, you can attach an action to a folder, so that that action will be performed whenever anything is added to that folder. So here’s what I did to get files from a folder into iTunes: Create a folder somewhere on your system. I put mine in my user directory. Open Automator. From the dialog box, select “Folder Action.” At the top, where it says “Folder Action receives files and folders added to,” select “Other” and select your new folder. Search for an action called “Set Var of Value”. Drag that action over to the right. From “Variable” select “New Variable.” Call it “Source” Search for an action called “Import Files into iTunes”. Drag that action over to the right underneath the variable action. Be sure to select “Library” from the empty dropdown. Search for an action called “Get Var of Value”. Drag that action over to the right underneath the iTunes action. Be sure the selected variable is “Source”. Search for an action called “Move Finder Items to Trash”. Drag that action over to the right. Search for an action called “Run AppleScript.” Drag that action over to the right. In the AppleScript action, paste this: on run {input, parameters} tell application "Finder" to empty trash return input end run Save the action. You’re done.
Read More
Apple
As I’ve mentioned a couple of times before, one of my projects right now is ripping all the DVDs I own so that I can watch them on my AppleTV (or any AppleTV in the house). Well, one of the problems I’ve run into a couple of times is longer movies that are distributed on two discs. This is usually movies like the Lord of the Rings Extended Edition or The Ten Commandments. Really, they’re one movie, but are distributed as two separate movies because of the restrictions of physical media. Well, digital media imposes no such restrictions on us, so why have two separate movies listed on the AppleTV? So after much trial and error, I finally discovered a way to get everything play nicely together. Unfortunately, this is not an easy problem to solve and even involved me writing a small script that could merge chapter files together because every single method I could find would eliminate chapter markers. So here, in abbreviated form, is the process for merging m4v files together and preserving chapter markers. Note: This tutorial assumes some level of technical proficiency. This is not a point-and-click process (yet :P) and requires the use of multiple tools and the shell. Tools you’ll need: Handbrake or whatever tool you’re using for ripping your legally obtained DVDs. MetaX and/or iDentify Subler remux Quicktime, which is now built into Mac OS X. chaptermerge, a script I wrote that merges chapter files together. The proces: Rip both movies from their individual DVDs using Handbrake or whatever other tool you’re using. Be sure that you’re adding chapter markers. Load each movie into MetaX and download the chapter names. That’s really the only thing you need to add to the file. Save the files with chapter names. Load each movie into Subler and extract the chapter files. To do this, select the chapter track and select File -> Export. Now, open the first movie in Quicktime. Drag the second movie on top of the first one. Quicktime will add the two together. Save the movie for use on an AppleTV. Get a beer or 6, because this takes awhile. While the movie is saving, use chaptermerge to merge the chapter files together. See the docs on how it works. Once the file has finished saving as a Quicktime MOV (it’s actually still h.264 inside the file), fire up remux and convert the merged file back into an m4v. Drag the file into remux, set the output to m4v, and save. Should be pretty quick - a matter of minutes. Load the merged file back into Subler and add the merged chapter track. Drag the chapter file into the Subler window. Save the file. Load the merged file into a tool such as iDentify or MetaX and add the remaining metadata. That’s it! You now have a merged file with both parts of the movie, accurate chapter markers and full metadata, ready to be copied to iTunes and viewed on your AppleTV.
Read More