Last active
April 23, 2016 19:02
-
-
Save gburca/cb261572bf0c7c4f874ae638fd99d1f8 to your computer and use it in GitHub Desktop.
Import Tellico data into Calibre
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
The following script will import Tellico book data into Calibre. | |
For more details, see: | |
http://ebixio.com/blog/2016/04/23/import-tellico-books-into-calibre/ | |
""" | |
import os, sys | |
import subprocess | |
# apt-get install python-bs4 | |
from bs4 import BeautifulSoup | |
def usage(): | |
print(""" | |
Usage: | |
> mkdir /tmp/tellico | |
> cd /tmp/tellico | |
> unzip [/path/to/TellicoFile/MyBookLibrary.tc] | |
> Tellico2Calibre.py tellico.xml | |
""") | |
if len(sys.argv) < 1: | |
usage() | |
sys.exit(2) | |
xmlFile = os.path.abspath(sys.argv[1]) | |
coverDir = os.path.join(os.path.dirname(xmlFile), "images") | |
with open(xmlFile, 'r') as tellico: | |
soup = BeautifulSoup(tellico.read()) | |
for entry in soup.findAll('entry'): | |
#print(entry) | |
#print(entry.title.string) | |
if not entry.title: | |
continue | |
cmdArgs = u"calibredb add --empty --duplicates --tags \"Print\"" | |
cmdArgs = u"{} --title \"{}\"".format(cmdArgs, entry.title.string) | |
authors = " & ".join([a.string.strip() for a in entry.findAll('author')]) | |
if len(authors) > 0: | |
cmdArgs = u"{} --authors \"{}\"".format(cmdArgs, authors) | |
if entry.isbn: | |
cmdArgs = u"{} --isbn \"{}\"".format(cmdArgs, entry.isbn.string) | |
if entry.cover: | |
cover = os.path.join(coverDir, entry.cover.string) | |
cmdArgs = u"{} --cover \"{}\"".format(cmdArgs, cover) | |
print(cmdArgs) | |
output = subprocess.check_output(cmdArgs, shell=True) | |
print(output) | |
# Typical output: | |
# Backing up metadata | |
# Added book ids: 273 | |
# Notifying calibre of the change | |
output = output.split("\n") | |
bookId = output[1].split()[3] | |
# Fields: calibredb set_metadata --list-fields | |
# comments, publisher, pubdate, rating, tags | |
meta = u"calibredb set_metadata" | |
if entry.publisher: | |
meta = u"{} --field \"publisher:{}\"".format(meta, entry.publisher.string) | |
if entry.pub_year: | |
meta = u"{} --field \"pubdate:{}\"".format(meta, entry.pub_year.string) | |
if entry.comments: | |
meta = u"{} --field \"comments:{}\"".format(meta, entry.comments.string) | |
if entry.rating: | |
meta = u"{} --field \"rating:{}\"".format(meta, entry.rating.string) | |
if entry.loaned: | |
meta = u"{} --field \"tags:{}\"".format(meta, "Print,Loaned") | |
meta = u"{} {}".format(meta, bookId) | |
print(meta) | |
output = subprocess.check_output(meta, shell=True) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment