Page 1 of 1

Renaming file base on JSON list?

Posted: 06 Oct 2017, 06:22
by dadelayer
Is there a way to rename files based on a JSON list? For example I have something like this :

Code: Select all

[{"title":"The Little Hours","year":"2017","imdbid":"tt5666304","scid":"10080"},{"title":"CarGo","year":"2017","imdbid":"tt6680792","scid":"10079"},{"title":"My Little Pony: The Movie","year":"2017","imdbid":"tt4131800","scid":"10078"},{"title":"Amityville: The Awakening","year":"2017","imdbid":"tt1935897","scid":"10077"},{"title":"6 Days","year":"2017","imdbid":"tt4703048","scid":"10076"},{"title":"Armstrong","year":"2016","imdbid":"tt4451006","scid":"10075"},{"title":"Spider-Man: Homecoming","year":"2017","imdbid":"tt2250912","scid":"10074"},{"title":"War for the Planet of the Apes","year":"2017","imdbid":"tt3450958","scid":"10073"},{"title":"Maudie","year":"2016","imdbid":"tt3721954","scid":"10072"},{"title":"Girls Trip","year":"2017","imdbid":"tt3564472","scid":"10071"},{"title":"The Emoji Movie","year":"2017","imdbid":"tt4877122","scid":"10070"},{"title":"An Ordinary Man","year":"2017","imdbid":"tt1785288","scid":"10069"},{"title":"Gerald's Game","year":"2017","imdbid":"tt3748172","scid":"10068"}]
the name of my files are something like 10080.mp4

Could FileBot know to find 10080 from the JSON list, which is the movie The Little Hours ?

Re: Renaming file base on JSON list?

Posted: 06 Oct 2017, 07:27
by rednoah
Yes, that's possible. But you'll need basic Groovy skills for writing a custom format that does the custom lookup with filename and JSON file. It'll be a relatively simple one-liner though.

You can use the Batch Rename files based on sibling XML files example as inspiration:
viewtopic.php?t=2072

Re: Renaming file base on JSON list?

Posted: 07 Oct 2017, 10:19
by rednoah
e.g.

Code: Select all

{
def json = new groovy.json.JsonSlurper().parseText('''[{"title":"The Little Hours","year":"2017","imdbid":"tt5666304","scid":"10080"}]''')
def scid = fn // e.g. 10080.mp4

json.find{ it.scid == scid }.title
}
Now you just need to have JsonSlurper read from a File. ;)

Re: Renaming file base on JSON list?

Posted: 08 Oct 2017, 07:42
by dadelayer
rednoah wrote: 07 Oct 2017, 10:19 e.g.

Code: Select all

{
def json = new groovy.json.JsonSlurper().parseText('''[{"title":"The Little Hours","year":"2017","imdbid":"tt5666304","scid":"10080"}]''')
def scid = fn // e.g. 10080.mp4

json.find{ it.scid == scid }.title
}
Now you just need to have JsonSlurper read from a File. ;)
Thank you very much, that seems to work now after reading from file. You saved me a headache. I sent a few $ via PayPal donate for a cup of coffee or something. Thank you again!

Re: Renaming file base on JSON list?

Posted: 08 Oct 2017, 17:15
by rednoah
Make sure to share your final solution. ;)

Re: Renaming file base on JSON list?

Posted: 08 Oct 2017, 19:41
by dadelayer

Code: Select all

{
def inputFile = new File("F:/MEGA/SC MOVIES/1.json")
def json = new groovy.json.JsonSlurper().parseText(inputFile.text)
def scid = fn 
def name = json.find{ it.scid == scid }.title
def year = json.find{ it.scid == scid }.year
def rename = name + ' - ' + year
}
I'm new to Groovy so it might not be the best possible syntax, but it works for em :)

Re: Renaming file base on JSON list?

Posted: 09 Oct 2017, 08:13
by rednoah
Here's how I'd solve this problem:

Code: Select all

{
    def jsonFile = "/path/to/file.json" as File
    def json = new groovy.json.JsonSlurper().parse(jsonFile)

    def scid = fn
    def node = json.find{ it.scid == scid }

    [node.name, node.year].join(' - ')
}