Match array based on dynamic variable name?

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
User avatar
Wolfie
Posts: 165
Joined: 27 Oct 2015, 02:59

Match array based on dynamic variable name?

Post by Wolfie »

Similar to the deep regex query from ages ago, but instead of returning a value where the key->value pair is (desired value)->(regex to match) or vice-versa... key = variable name for the value to match, value = what to match it against (so return the matching array).

I have two copies of The Bad News Bears (TV series), the first available was German/PAL, then a US version was released that I also got. The German version has "PAL" as part of the a variable while the USA version has "NTSC." So to distinguish between the two versions automatically, I want to be able to pick a value to match (video[0].colourPrimaries) to see if it has the word "PAL" in it. If so, it returns "German" with array (matching key->value pair).

The arrangement I'm looking to use is...

Groovy: Select all

"editions":[
    "tt0078566":[
        "German":[ "video[0].colourPrimaries": "/PAL/" ],
    ],
],
Obviously it would only try to find matches based on the content (IMDb, TMDb, TVDb). The part I need help with is using the key for the variable to use.

I come up with some interesting concepts/ideas, don't I? 🤣
User avatar
rednoah
The Source
Posts: 24671
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Match array based on dynamic variable name?

Post by rednoah »

:arrow: I guess could just write code like so:

Format: Select all

{
	if (n == /The Bad News Bear/ && video.colourPrimaries =~ /PAL/) {
		return " [German]"
	}
}
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 165
Joined: 27 Oct 2015, 02:59

Re: Match array based on dynamic variable name?

Post by Wolfie »

I was thinking more along the lines of an external file that would have the information, such as the array I showed above. So if it matches the IMDb ID (for example), it'll look to see what variable needs to be used for comparison. So x would be set to "video[0].colourPrimaries" but instead of comparing "y" to "x," it would be comparing the variable that x is referring to, to the value of y. Sort of like executing the contents of a variable, except that it wouldn't be a command.
User avatar
rednoah
The Source
Posts: 24671
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Match array based on dynamic variable name?

Post by rednoah »

:idea: My code is much easier to read and than your proposed external text file. I wouldn't add unnecessary complexity.

:arrow: You can always read the code from an external text file: [DOCS] Split code into external *.groovy script files
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 165
Joined: 27 Oct 2015, 02:59

Re: Match array based on dynamic variable name?

Post by Wolfie »

The idea is to be able to easily add things without a ton of 'if' conditions.
User avatar
rednoah
The Source
Posts: 24671
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Match array based on dynamic variable name?

Post by rednoah »

:idea: I really wouldn't add an extra layer of unnecessary logic. My code is easier to read, and easier to maintain, and almost the same size too. If you are writing your JSON configuration file yourself, just for FileBot, then you really want to just write the code instead. A file only makes sense if you already have the file due to some other tool, or because it's shared across multiple tools, or because it's easier to maintain, etc. I look at your code sample and my code sample, and mine is definitely easier to maintain.


:arrow: That said, you can write your own solution, and if you need help with dynamically executing code that you have read as text from your configuration file, then that would work like so:

Format: Select all

{
	def snippet = 'video[0].colourPrimaries'
	evaluate(snippet)
}
:idea: Please read the FAQ and How to Request Help.
User avatar
Wolfie
Posts: 165
Joined: 27 Oct 2015, 02:59

Re: Match array based on dynamic variable name?

Post by Wolfie »

Superb! That works beautifully.

Now I have a way to distinguish between different versions of the same show so I can keep them separated without continuous effort.
Post Reply