Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
bharry29
Posts: 4
Joined: 02 Sep 2023, 18:13

Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by bharry29 »

Hello everyone,

I'm currently working on a FileBot script for post-processing my movie collection, and I'm facing a specific challenge that I need assistance with. I hope some experienced FileBot users here can help me out.

Background:
I have a large collection of movies that I want to organize into folders based on their original language using FileBot. Additionally, I want to ensure that the file naming follows the Plex movie naming conventions. To achieve this, I've created two separate scripts: one for fetching movie details from TMDb and another for applying the Plex formatting.

The Challenge:
I'm having trouble getting these two scripts to work together within FileBot. Specifically, I want to ensure that the movie details are fetched from TMDb, and then the Plex formatting is applied to the renamed files in the correct language-based folders.

Current Setup:
Here's my current setup:

I have a script that fetches movie details from TMDb and organizes the files into language-based folders, but it doesn't apply Plex formatting.
I have a preset in FileBot that applies Plex formatting, but it doesn't organize the files into language-based folders.

What I Need:
I need a way to combine these two scripts so that I can fetch movie details from TMDb, apply Plex formatting, and ensure the files are organized correctly into language-based folders.

Questions:

Is it possible to create a wrapper script that combines the TMDb fetching and Plex formatting logic?
How can I set up FileBot to ensure this wrapper script runs as a post-processing step after applying the Plex preset?
Are there any examples or templates available that I can use as a starting point for this combined script?

I would greatly appreciate any guidance, examples, or suggestions from the community on how to tackle this challenge effectively. Thank you in advance for your help!
User avatar
rednoah
The Source
Posts: 23002
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by rednoah »

You'll want to express the target file path you want via your custom format, and then let FileBot move / rename files accordingly, no need for multiple custom scripts, just one filebot command:

Console Output: Select all

$ filebot -rename -r ./input --db TheMovieDB --output ./output --format "{info.OriginalLanguage}/{~plex.id}" --action TEST  --log INFO
[TEST] from [input/Avatar.2009.mp4] to [output/en/Avatar (2009) {tmdb-19995}/Avatar (2009).mp4]
:!: Note that Plex will expect that en is added as Movie folder, so you'll have to add each language folder as Movie folder to your Movies library if you follow this naming scheme.
:idea: Please read the FAQ and How to Request Help.
bharry29
Posts: 4
Joined: 02 Sep 2023, 18:13

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by bharry29 »

Is there a way to do it on the filebot UI? I'm not running filebot as a script but using file-bot node as a program in my synology NAS?

I'm trying to do the following as a post-process after renaming the files per the preset according to plex format:

Groovy: Select all

// Set your TMDb API key
def apiKey = "e90fd22e1f7a120d5703975fac12501e"

// TMDb API base URL
def tmdbBaseUrl = "https://api.themoviedb.org/3"

// Mapping of Plex movie formatting language codes to full language names
def languageMap = [
    "en": "English",
    "hi": "Hindi",
    "te": "Telugu",
    // Add more languages here...
]

// Function to fetch movie details
def fetchMovieDetails = { movieTitle ->
    def encodedMovieTitle = URLEncoder.encode(movieTitle, "UTF-8")

    def tmdbUrl = "${tmdbBaseUrl}/search/movie?api_key=${apiKey}&query=${encodedMovieTitle}"
    def response = new URL(tmdbUrl).getText()

    def searchResults = new groovy.json.JsonSlurper().parseText(response)
    if (!searchResults.results.isEmpty()) {
        def movie = searchResults.results[0]
        return movie
    } else {
        return null
    }
}

// Define the root destination folder where you want to organize your movies
def destinationRoot = "G:\\Media\\OrganizedMovies"

// Get the original language code from the file path using Plex naming convention
def getOriginalLanguageCode = { filePath ->
    def matcher = filePath =~ /\/(en|hi|te)\//
    return matcher ? matcher[0][1] : null
}

// Main script execution
def organizeMovies = { file, movieTitle ->
    def movieInfo = fetchMovieDetails(movieTitle)
    
    if (movieInfo) {
        def originalLanguageCode = getOriginalLanguageCode(file.toString())
        def originalLanguage = languageMap.getOrDefault(originalLanguageCode, "Unknown")
        def destinationLanguageFolder = new File("${destinationRoot}\\${originalLanguage}")
        
        if (!destinationLanguageFolder.exists()) {
            destinationLanguageFolder.mkdirs()
        }

        def movieFolderName = movieInfo.title ?: "Unknown Movie"
        def destinationMovieFolder = new File("${destinationLanguageFolder}\\${movieFolderName}")
        if (!destinationMovieFolder.exists()) {
            destinationMovieFolder.mkdirs()
        }

        def destinationFilePath = "${destinationMovieFolder}\\${file.name}"
        file.renameTo(new File(destinationFilePath))
        
        println "Moved '${file.name}' to '${destinationMovieFolder}\\'"
    } else {
        println "Movie details not found for '${movieTitle}'. '${file.name}' was not moved."
    }
}

// Process the incoming file list
def movieFiles = args*.toFile()
movieFiles.each { movieFile ->
    def movieTitle = movieFile.name - ".mkv"
    organizeMovies(movieFile, movieTitle)
}
User avatar
rednoah
The Source
Posts: 23002
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by rednoah »

:idea: You can paste the following format in the Format Editor, or the corresponding *Format fields in the FileBot Node WebUI:

Format: Select all

/path/to/Media/{info.OriginalLanguage}/{~plex.id}

:idea: Note that your custom code does not use the TMDB original language information, and instead merely checks for existing folders named en or hi or te, so you can do that too if that's what you want:

Format: Select all

/path/to/Media/
{
	// split by either Windows \ or Unix /
	def folders = folder.path.split(/\\|\//)

	if ('en' in folders)
		return 'English'
	if ('hi' in folders)
		return 'Hindi'
	if ('te' in folders)
		return 'Telugu'

	return 'Unknown'
}
/
{ ~plex.id }


:idea: If you're using FileBot Node, then that means that you are using the amc script via the filebot command-line tool. You are notably not using the FileBot Desktop application. You do want to use the FileBot Desktop application to prototype and thoroughly test your custom format though, before integrating it into your amc script commands in a second step.

:idea: Use @file syntax for reading complex custom formats from text files, i.e. your custom format via a custom --def movieFormat option.




EDIT:

:!: Did you use ChatGPT to generate code? Your custom Groovy code makes no sense in the context of FileBot. You say you're running on Synology NAS, a Linux system, yet you use Windows file paths hard-coded in your script. It seems to be a very poor attempt at writing a standalone (i.e. does not integrate with FileBot) custom Groovy code to move / rename files.
:idea: Please read the FAQ and How to Request Help.
bharry29
Posts: 4
Joined: 02 Sep 2023, 18:13

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by bharry29 »

I'm testing the script in my windows machine for now and will test it in my NAS after modifying it per Linux paths. I did try ChatGPT to modify my basic code but will try what you've suggested with the formats in the UI.
bharry29
Posts: 4
Joined: 02 Sep 2023, 18:13

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by bharry29 »

Also, what should I do if the movie info is not found by the script? How do I handle that scenario?
User avatar
rednoah
The Source
Posts: 23002
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Subject: Need Help with FileBot Script to Organize Movies by Language and Plex Formatting

Post by rednoah »

bharry29 wrote: 04 Sep 2023, 15:48 Also, what should I do if the movie info is not found by the script? How do I handle that scenario?
:idea: If FileBot cannot identify a file, yet the movie in the file exists the database, then you can manually search for and select the movie. Please read Q: How do I fix misidentified files? for details.

:idea: If you want to process generic non-movie files, then you may be able to use Plain File Mode to create your own file path rewrite rules.
:idea: Please read the FAQ and How to Request Help.
Post Reply