Reuse def

All about user-defined episode / movie / file name format expressions
Post Reply
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Reuse def

Post by kim »

is there a way to reuse a pre def variable in another {} ?
e.g. like this

Code: Select all

{def infoDK = net.filebot.WebServices.TheMovieDB.getMovieInfo(Movie, new Locale ('da', 'DK'), true)} 
{infoDK.name}
if not, Feature Request ;)
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Reuse def

Post by rednoah »

1.
No, but you could write your entire format in a single {...} block.


2.
You can use the {localize} binding for what you're trying to do:

Code: Select all

{localize.Danish.n}

Code: Select all

{localize.Danish.info.name}
@see viewtopic.php?t=3761
:idea: Please read the FAQ and How to Request Help.
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Reuse def

Post by kim »

I know, I just changed it because of this viewtopic.php?f=6&t=5120

localize = TheMovieDB.getMovieInfo(movie, locale, FALSE) ?

I want TheMovieDB.getMovieInfo(movie, locale, TRUE) to reduce api load

from htpc

Code: Select all

def movieInfo = TheMovieDB.getMovieInfo(movie, locale, true)
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Reuse def

Post by rednoah »

{localize.Danish.info.name} does ask for extended information so it should be exactly the same as TheMovieDB.getMovieInfo(movie, Danish, true) as far as I know.

If you set the last parameter to false then that should require much less data, assuming that the data isn't cached already. Kinda depends on what you're doing.
:idea: Please read the FAQ and How to Request Help.
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Reuse def

Post by kim »

well I tested it and:

it looks like (format):

Code: Select all

def infoDK = localize.DANISH.info;
def infoUS = localize.ENGLISH.info;
= LOG:
Fetch resource: api.themoviedb.org/3/movie/ID?language=en-US
Fetch resource: api.themoviedb.org/3/movie/ID?language=da-DK
Fetch resource: api.themoviedb.org/3/movie/ID?append_to_response=alternative_titles%2Creleases%2Ccasts%2Ctrailers&language=da
Fetch resource: api.themoviedb.org/3/configuration
Fetch resource: api.themoviedb.org/3/movie/ID?append_to_response=alternative_titles%2Creleases%2Ccasts%2Ctrailers&language=en
[MOVE] from *** to ***
Fetch resource: api.themoviedb.org/3/movie/ID?language=en
to me this looks bad :(

btw: still waiting on the new revision
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Reuse def

Post by kim »

And
def infoDK = net.filebot.WebServices.TheMovieDB.getMovieInfo(Movie, new Locale ('da', 'DK'), true);
def infoUS = net.filebot.WebServices.TheMovieDB.getMovieInfo(Movie, new Locale ('en', 'US'), true);
= LOG:
Fetch resource: api.themoviedb.org/3/movie/ID?language=en-US
Fetch resource: api.themoviedb.org/3/movie/ID?append_to_response=alternative_titles%2Creleases%2Ccasts%2Ctrailers&language=da-DK
Fetch resource: api.themoviedb.org/3/configuration
Fetch resource: api.themoviedb.org/3/movie/ID?append_to_response=alternative_titles%2Creleases%2Ccasts%2Ctrailers&language=en-US
[MOVE] from *** to ***
Fetch resource: api.themoviedb.org/3/movie/ID?language=en
Only the rename part (from AMC)

And this way it gets reused in the htpc (not downloaded again)
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Reuse def

Post by rednoah »

New revisions are now uploaded here instead of SF:
http://get.filebot.net/filebot/latest/
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Re: Reuse def

Post by devster »

rednoah wrote: 16 Jun 2017, 18:14 1.
No, but you could write your entire format in a single {...} block.
What is the difference between:

Code: Select all

{ superu = { it.upper() } }
{ superu(ny)} 
(stupid example, but works) and something like:

Code: Select all

{ def a = audioLanguages; a.size() > 1 ? a.ISO3.join(", ").upperInitial() : a.name.first() }
In the first case I seem to be able to reuse "superu(something)" in other parts of the script, but in the second case I seem to be able to reuse it.
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Reuse def

Post by rednoah »

If you use def then it's a Groovy variable for the local block. If you don't use def then it's a global variable for the Groovy engine. It's difficult to reason about global variables. If you're advanced enough to use variables and functions, then I'd recommend using def for all your variables writing the entire format in a single {...} block of structured Groovy code (kinda like you're doing in your shared custom format).

Here, you're assigning a global variable, and the value of that variable is a Closure:

Code: Select all

superu = { it.upper() }
:idea: Please read the FAQ and How to Request Help.
Post Reply