![]() |
#1 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 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 |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,187
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
A file type plugin is not the right place to do this and you really shouldn't create an extra db instance. Having multiple db objects writing to the same db is a recipe for bad things to happen. In fact on windows, you will get permission denied errors since windows enforces mandatory file locking.
Instead create a replacement for the add books plugin (you can re-use the calibre add books code for the most part) and have that plugin scan newly imported book records for ones that contain only a single format, and launch a convert for them, by calling the convert books plugin. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 41
Karma: 1005240
Join Date: Feb 2012
Device: Kindle Keyboard, iPhone
|
Thanks for the rapid reply. Thanks also for the architectural advice about how to proceed - I was starting from the simplest plugin example, so writing an on_import action seemed logical given my goal. I will investigate the approach you suggest tomorrow and post any follow-up questions here.
|
![]() |
![]() |
![]() |
#4 |
Enthusiast
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 27
Karma: 250352
Join Date: Jun 2016
Location: Bolivia, South America
Device: Kobo aura h2o
|
Hi Anoved, have you been able to create that plugin? I was wondering because I want to make almost the same for kepub format, and if you made it and published the source code it would be a lot easier for me
![]() Thank you |
![]() |
![]() |
![]() |
#5 | |
null operator (he/him)
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 21,621
Karma: 29710338
Join Date: Mar 2012
Location: Sydney Australia
Device: none
|
Quote:
Moderator Notice
You posted to a thread that's almost almost four years old - do you really expect the OP to answer - please pay heed to the warning message you get when you open ancient threads BR |
|
![]() |
![]() |
Advert | |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Old Thread] Feature Idea - Auto convert on import | jphphotography | Calibre | 6 | 11-04-2012 09:17 PM |
Book download plugin - help requested | Kvasnicka | Plugins | 4 | 06-10-2011 12:26 PM |
Advice requested re: Nook Books. | DavidKitson | Barnes & Noble NOOK | 2 | 03-19-2011 10:14 AM |
How should file names be parsed and prepared for calibre import? Use cases requested | GlennMaples | Calibre | 0 | 01-09-2011 12:41 AM |
Auto import from folder | benlau | Calibre | 3 | 01-19-2010 03:01 AM |