Register Guidelines E-Books Search Today's Posts Mark Forums Read

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

Notices

Reply
 
Thread Tools Search this Thread
Old 03-17-2025, 05:55 PM   #1441
fidvo
Addict
fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.
 
Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
Need help with module to add empty book

I'm trying to create a module that adds an empty book and fills in some default metadata. Everything works except that the book doesn't show up in the book list until I reload the library (for example by restarting Calibre or switching to a different library and back). Here is my code:

Code:
from calibre_plugins.action_chains.actions.base import ChainAction
from calibre.ebooks.metadata.book.base import Metadata

class TestAddEmpty(ChainAction):
    name = 'Add Empty'

    def run(self, gui, settings, chain):
        db = gui.current_db
        ids, duplicates = db.new_api.add_books([(Metadata(None), {})])

        # What do I put here?

        newid = ids[0]
        gui.library_view.select_rows([newid])
I assume I have to add something at the # What do I put here? marker in order to make the new book show up, but I don't know what it is. Any ideas?

Thanks.
fidvo is offline   Reply With Quote
Old 03-18-2025, 05:20 AM   #1442
Terisa de morgan
Grand Sorcerer
Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.
 
Terisa de morgan's Avatar
 
Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
Need help with an action chain in the same way:
  1. Collect a list of dta for a specific column
  2. Duplicate the selected book as many times as element in the previous list
  3. For all elements in the initial list, set the column with the proper value in the duplicated book
  4. Refresh the screen

For [1] I've found the proper resource
For [2] I can do a module which:
  1. Collect metadata from the selected book
  2. Create as many duplicates as necessary
  3. Set the proper column value

I have the following doubts:
  1. How do I pass the list to the module? I've found how to define vars local/global to the chain but I don't know how the module would be able to read them
  2. How do you check that one and only one is selected? So you raise an error if no book is selected or more than one book is selected.

Sorry for the long post, but I've done very basic things at Action Chains till now.
Terisa de morgan is offline   Reply With Quote
Old 03-18-2025, 09:35 AM   #1443
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by fidvo View Post
I'm trying to create a module that adds an empty book and fills in some default metadata. Everything works except that the book doesn't show up in the book list until I reload the library (for example by restarting Calibre or switching to a different library and back). Here is my code:

Code:
from calibre_plugins.action_chains.actions.base import ChainAction
from calibre.ebooks.metadata.book.base import Metadata

class TestAddEmpty(ChainAction):
    name = 'Add Empty'

    def run(self, gui, settings, chain):
        db = gui.current_db
        ids, duplicates = db.new_api.add_books([(Metadata(None), {})])

        # What do I put here?

        newid = ids[0]
        gui.library_view.select_rows([newid])
I assume I have to add something at the # What do I put here? marker in order to make the new book show up, but I don't know what it is. Any ideas?

Thanks.
I have no idea why this is happening. I tried adding either one of these two lines, to no avail:

Code:
gui.iactions['Add Books'].refresh_gui(1,set_current_row=0)
Code:
gui.library_view.model().books_added(1)
Maybe if you post this in the development forum Kovid of chaley can help.
capink is offline   Reply With Quote
Old 03-18-2025, 09:36 AM   #1444
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by Terisa de morgan View Post
How do I pass the list to the module? I've found how to define vars local/global to the chain but I don't know how the module would be able to read them
To retrieve the variable using a python module:
Code:
chain.chain_vars.get('variable_name', '')
Quote:
Originally Posted by Terisa de morgan View Post
How do you check that one and only one is selected? So you raise an error if no book is selected or more than one book is selected.
If you are doing this using python:
Code:
if len(gui.current_view().selectionModel().selectedRows()) !=1:
   do_something
If you are doing it with a regular action, you can set a condition as illustrated in this post.
capink is offline   Reply With Quote
Old 03-18-2025, 09:38 AM   #1445
Terisa de morgan
Grand Sorcerer
Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.
 
Terisa de morgan's Avatar
 
Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
Quote:
Originally Posted by capink View Post
To retrieve the variable using a python module:
Code:
chain.chain_vars.get('variable_name', '')


If you are doing this using python:
Code:
if len(gui.current_view().selectionModel().selectedRows()) !=1:
   do_something
If you are doing it with a regular action, you can set a condition as illustrated in this post.
Thank you, I'm going to see if my ideas are working (I like to try programming new things).
Terisa de morgan is offline   Reply With Quote
Old 03-18-2025, 10:11 AM   #1446
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,269
Karma: 7955525
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by fidvo View Post
I'm trying to create a module that adds an empty book and fills in some default metadata. Everything works except that the book doesn't show up in the book list until I reload the library (for example by restarting Calibre or switching to a different library and back). Here is my code:

Code:
from calibre_plugins.action_chains.actions.base import ChainAction
from calibre.ebooks.metadata.book.base import Metadata

class TestAddEmpty(ChainAction):
    name = 'Add Empty'

    def run(self, gui, settings, chain):
        db = gui.current_db
        ids, duplicates = db.new_api.add_books([(Metadata(None), {})])

        # What do I put here?

        newid = ids[0]
        gui.library_view.select_rows([newid])
I assume I have to add something at the # What do I put here? marker in order to make the new book show up, but I don't know what it is. Any ideas?

Thanks.
This template contains an example of what you put "here".
Code:
python:
def evaluate(book, context):
	db = context.db

	from calibre.ebooks.metadata.book.base import Metadata
	ids, duplicates = db.new_api.add_books([(Metadata(None), {})])
	print(ids, duplicates)

	from calibre.gui2.ui import get_gui
	gui = get_gui()
	gui.current_db.data.refresh()
	gui.library_view.model().resort()
	gui.tags_view.recount()

	gui.library_view.set_current_row(book_id=ids[0])
	return 'a string'
chaley is offline   Reply With Quote
Old 03-18-2025, 12:49 PM   #1447
fidvo
Addict
fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.
 
Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
Quote:
Originally Posted by chaley View Post
This template contains an example of what you put "here".
It worked!

Thanks, chaley. You're awesome.

I tweaked it a little, and here is the complete module code that adds an empty book, in case it's of interest to anyone else:

Code:
from calibre_plugins.action_chains.actions.base import ChainAction
from calibre.ebooks.metadata.book.base import Metadata

class AddEmpty(ChainAction):
    name = 'Add Empty'

    def run(self, gui, settings, chain):
        db = gui.current_db
        ids, duplicates = db.new_api.add_books([(Metadata(None), {})])
        db.data.refresh()
        gui.library_view.model().resort()
        gui.tags_view.recount()
        newid = ids[0]
        gui.library_view.select_rows([newid])
fidvo is offline   Reply With Quote
Old 03-18-2025, 05:38 PM   #1448
Terisa de morgan
Grand Sorcerer
Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.
 
Terisa de morgan's Avatar
 
Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
Quote:
Originally Posted by fidvo View Post
It worked!

Thanks, chaley. You're awesome.

I tweaked it a little, and here is the complete module code that adds an empty book, in case it's of interest to anyone else:
Thank you, as I'm going to use it as the base for duplicating empty books (I use it from time to time).
Terisa de morgan is offline   Reply With Quote
Old 03-19-2025, 12:43 AM   #1449
fidvo
Addict
fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.fidvo ought to be getting tired of karma fortunes by now.
 
Posts: 307
Karma: 1599870
Join Date: Jun 2012
Device: none
Quote:
Originally Posted by Terisa de morgan View Post
Thank you, as I'm going to use it as the base for duplicating empty books (I use it from time to time).
I actually have a different module that does exactly that. It suffered from the same problem as the other one, which is now fixed.

This one creates the book with the metadata already copied from the book that was selected when you activate the plugin, so it's probably more efficient than creating a new book and copying over metadata fields afterward.

A couple of notes:

1. It only allows you to duplicate one book at a time, because I didn't need anything more robust when I wrote it. The code is simple enough though that if you're at all familiar with Python it shouldn't be too hard to modify it to handle multiple books, if that's what you need.

2. It only duplicates the metadata; it doesn't copy the book formats. Since you mentioned duplicating empty books, I figure that shouldn't be a problem for you.

Here is the source code:

Code:
        
        
from calibre_plugins.action_chains.actions.base import ChainAction

class DuplicateBook(ChainAction):

    name = 'Duplicate Book'
    
    def run(self, gui, settings, chain):
        
        db = gui.current_db
        rows = gui.current_view().selectionModel().selectedRows()
        book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ]
        #Only applies if exactly one book is selected.
        if len(book_ids) == 1:
            book_id = book_ids[0]
            #get the metadata from the existing book.
            meta = db.new_api.get_metadata(book_id)
            ids, duplicates = db.new_api.add_books([(meta, {})])
            newid = ids[0]
            db.data.refresh()
            gui.library_view.model().resort()
            gui.tags_view.recount()
            gui.library_view.select_rows([newid])
        else:
            raise Exception("Please select only one book first.")
fidvo is offline   Reply With Quote
Old 03-19-2025, 04:45 AM   #1450
Terisa de morgan
Grand Sorcerer
Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.Terisa de morgan ought to be getting tired of karma fortunes by now.
 
Terisa de morgan's Avatar
 
Posts: 6,511
Karma: 12595249
Join Date: Jun 2009
Location: Madrid, Spain
Device: Kobo Clara/Aura One/Forma,XiaoMI 5, iPad, Huawei MediaPad, YotaPhone 2
Quote:
Originally Posted by fidvo View Post
I actually have a different module that does exactly that. It suffered from the same problem as the other one, which is now fixed.

This one creates the book with the metadata already copied from the book that was selected when you activate the plugin, so it's probably more efficient than creating a new book and copying over metadata fields afterward.

A couple of notes:

1. It only allows you to duplicate one book at a time, because I didn't need anything more robust when I wrote it. The code is simple enough though that if you're at all familiar with Python it shouldn't be too hard to modify it to handle multiple books, if that's what you need.

2. It only duplicates the metadata; it doesn't copy the book formats. Since you mentioned duplicating empty books, I figure that shouldn't be a problem for you.

Here is the source code:

Code:
        
        
from calibre_plugins.action_chains.actions.base import ChainAction

class DuplicateBook(ChainAction):

    name = 'Duplicate Book'
    
    def run(self, gui, settings, chain):
        
        db = gui.current_db
        rows = gui.current_view().selectionModel().selectedRows()
        book_ids = [ gui.library_view.model().db.id(row.row()) for row in rows ]
        #Only applies if exactly one book is selected.
        if len(book_ids) == 1:
            book_id = book_ids[0]
            #get the metadata from the existing book.
            meta = db.new_api.get_metadata(book_id)
            ids, duplicates = db.new_api.add_books([(meta, {})])
            newid = ids[0]
            db.data.refresh()
            gui.library_view.model().resort()
            gui.tags_view.recount()
            gui.library_view.select_rows([newid])
        else:
            raise Exception("Please select only one book first.")
Thank you! I've got an additional step changing one custom column, but that's easy. I'm going to create more than one book but that's quite easy to modify.
Terisa de morgan is offline   Reply With Quote
Old 03-20-2025, 04:29 PM   #1451
Alinara
Member
Alinara began at the beginning.
 
Posts: 10
Karma: 10
Join Date: Nov 2023
Device: none
Question

I have a basic question because I dont understand why it do not work as I want it to. Asfar as I understood you can set conditions before a specific task or a chain starts. But my condition do not work. If I have 2 Books where one is in a"Series" and the other is a "Single". It do not get the two right.

My Code condition A:

program:
ifempty(field('series'), 'text_if_empty')

text
=
text_if_empty

chain -> edit Single field choose in field XY "Single"



My Code condition B:

program:
ifempty(field('series'), 'text_if_empty')

text
!=
text_if_empty


chain -> edit Single field choose in field XY "Series"


Here also screenshots:



What do I understand wrong here? Does is not check the condition at every Book selected? Or is the Text check with = and != not possible?

Its my first try with the plugin and I tested some possibilitys and read the instructions but dont get what I understood wrong.
Alinara is offline   Reply With Quote
Old 03-20-2025, 07:41 PM   #1452
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,174
Karma: 1988646
Join Date: Aug 2015
Device: Kindle
Conditions does not check all selected books because they are meant quick branching tool based on things that can be tested quickly like selection_count etc.

If you want to check every book, and only process books that have series column (I suppose that what you want to do), you add a "Search using template" Action and do the following:
  • Make sure the "Select all books resulting from this search" checkbox is ticked.
  • Make sure you select the "template output is a list of book ids" radio button.
  • Use the following template:
    Code:
    program:
        book_ids = '';
        for book_id in from_selection('id'):
            if book_field(book_id, 'series') != '' then
                book_ids = list_union(book_ids, book_id, ',')
            fi
        rof;
        book_ids
This will deselect all books that are not part of a series.

As an aside, attach your screenshots to the post instead of using external sites as they are not showing.
capink is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Action Chains Resources capink Plugins 68 01-20-2025 01:06 PM
[Editor Plugin] Editor Chains capink Plugins 94 07-03-2024 07:26 PM
[GUI Plugin] Noosfere_util, a companion plugin to noosfere DB lrpirlet Plugins 2 08-18-2022 03:15 PM
[GUI Plugin] Save Virtual Libraries To Column (GUI) chaley Plugins 14 04-04-2021 05:25 AM


All times are GMT -4. The time now is 04:23 AM.


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