Register Guidelines E-Books Today's Posts Search

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

Notices

Reply
 
Thread Tools Search this Thread
Old 04-23-2024, 09:30 AM   #1321
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Try this:

Code:
id_path_map = {}
for row in rows:
    book_id = self.gui.library_view.model().id(row)
    id_path_map[book_id] = dpath
self.gui.current_db.new_api.set_field('#custompath', id_path_map)
capink is offline   Reply With Quote
Old 04-23-2024, 01:01 PM   #1322
Dzib
Enthusiast
Dzib began at the beginning.
 
Posts: 29
Karma: 10
Join Date: Mar 2017
Device: Kindle Paperwhite
Works fine, thanks
Another question, not related : if I make a new chain with open with action, how to use the serie in the template but only if it's not null ?
I've tried with an if else statement but it's not working

Last edited by Dzib; 04-24-2024 at 02:01 AM.
Dzib is offline   Reply With Quote
Old 04-24-2024, 02:48 AM   #1323
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by Dzib View Post
Works fine, thanks
Append the following code under the run() method:

Code:
        col_name = '#custompath'
        db = gui.current_db
        id_path_map = {}
        for book_id in chain.scope().get_book_ids():
            fmt_paths = []
            fname = chain.evaluate_template(opts.template, book_id)
            if single_dir:
                fname = os.path.basename(fname)
            if opts.formats == 'all':
                formats = db.formats(book_id, index_is_id=True)
            else:
                formats = settings['formats']
            for fmt in [fmt.strip().lower() for fmt in formats.split(',')]:
                fmt_paths.append(os.path.join(path, f'{fname}.{fmt.lower()}'))
            id_path_map[book_id] = ','.join(fmt_paths)
        db.new_api.set_field(col_name, id_path_map)
Note: If you save more than one format per book, the above code will add them all to the custom column as comma separated list.

Quote:
Originally Posted by Dzib View Post
Another question, not related : if I make a new chain with open with action, how to use the serie in the template but only if it's not null ?
I've tried with an if else statement but it's not working
Consult the template language manual under the "Conditionally including text" section.

Last edited by capink; 04-24-2024 at 06:22 AM.
capink is offline   Reply With Quote
Old 04-24-2024, 04:16 AM   #1324
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by capink View Post
Append the following code under the run() method
Another way to do this without having to hard code the custom column name, is to append this instead to the run() method:

Code:
        db = gui.current_db
        for book_id in chain.scope().get_book_ids():
            fmt_paths = []
            fname = chain.evaluate_template(opts.template, book_id)
            if single_dir:
                fname = os.path.basename(fname)
            if opts.formats == 'all':
                formats = db.formats(book_id, index_is_id=True)
            else:
                formats = settings['formats']
            for fmt in [fmt.strip().lower() for fmt in formats.split(',')]:
                fmt_paths.append(os.path.join(path, f'{fname}.{fmt.lower()}'))
            self.plugin_action.book_vars[book_id]['save_to_disk_path'] = ','.join(fmt_paths)
Now, in your chain, add a second "Single Field Edit", choose the custom column from the dropdown menu and add the following template:

Code:
program:
	book_vars('save_to_disk_path')

Last edited by capink; 04-24-2024 at 06:23 AM.
capink is offline   Reply With Quote
Old 04-24-2024, 06:39 AM   #1325
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: 11,746
Karma: 6997673
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
Another way to do this without having to hard code the custom column name, is to append this instead to the run() method:
IIRC this can produce a value that isn't the "real" path generated by Save to Disk. The Save to Disk function will truncate segments of the path so the path length fits within the 250 character limit calibre enforces on the path length. The resulting path is also "sanitized" to remove any characters that shouldn't be in paths, for example colons, control characters, question marks, asterisks, etc.

The only way I know to get the "real" path is to include the calibre ID somewhere in the path, walk the resulting book folders to generate the full path to a book, then use the ID to insert that path into the book's metadata.
chaley is offline   Reply With Quote
Old 04-24-2024, 07:45 AM   #1326
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
IIRC this can produce a value that isn't the "real" path generated by Save to Disk. The Save to Disk function will truncate segments of the path so the path length fits within the 250 character limit calibre enforces on the path length. The resulting path is also "sanitized" to remove any characters that shouldn't be in paths, for example colons, control characters, question marks, asterisks, etc.

The only way I know to get the "real" path is to include the calibre ID somewhere in the path, walk the resulting book folders to generate the full path to a book, then use the ID to insert that path into the book's metadata.
Ahhh. I totally missed that. Using the id would work so long as the user includes it in the template in a pre-defined format that allows for parsing. Also the default template does not include the id.

One other way is to follow calibre's own way of sanitizing and shortening paths. Something like this:

Code:
        from calibre.library.save_to_disk import sanitize_args, get_path_components
        db = gui.current_db
        for book_id in chain.scope().get_book_ids():
            fmt_paths = []
            root, opts, length = sanitize_args(path, opts)
            mi = db.get_metadata(book_id, index_is_id=True)
            components = get_path_components(opts, mi, book_id, length)
            extensionless_path = os.path.sep.join(components)
            if single_dir:
                extensionless_path = components[-1]
            if opts.formats == 'all':
                formats = db.formats(book_id, index_is_id=True)
            else:
                formats = settings['formats']
            for fmt in [fmt.strip().lower() for fmt in formats.split(',')]:
                fmt_paths.append(os.path.join(root, f'{extensionless_path}.{fmt.lower()}'))
            self.plugin_action.book_vars[book_id]['save_to_disk_path'] = ','.join(fmt_paths)

Last edited by capink; 04-24-2024 at 07:55 AM.
capink is offline   Reply With Quote
Old 04-25-2024, 02:36 AM   #1327
Dzib
Enthusiast
Dzib began at the beginning.
 
Posts: 29
Karma: 10
Join Date: Mar 2017
Device: Kindle Paperwhite
Woaw, your code works perfect !
Many thanks to you & to chaley

Last edited by Dzib; 04-25-2024 at 06:39 AM.
Dzib is offline   Reply With Quote
Old 04-28-2024, 12:20 PM   #1328
lalf
Junior Member
lalf began at the beginning.
 
Posts: 8
Karma: 10
Join Date: Jul 2020
Device: Kobo Clara HD
Hello, I am trying to run 2 different types of Add Book actions, where one handles the metadata with the name, and the other with the content of the ebook (i go more in details here) https://www.mobileread.com/forums/sh...15&postcount=1

I tried the Add Book action but it seems to not have the metadata handling part that Calibre has. Is it possible adding it? It could fix the issue I have currently as I need to have metadata handled differently for 2 types of books (2 different folders).

Thanks for your work !
lalf is offline   Reply With Quote
Old 04-28-2024, 07:49 PM   #1329
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 8,672
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Is there an action that can select the first item in the book list? This would be for use after refreshing the book sort, which depending on book can sometimes send me a few hundred books down.

Last edited by ownedbycats; 04-28-2024 at 07:53 PM.
ownedbycats is offline   Reply With Quote
Old 04-29-2024, 03:12 AM   #1330
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by lalf View Post
Hello, I am trying to run 2 different types of Add Book actions, where one handles the metadata with the name, and the other with the content of the ebook (i go more in details here) https://www.mobileread.com/forums/sh...15&postcount=1

I tried the Add Book action but it seems to not have the metadata handling part that Calibre has. Is it possible adding it? It could fix the issue I have currently as I need to have metadata handled differently for 2 types of books (2 different folders).

Thanks for your work !
The add action calls calibre Adder, which follows the settings in calibre to decided whether to read metadata from book or from filename.

What you can do, is use the GetFileName plugin, which stores the filename in custom column. With this you can use Action Chains to add the book and apply the metadata from the GetFileName column. Action allows you to use Calibre Template language to write data to column using regex.
capink is offline   Reply With Quote
Old 04-29-2024, 03:15 AM   #1331
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,100
Karma: 1954136
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by ownedbycats View Post
Is there an action that can select the first item in the book list? This would be for use after refreshing the book sort, which depending on book can sometimes send me a few hundred books down.
Selection Modifier.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Action Chains Resources capink Plugins 59 Yesterday 05:17 PM
[Editor Plugin] Editor Chains capink Plugins 90 04-29-2024 12:21 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 10:08 PM.


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