Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 01-25-2020, 04:57 AM   #1
digitalvideo
Junior Member
digitalvideo began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2017
Device: kobo H20
help with epub on_import filetype plugin

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

Last edited by digitalvideo; 01-25-2020 at 05:00 AM.
digitalvideo is offline   Reply With Quote
Old 01-29-2020, 05:54 AM   #2
digitalvideo
Junior Member
digitalvideo began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2017
Device: kobo H20
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
Attached Files
File Type: zip IEATFNOM_plugin v0.97.zip (1.3 KB, 255 views)
digitalvideo is offline   Reply With Quote
Advert
Old 01-29-2020, 07:02 AM   #3
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
You're welcome and glad you figured it out.
kovidgoyal is offline   Reply With Quote
Old 02-26-2020, 04:22 AM   #4
digitalvideo
Junior Member
digitalvideo began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2017
Device: kobo H20
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?
digitalvideo is offline   Reply With Quote
Old 02-27-2020, 05:13 AM   #5
digitalvideo
Junior Member
digitalvideo began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Apr 2017
Device: kobo H20
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,
digitalvideo is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[FileType Plugin] GetFileName Terisa de morgan Plugins 412 03-15-2024 01:56 AM
[FileType Plugin] YVES Bible Plugin ClashTheBunny Plugins 27 01-16-2023 01:25 AM
Question about file_types in FileType plugin Terisa de morgan Plugins 6 02-08-2017 05:53 AM
Zip Filetype Plugin? rsingley Plugins 7 02-11-2011 05:11 PM
How to register a filetype? plisken iRex 2 09-28-2009 07:16 AM


All times are GMT -4. The time now is 06:58 PM.


MobileRead.com is a privately owned, operated and funded community.