Mobileread
help with epub on_import filetype plugin
#1  digitalvideo 01-25-2020, 04:57 AM
Hi,

I try to create an on_import filetype plugin that:

Read the current import epub metadata
Read the current import epub name
Create a temporary epub
Apply it current import epub metadata
Modify temporary epub author and tile from current import epub name
Return the temporary epub


It seem to work if I drag and drop pub one by one, but if I drag and drop multiple epub some are not well flagged and it also make duplicate ?

Same problem if I import a folder with multiple epub.

In fact, I not find a good explanation of tempory_file or a sample to make it that way (all my try not working).

I have to say that i'm new in python and calibe dev
I'm on macos 10.14.6 and Calibre 4.9.1

Regards

Code
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2020, Philippe Perquin <philippe.perquin@laposte.net>'
__docformat__ = 'restructuredtext en'
import os
import shutil
from calibre.customize import FileTypePlugin
class IEATFNOM(FileTypePlugin): name = 'IEATFNOM' # Name of the plugin description = 'I have my epub named to "author - title.epub", and now i want calibre to import epub with $authors $title metadata from files names and other metadatas from info in files' supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on author = 'Philippe Perquin' # The author of this plugin version = (0, 41, 0) # The version number of this plugin file_types = set(['epub']) # The file types that this plugin will be applied to on_import = True # Run this plugin at import drag and drop can_be_disabled = True priority = 100 minimum_calibre_version = (4, 8, 00) def run(self, path_to_ebook): from calibre.ebooks.metadata.meta import get_metadata, set_metadata if os.path.exists('/Users/pil/Documents/piltemp.epub'): os.remove('/Users/pil/Documents/piltemp.epub') ImportExt = os.path.splitext(path_to_ebook)[-1][1:].lower() ImportName = os.path.splitext(os.path.basename(path_to_ebook))[0] ImportNameSplit = ImportName.split(' - ') epubauthor = '' epubtitle = '' epubauthor = str(ImportNameSplit[0]) epubauthor = epubauthor.lower()#+ " titi" #for debug purpose''' if len(ImportNameSplit) == 2: epubtitle = str(ImportNameSplit[1])#+ " titi" #for debug purpose''' epubtitle = epubtitle.lower() shutil.copy2(path_to_ebook, '/Users/pil/Documents/piltemp.epub') piltempFile = open('/Users/pil/Documents/piltemp.epub', 'r+b') piltempMeta = get_metadata(piltempFile, ImportExt) piltempMeta.title = epubtitle piltempMeta.title_sort = epubtitle #active this if you want the same as title in sort piltempMeta.authors = [epubauthor] #transfrom the string in list piltempMeta.author_sort = epubauthor #active this if you want the same as authors in sort set_metadata(piltempFile, piltempMeta, ImportExt) return piltempFile.name
Reply 

#2  digitalvideo 01-29-2020, 05:54 AM
Hi,

I finnally find the bug (the message was better on windows), it comme from the simultaneous jobs.

I also now use the self.temporary_file() method.

kovidgoyal, thanks a lot for this great software

Regards,

Code
from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2020, Philippe Perquin <digitalvideo.digitalvideo@gmail.com>'
__docformat__ = 'restructuredtext en'
import os
import shutil
from random import randint
import sys
from calibre.customize import FileTypePlugin
class IEATFNOM(FileTypePlugin): name = 'IEATFNOM' # Name of the plugin description = 'I have my epub named to "author - title.epub", and now i want calibre to import epub with $authors $title metadata from files names and other metadatas from info in files' supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on author = 'Philippe Perquin' # The author of this plugin version = (0, 97, 0) # The version number of this plugin file_types = set(['epub']) # The file types that this plugin will be applied to on_import = True # Run this plugin at import drag and drop can_be_disabled = True #priority = 100 minimum_calibre_version = (4, 8, 00) def run(self, path_to_ebook): from calibre.ebooks.metadata.meta import get_metadata, set_metadata '''ASK CALIBRE FOR A TEMPORY FILE''' CreatedTemporaryFile = self.temporary_file('.epub') '''GET INFO FROM ORIGINAL FILE''' #ImportExt = os.path.splitext(path_to_ebook)[-1][1:].lower() ImportExt = 'epub' ImportName = os.path.splitext(os.path.basename(path_to_ebook))[0] '''RETRIEVE AUTHORS AND TITLE FROM ORIGINAL FILE NAME''' ImportNameSplit = ImportName.split(" - ") epubauthor = "" epubtitle = "" epubauthor = str(ImportNameSplit[0]) epubauthor = epubauthor.lower()#+ " titi" #for debug purpose if len(ImportNameSplit) == 2: epubtitle = str(ImportNameSplit[1])#+ " titi" #for debug purpose epubtitle = epubtitle.lower() '''COPY ORIGINAL FILE TO TEMPORY FILE PLACE, WILL BE DELETE BY CALIBRE''' shutil.copy2(path_to_ebook, CreatedTemporaryFile.name) '''OPEN TEMPORY FILE AND GET METADA''' TempFile = open(CreatedTemporaryFile.name, 'r+b') TempMeta = get_metadata(TempFile, ImportExt) TempMeta.title = epubtitle TempMeta.title_sort = epubtitle #active this if you want the same as title in sort TempMeta.authors = [epubauthor] #transfrom the string in list TempMeta.author_sort = epubauthor #active this if you want the same as authors in sort #piltempMeta.publisher = 'Hello World2' # acitivate this to show change in main Calibre windows #for debug purpose '''SET METADATA IN TEMPORY FILE''' set_metadata(TempFile, TempMeta, ImportExt) '''RETURN THE FILE PATH OF TEMPORY FILE TO ADD''' return TempFile.name
[zip] IEATFNOM_plugin v0.97.zip (1.3 KB, 36 views)
Reply 

#3  kovidgoyal 01-29-2020, 07:02 AM
You're welcome and glad you figured it out.
Reply 

#4  digitalvideo 02-26-2020, 04:22 AM
hi,

I try now to make it more universal.
I add a config GUI.

But for this i also try to change the code a litle bit.

When the code was :

Quote
TempMeta = get_metadata(TempFile, ImportExt)

TempMeta.title = epubtitle
I try to change it this way :

Quote
TempMeta = get_metadata(TempFile, ImportExt)
MetaValueToChange = title
TempMeta.MetaValueToChange = epubtitle
but this give me this error AttributeError: Metadata object has no attribute named: 'MetaValueToChange'

in fact i try to put in variable the name of the value to change, in this case i can use the same code with multiples value

any one have an idea?
Reply 

#5  digitalvideo 02-27-2020, 05:13 AM
Hi,

Maybe i found a solution by writing :

Quote
TempMeta = get_metadata(TempFile, ImportExt)
MetaValueToChange = title
setattr(TempMeta, MetaValueToChange, epubtitle)
this seem to work

regards,
Reply 

Today's Posts | Search this Thread | Login | Register