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