View Single Post
Old 02-17-2012, 12:02 AM   #1
anoved
Enthusiast
anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.anoved ought to be getting tired of karma fortunes by now.
 
anoved's Avatar
 
Posts: 41
Karma: 1005240
Join Date: Feb 2012
Device: Kindle Keyboard, iPhone
Advice requested to complete a auto-convert-format import plugin

I usually get ebooks in Mobi format for best results with my Kindle, but I like to maintain ePub versions as well to use with with other apps and devices. I don't add new books to my library so frequently that it's a hassle to do this manually, but I thought it would be a good project to write a plugin to do it for me automatically.

Goal: A plugin that automatically generates ePub versions of imported Mobi files. End state of import is a single book record with two formats, ePub and Mobi. (Eventually, I suppose this could be generalized to any preferred formats.)

Status: Close, but no cigar. The plugin I've written runs when a Mobi file is imported, creates an ePub version, and adds the ePub version to the library before proceeding to import the Mobi as usual. The result, however, is two separate book records, one in ePub format and the other Mobi, with otherwise identical metadata. I had hoped enabling the Automerge option would be sufficient to merge these, but it is not the case. (I suspect the unorthodox sequence of importing one book while ostensibly importing another doesn't give automerge a chance to work.)

Request: Any help figuring out how to implement this last step (merging the ePub version with the Mobi) would be appreciated. Also, since this is my first attempt at a plugin and I'm as yet somewhat unfamiliar with the Calibre code, any advice on canonical/better ways to do things would be great.

Code:
from calibre.customize import FileTypePlugin

class AutoCreateEpubFromMobi(FileTypePlugin):

	name = 'AutoCreateEpubFromMobi Plugin'
	description = 'Automatically create ePub versions of imported Mobi files.'
	supported_platforms = ['windows', 'osx', 'linux']
	author = 'Jim DeVona'
	version = (1, 0, 0)
	minimum_calibre_version = (0, 8, 39)
	file_types = set(['mobi'])
	on_import = True
	
	def run(self, path_to_ebook):
		
		# Make an ePub version of the input Mobi file.
		from calibre.ebooks.conversion.plumber import Plumber
		from calibre.utils.logging import Log
		log = Log()
		output_path = self.temporary_file('.epub')
		plumber = Plumber(path_to_ebook, output_path.name, log)
		plumber.run()
		
		# Get the ePub metadata, needed to import it manually.
		from calibre.ebooks.metadata.meta import get_metadata
		mdfile = open(output_path.name, 'r+b')
		mi = get_metadata(mdfile, 'epub')

		# Import the ePub.
		# Ideally, with correct Automerge settings, this format would be merged
		# with the original Mobi file being imported in a single book record.
		from calibre.utils.config import prefs
		from calibre.library.database2 import LibraryDatabase2
		db = LibraryDatabase2(prefs['library_path'])
		epub_id = db.import_book(mi, [output_path.name])
		
		# Get the imported ePub to show up in the browser.
		# (Without this, it doesn't appear in GUI until after restart.)
		# Not sure if there's a better way. Lifted from calibre/library/cli.py
		from calibre.utils.ipc import RC
		import time
		t = RC()
		t.start()
		time.sleep(1)
		if t.done:
			t.conn.send('refreshdb:')
			t.conn.close()		
		
		# Resume importing the original Mobi.
		return path_to_ebook
anoved is offline   Reply With Quote