View Single Post
Old 08-09-2012, 10:46 PM   #1
oblib__
Junior Member
oblib__ began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Aug 2012
Device: Smartphone
Help with content modifying plugin

I hope this isn't the wrong place to ask, or to simple/demanding of a question, but I'd like to make a Calibre plugin that can do mass replacements of strings within the text of a book, independent of incoming or outgoing format. I've got the heart of the conversion written in python already, in that if I pass my python script a text file (or even just a string), it does the replacements and prints out or saves the result.

What I'm missing is how to do the plugin. Could someone either point me to a similar plugin that I could copy and learn from, or even better, provide me with the skeleton outline of what I need to do this? The "write your own plugin" page gives me the basics, and I would think that I could take the HelloWorld plugin and make it do what I want with just a few changes.

So here's the heart of HelloWorld. How do I make it provide me with the contents of the book (as in all of the words in the book) as a string, and then let me save a modified string as the new contents?
Code:
 def run(self, path_to_ebook):
        from calibre.ebooks.metadata.meta import get_metadata, set_metadata
        file = open(path_to_ebook, 'r+b')
        ext  = os.path.splitext(path_to_ebook)[-1][1:].lower()
        mi = get_metadata(file, ext)
        mi.publisher = 'Hello World'
        set_metadata(file, mi, ext)
        return path_to_ebook
Thanks in advance!
oblib__ is offline   Reply With Quote