Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 03-21-2021, 04:25 PM   #1
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Template: Converting a search & replace into a template

For fanfiction, I have custom ao3: and ffnet: identifiers that use the story ID. I use it for crossposted stories and because it looks a little nicer than the url: identifier in book details.

I have some search & replaces that extract the story ID from the urls and copy them to the identifiers, and then another that removes the URL identifiers. Here's the first two search & replaces (including what happens if you give the wrong url type to one):

Click image for larger version

Name:	2021-03-20_220034.png
Views:	126
Size:	47.0 KB
ID:	186090

I also have action chain to automate this:
  1. If publisher:"=Archive of Our Own" AND identifiers:"=url:" AND NOT #fanficstatus:"=Anthology", then run search & replace "AO3 Identifiers."
  2. If publisher:"=Fanfiction.net" AND identifiers:"=url:" AND NOT #fanficstatus:"=Anthology", then run search & replace "FFNet Identifiers."
  3. If #fanficstatus:"~(Completed|Oneshot|Abandoned)" AND identifiers:"=url:" AND (identifiers:"=ao3:" OR identifiers:"=ffnet:"), then run a search & replace to remove the url: identifier. (I keep the url: on in-progress fics so FFF has access to it.)

The current way of doing it requires a lot of selection modifiers which change the book list. So I want try turning it into templates to not change the book list. This is why I asked about single field edit identifiers in the Action Chains thread.

I made GPM template that matches the books I want for each step, but am not actually sure how to modify the identifiers. And I'm also not sure if this would be best as one single template or three smaller ones (my test templates are the latter; I tried the former and got a bit confused on how to arrange the thing).

Any suggestions?

Last edited by ownedbycats; 03-21-2021 at 09:08 PM.
ownedbycats is offline   Reply With Quote
Old 03-22-2021, 03:19 PM   #2
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Unfortunately I don't understand what you want to do.

You say you want to modify identifiers with a template. By themselves, templates can't modify metadata. They need to be invoked by something that can change the metadata, such as action chains or search & replace.

You say that selection modifiers (in action chains) are a problem. You can avoid these by doing searches in your templates. One way to do this is to use a chain variable action to compute the identifiers you want for each book (using from_selection) and store the result in a book_var. Then use a template in single field edit to fetch and return the book var, returning the existing identifiers if the book var isn't set. As far as I can tell you would need to select the books with the identifier you want to change. The action chain wouldn't need to alter that selection.

Of course you might be asking a different question...
chaley is offline   Reply With Quote
Advert
Old 03-22-2021, 03:22 PM   #3
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Yes, i want to replace the search & replaces in my action chains with templates.
ownedbycats is offline   Reply With Quote
Old 03-22-2021, 03:33 PM   #4
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Yes, i want to replace the search & replaces in my action chains with templates.
I think you can do this as I described, but using from_search() instead of from_selection(). The chain_vars action would do the search and compute the result for each of the three cases. You would then need do 1 selection modifier to select all books and run a single field edit on each book, returning the value you want in identifiers.

There is some complexity if the same book can have multiple identifiers changed. In the second and subsequent searches you would need to see if a book_var for that book had already been set and, if so, use that instead of what is in the metadata.

A possible worthwhile addition to ask of capink would be to have the single field edit run over any book with any book_var set. That would eliminate the need to modify any selection. The chain vars action would compute what is needed and the single field action would run over any books with new data.
chaley is offline   Reply With Quote
Old 03-23-2021, 03:18 PM   #5
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
I will try that
ownedbycats is offline   Reply With Quote
Advert
Old 03-23-2021, 03:22 PM   #6
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
I will try that
My attempt at an average rating chain might help explain what I am talking about.
chaley is offline   Reply With Quote
Old 03-24-2021, 08:01 AM   #7
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
After thinking about this overnight, I think my suggestion is more than you need. All you need to do is run a single field edit on identifiers over all the books. The template would generate the 'right' identifiers and return those. If there are no changes then return the existing identifiers.

The template would implement the equivalent of the three S & Rs, manipulating the list directly. Here is a sample template that does the first and the third. The second is a near copy of the first.

Code:
program:
	publisher = field('publisher');
	ids = field('identifiers');
	status = field('#fanficstatus');
	u = select(ids, 'url');

	if publisher == 'Archive of Our Own' && u && status != 'Anthology' then
		ids = list_difference(ids, strcat('url:', u), ',');
		n = re(u, '^.*/(\d+)$', '\1');
		ids = list_union(ids, strcat('ao3:', n), ',')
	fi;

	if 'Completed|Oneshot|Abandoned' in status && u &&
			(select(ids, 'ao3') || select(ids, 'ffnet')) then
		ids = list_difference(ids, strcat('url:', u), ',')
	fi;
	ids
chaley is offline   Reply With Quote
Old 03-24-2021, 05:24 PM   #8
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
That works pretty well for converting the IDs.

For some reason it removes the URL from #fanficstatus: In-Progress fics which I do not want, thinking it might be a regex issue so I'll tinker a bit more.

EDIT: See next post

Last edited by ownedbycats; 03-26-2021 at 01:34 AM.
ownedbycats is offline   Reply With Quote
Old 03-25-2021, 11:02 PM   #9
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Ok I did some more tinkering of the code on #7 and tweaked the regexes a bit (^.*/(\d+)$ occasionally captured chapter numbers instead of the story id).

Code:
program:
	publisher = field('publisher');
	ids = field('identifiers');
	status = field('#fanficstatus');
	u = select(ids, 'url');

	if publisher == 'Archive of Our Own' && u && status != 'Anthology' then
		ids = list_difference(ids, strcat('url:', u), ',');
		n = re(u, '^https://archiveofourown.org/works/(\d+)', '\1');
		ids = list_union(ids, strcat('ao3:', n), ',')
	fi;

	if publisher == 'FanFiction.net' && u && status != 'Anthology' then
		ids = list_difference(ids, strcat('url:', u), ',');
		n = re(u, '^https://www.fanfiction.net/s/(\d+)/(\d+)/', '\1');
		ids = list_union(ids, strcat('ffnet:', n), ',')
	fi;

	if 'Completed|Oneshot|Abandoned' in status && u &&
		(select(ids, 'ao3') || select(ids, 'ffnet')) then
		ids = list_difference(ids, strcat('url:', u), ',')
	fi;
	ids
Oddly enough if I remove the last part it still removes the url: identifier

I will keep tinkering.

EDIT: If I remove the list_difference lines (except the last one), it does not remove url: Is that necessary for something else to work? I did not see any strange results when I tried with multiple books of different statuses
Attached Thumbnails
Click image for larger version

Name:	2021-03-26 05_14_57-Template tester.png
Views:	78
Size:	38.4 KB
ID:	186155  

Last edited by ownedbycats; 03-26-2021 at 04:20 AM.
ownedbycats is offline   Reply With Quote
Old 03-26-2021, 04:08 AM   #10
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
OK, I did some more testing of the code in #7. For some reason it behaves differently for Archive of Our Own and Fanfiction.net.
I am not sure of the transformation rules you want.

Here are transforms implemented by the template in #7
  • Case 1: Book with metadata
    • Publisher: Archive of Our Own
    • Status: In-Progress
    • ids: url:https://archiveofourown/works/12345
    then the template does:
    1. The first 'if' succeeds and
      • adds ao3:12345 to ids
      • removes url:... from ids
      The identifiers are now 'ao3:12345'
    2. The second 'if' condition fails because In-Progress is not in status.
    The result identifiers are 'ao3:12345'. This is wrong? Perhaps the first 'if' is not supposed to remove the url:... identifier?
  • Case 2: If the book has the following metadata:
    • Publisher: FanFiction
    • Status: In-Progress
    • ids: ffnet:abcde, url:https://archiveofourown/works/9876
    then the template does:
    1. The first 'if' condition fails because of wrong publisher, doing nothing
    2. The second 'if' condition fails because status is In-Progress, doing nothing.
    The ids remain 'ffnet:abcde, url:https://archiveofourown/works/9876'. Is this right?
  • Case 3: If the book has the following metadata:
    • Publisher: FanFiction
    • Status: Completed
    • ids: ffnet:abcde, url:https://archiveofourown/works/9876
    then the template does:
    1. The first 'if' condition fails because of wrong publisher, doing nothing
    2. The second 'if' condition succeeds because status is Completed. The url:... id is removed.
    The ids become 'ffnet:abcde'. Is this right?
You also must be careful about the order of evaluation. The second 'if' condition might succeed because the first 'if' adds the identifier 'ao3'. You may or may not always want that.
chaley is offline   Reply With Quote
Old 03-26-2021, 04:11 AM   #11
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,741
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Is that necessary for something else to work?
The list_difference() functions in the first 2 if blocks are removing identifiers. It seems that isn't what you want.
chaley is offline   Reply With Quote
Old 03-26-2021, 04:32 AM   #12
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,616
Karma: 61176603
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Yes, I only wanted to remove identifiers if they matched certain statuses. That's what the third part does. Thank you.

Last edited by ownedbycats; 03-26-2021 at 05:18 AM.
ownedbycats is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bulk metadata Search/Replace: template function question meghane_e Library Management 3 01-24-2019 09:32 PM
Using built-in template functions in a custom template function ilovejedd Library Management 4 01-28-2018 12:20 PM
Template Help in S&R Tanjamuse Library Management 2 09-10-2017 06:37 AM
template or search feature question bulldogmo Calibre 2 08-06-2014 06:34 PM


All times are GMT -4. The time now is 07:20 PM.


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