Noob groovy question

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Noob groovy question

Post by devster »

I'm trying to remove a few tags from the default match as I'm already catching them myself.

Code: Select all

    { specials = { allOf
                     { tags.removeAll { it.contains('imax') } }
                     { def last = n.tokenize(" ").last()
                       fn.after(/(?i:$last)/).findAll(/(?i:imax).(?i:edition|version)?/)*.replaceAll(/[._-]/, " ") }
                     .flatten().sort() }
      specials() }
I also tried

Code: Select all

tags.removeAll { it ==~ /(?i:imax)/ }
tags.removeIf { it ==~ /(?i:imax)/ }
tags.removeIf { it.matches(/(?i:imax)/) }
anyway, result is always the same, I'm stuck as the closure returns a true or false instead of the modified array.
What's the correct syntax/method?
Thanks.
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: Noob groovy question

Post by rednoah »

I'd use Collection.findAll{predicte} for that.
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Re: Noob groovy question

Post by devster »

I'll try that, in that case I'd have to do reverse matching though, right? So something like

Code: Select all

tags.findAll { !( it ==~ /(?i:imax)/ }
I'm still a bit confused as to why my solution isn't working though. In a regular groovy script if I try to run my solutions they seem to work.
For example on tio.run:

Code: Select all

def tags = ['Imax']
def out = tags.findAll { ! (it ==~ /(?i:imax)/) }
tags.removeIf { it ==~ /(?i:imax)/ }
assert out.size() == 0
assert tags.size() == 0
works. Is it because it's inside an allOf?
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: Noob groovy question

Post by rednoah »

Maybe different versions of Java and Groovy runtime? e.g. removeIf is a pretty new addition to the standard API. Plus maybe removeIf doesn't mutate the array but returns a new one without modifying the original.

Being inside of an allOf block won't make a difference.

Do you get a warning in the format editor or it working and just not doing what you think but should be doing?
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Re: Noob groovy question

Post by devster »

The below:

Code: Select all

    { specials = { allOf
                     { tags.removeIf { it ==~ /(?i:imax)/ } }
                     { def last = n.tokenize(" ").last()
                       fn.after(/(?i:$last)/).findAll(/(?i:imax).(?i:edition|version)?/)*.replaceAll(/[._-]/, " ") }
                     .flatten().sort() }
      specials() }
returns [IMAX Edition, true] in the format. So I'm guessing the function works as expected (returning a boolean).

Example filename >> Aquaman.2018.IMAX.Edition.1080p.BluRay.DDP7.1.x264-Geek.mkv

I believe something is happening because tags is a binding, I tried the following, which works:

Code: Select all

    { def t = tags
      t.add(3)
      assert t == ['Imax', 3]
      t.removeIf { it ==~ /(?i:imax)/ }
      assert t == [3]
      t.removeIf { it instanceof Integer }
      assert t.size() == 0
	 specials = { allOf
                     { t }
                     { def last = n.tokenize(" ").last()
                       fn.after(/(?i:$last)/).findAll(/(?i:imax).(?i:edition|version)?/)*.replaceAll(/[._-]/, " ") }
                     .flatten().sort() }
      specials() }
So I guess problem solved.

For reference, I also tried with groovysh on my machine:

Code: Select all

Groovy Shell (2.5.6, JVM: 11.0.2)
Type ':help' or ':h' for help.
groovy:000> tags = ['Imax']
===> [Imax]
groovy:000> out = tags.findAll { ! (it ==~ /(?i:imax)/) }
===> []
groovy:000> tags.removeIf { it ==~ /(?i:imax)/ }
===> true
groovy:000> assert out.size() == 0
===> null
groovy:000> assert tags.size() == 0
===> null
I only work in black and sometimes very, very dark grey. (Batman)
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Noob groovy question

Post by kim »

Can't you just use ?

Code: Select all

{tags*.replaceAll(/(?i:imax)/).join()}
I just do this:

Code: Select all

{
def Version = {any{' [' + fn.matchAll(/Recut|UNCUT|UNRATED|EXTENDED|Special.+Edition|Final.Cut|Super.Sized.+Rated.+Version|director.?s.cut|Ultimate.+Edition|\bTHE\b.[^\d]+\bCUT\b|IMAX(?:.Edition|.Version)?/)
*.lower()
*.upperInitial()
*.lowerTrail()
.unique()
.join(', ')
.replaceAll(/Imax/, 'IMAX')
.replaceAll(/[._]/, ' ') + ']'}
{}
};
}
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Re: Noob groovy question

Post by devster »

Neat.
My last version works though, no issues.
Do you have your full format shared somewhere @Kim? It seems very similar to mine, I think I can copy some bits.
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: Noob groovy question

Post by rednoah »

devster wrote: 08 Apr 2019, 17:04 I believe something is happening because tags is a binding.
I see. Bindings are indeed not variables that you can modify. You'll get a new value or object each time you access a binding.
:idea: Please read the FAQ and How to Request Help.
kim
Power User
Posts: 1251
Joined: 15 May 2014, 16:17

Re: Noob groovy question

Post by kim »

Not pretty, but it works :)

Code: Select all

{label}/{
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);
def sameTitle = (infoDK.name == infoUS.name);
def dkInSoundtrack=infoUS.spokenLanguages.toString().contains('da');
def dkInAudiotrack= any{audioLanguages.toString().contains('da')}{false};
def dkInTitle = ' [DK]';
def dkInFileName=[/.DK/,/ DK/];
def norm = {it.replaceAll(/[:–-] /, ' - ').replaceAll(/\//, '_').replace(/:/, '.').trim()};
def Version = {any{' [' + fn.matchAll(/Recut|UNCUT|UNRATED|EXTENDED|Special.+Edition|Final.Cut|Super.Sized.+Rated.+Version|director.?s.cut|Ultimate.+Edition|\bTHE\b.[^\d]+\bCUT\b|IMAX/)*.lower()*.upperInitial()*.lowerTrail().unique().join(', ').replaceAll(/Imax/, 'IMAX').replaceAll(/[._]/, ' ') + ']'}{}};
((dkInFileName.any{fn.contains(it)})||(dkInSoundtrack)||(dkInAudiotrack)) ? (norm(sameTitle && genres.contains('Animation') ? (infoDK.name+Version+dkInTitle) : (infoDK.name+Version))) : (norm(infoUS.name+Version))}{
' ('+y+')'}/{
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);
def dkInSoundtrack=infoUS.spokenLanguages.toString().contains('da');
def dkInAudiotrack= any{audioLanguages.toString().contains('da')}{false};
def dkInFileName=[/.DK/,/ DK/];
def norm = {it.replaceAll(/[()',]/).replaceAll(/[:–-]/, '.').upperInitial().space('.').replaceAll(/\//, '_').trim()};
((dkInFileName.any{fn.contains(it)})||(dkInSoundtrack)||(dkInAudiotrack)) ? norm(infoDK.name) : norm(infoUS.name)}{
any{'.'+fn.matchAll(/Recut|UNCUT|UNRATED|EXTENDED|Special.+Edition|Final.Cut|Super.Sized.+Rated.+Version|director.?s.cut|Ultimate.+Edition|\bTHE\b.[^\d]+\bCUT\b|IMAX/)*.upperInitial()*.lowerTrail().unique().join('.').replaceAll(/Imax/, 'IMAX').replaceAll(/[_]/,'.').upper()}{}}{
any{'.'+fn.match(/Repack|Proper|RERIP/).upper()}{}}{
'.'+y}{
any{'.'+fn.matchAll(/\bR5\b|\b3D\b/).join('.').upper()}{}}{any{'.'+s3d}{}}{
any{(video.displayAspectRatio.join('').toBigDecimal() <= 1.5) ? '.FS' : null}{}}{
any{'.'+fn.matchAll(/\bDK\b\.\bEN\b|\bEN\b\.\bDK\b|\bDK\b|\bDC\b|\bSTV\b/).join('.').upper()}{}}{
def dir = file.parentFile.name+'.'+fn;
dir.find(/\bm720p\b|\bm1080p\b/) ? '.'+'m'+vf : dim[0]>=1280||dim[1]>=720 ? '.'+vf : '.'+hd}{
'.'+source}{
any{'.'+video.codecID.join('').match(/DX50|DIVX|DIV3|XVID/).replaceAll(/DIVX/, 'DivX4').replaceAll(/(?i)DX50/, 'DivX5').replaceAll(/(?i)DIV3/, 'DivX3').replaceAll(/(?i)XVID/,'XViD')}{'.'+vc.match(/x264|AVC/).replaceAll(/(?i)AVC/,'x264').lower()}{}}{
def auCo = audio.Codec;
def ChannelString = any
 {(audio.'ChannelPositionsString2'*.replaceAll(/Object\sBased\s\/|(\d+)?\sobjects\s\/|0.(?=\d.\d)/, '')*.split(' / ')*.collect{ it.split('/')*.toDouble().sum() }*.max().max()).toString()}
 {channels};
def codecSubVersion = any
 {audio.any{ a -> call{a.FormatProfile} =~ 'HRA' } ? '.HRA' : null}
 {audio.any{ a -> call{a.FormatProfile} =~ 'X / MA / Core' } ? '.X' : null}
 {audio.any{ a -> call{a.FormatProfile} =~ 'MA / Core' } ? '.MA' : null}
 {audio.any{ a -> call{a.FormatProfile} =~ 'ES Matrix / Core' } ? '-ES' : null}
 {null};
def codecVersion = any
 { [ [auCo.join().match(/DTS-HD/), codecSubVersion].join(), auCo.join().match(/TrueHD/)].join('.')}
 {[auCo.join().match(/DTS-HD/), codecSubVersion].join()}
 {auCo.join().match(/TrueHD/)}
 {codecSubVersion != null ? [auCo.join().match(/DTS/), codecSubVersion].join() : auCo.join().match(/DTS/)}
 {ac};
(allOf
 {'.'+codecVersion}
 {if( ((ac == 'AAC'||ac == 'MP3') && (channels != '2.0' || ChannelString != channels) ) || ( (ac == 'AC3'||ac == 'DTS'||ac == 'TrueHD') && (channels != '5.1' || ChannelString != channels) ) ) return ChannelString}
 {aco.match(/Atmos/)}
 ).join('.')
}{
def dn = call{(file.parentFile.name).match(/(?:\-\w+(?=\[\w+\])|\-\w+$)/)};
def nn = call{fn.match(/(?:(?=(?:19|20)\d{2})\w.+$)/)};
def g = call{(file.parentFile.name+'.'+fn).find(/\-/) ? call{group.replaceAll(/\bDK\b|\bFS\b/,'')} : null};
def gg = '-'+g;
def gb = call{fn.match(/(?:^\w{3,}(?=[-]))/)};
def fm = call{fn.match(/(?:(?:\-(?!\bDL\b|\bRip\b|\bES\b|\bHD\b)\w+$)|(?:\-(?!\bDL\b|\bRip\b|\bES\b|\bHD\b)\w+[\.\_]\w+[\-]\w+$)|(?:\-\w+(?=\.cd[0-9]|\-trailer|\.part[0-9]|\.da|\.en|\.dan|\.eng))|(?:\-(?!\bDL\b|\bRip\b|\bES\b|\bHD\b)\w+[\-\.\_](?!\b720p\b|\b1080p\b)\w+$)|(?:\-\w+[\.]+\w+(?=\.cd[0-9]))|(?:\-(?!\bDL\b|\bRip\b|\bES\b|\bHD\b)\w+(?=\[\w+\]$)))/)};
if(g!=null && dn!=null && gg.equalsIgnoreCase(dn)) return '-'+g;
if(g!=null && dn!=null) return dn;
if(nn==null && dn!=null) return dn;
if(g!=null && (fm!=null||gb!=null) && (gg.equalsIgnoreCase(fm) || g.equalsIgnoreCase(gb))) return '-'+g;
if(g!=null && g!= '' && (fm!=null && gb!=null)) return '-'+g;
if(g==null && fm!=null && nn!=null) return ''+fm;
if(gb!=null && fm!=null && nn!=null) return '-'+gb;
if(g==null && gb!=null && fm==null && dn==null && nn==null) return '-'+gb;
if(g!=null && fm!=null && g!=fm) return fm;
if(g==null && fm==null && gb!=null && dn==null && nn==null) return '-'+gb;
if(g==null && fm!=null && gb!=null && dn==null && nn==null) return '-'+gb;
if(fm!=null) return fm}{
any{'.cd'+pi}{}}{
any{subt.replace(/.ind/, '')}{}}
Post Reply