Page 1 of 1

Reuse def

Posted: 16 Jun 2017, 18:01
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 ;)

Re: Reuse def

Posted: 16 Jun 2017, 18:14
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

Re: Reuse def

Posted: 16 Jun 2017, 18:22
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)

Re: Reuse def

Posted: 16 Jun 2017, 18:27
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.

Re: Reuse def

Posted: 16 Jun 2017, 23:01
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

Re: Reuse def

Posted: 16 Jun 2017, 23:07
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)

Re: Reuse def

Posted: 17 Jun 2017, 04:36
by rednoah
New revisions are now uploaded here instead of SF:
http://get.filebot.net/filebot/latest/

Re: Reuse def

Posted: 20 Jun 2017, 22:46
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.

Re: Reuse def

Posted: 21 Jun 2017, 00:54
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() }