03-21-2021, 05:25 PM | #1 |
Custom User Title
Posts: 9,811
Karma: 68360983
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): I also have action chain to automate this:
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 10:08 PM. |
03-22-2021, 04:19 PM | #2 |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
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... |
Advert | |
|
03-22-2021, 04:22 PM | #3 |
Custom User Title
Posts: 9,811
Karma: 68360983
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.
|
03-22-2021, 04:33 PM | #4 | |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
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. |
|
03-23-2021, 04:18 PM | #5 |
Custom User Title
Posts: 9,811
Karma: 68360983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I will try that
|
Advert | |
|
03-23-2021, 04:22 PM | #6 |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
My attempt at an average rating chain might help explain what I am talking about.
|
03-24-2021, 09:01 AM | #7 |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
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 |
03-24-2021, 06:24 PM | #8 |
Custom User Title
Posts: 9,811
Karma: 68360983
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 02:34 AM. |
03-26-2021, 12:02 AM | #9 |
Custom User Title
Posts: 9,811
Karma: 68360983
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 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 Last edited by ownedbycats; 03-26-2021 at 05:20 AM. |
03-26-2021, 05:08 AM | #10 | |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Here are transforms implemented by the template in #7
|
|
03-26-2021, 05:11 AM | #11 |
Grand Sorcerer
Posts: 12,142
Karma: 7908995
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
|
03-26-2021, 05:32 AM | #12 |
Custom User Title
Posts: 9,811
Karma: 68360983
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 06:18 AM. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Bulk metadata Search/Replace: template function question | meghane_e | Library Management | 3 | 01-24-2019 10:32 PM |
Using built-in template functions in a custom template function | ilovejedd | Library Management | 4 | 01-28-2018 01:20 PM |
Template Help in S&R | Tanjamuse | Library Management | 2 | 09-10-2017 07:37 AM |
template or search feature question | bulldogmo | Calibre | 2 | 08-06-2014 07:34 PM |