Getting Media ID from Path

Any questions? Need some help?
Post Reply
Reneg
Posts: 36
Joined: 04 Jan 2012, 20:32

Getting Media ID from Path

Post by Reneg »

Hello,

I am using Filbot Node Clien in Docker to process my file.

I launch it from a script using the following command:

Shell: Select all

docker exec filebot-node ./opt/filebot-node/task 1
task 1 is:

Shell: Select all

-script
fn:amc
/volume1/ORIGINAL
--output
/archive
--action
move
-non-strict
--order
Airdate
--conflict
auto
--lang
en
--def
clean=y
skipExtract=y
seriesFormat=original\{ if(anime){return "ANIME SERIES"}else if(genres.contains("Animation")){return "CARTONI"}else{"TVS"} }\{plex.tail}{if (dc > 1) '.'+di}
movieFormat=original\{height <= 1080 ? genres.contains("Animation")  ? "ANIMATION": "MOVIES" : genres.contains("Animation")  ? "ANIMATION 4K" : "MOVIES 4K"}\{plex.tail}
excludeList=.excludes
--apply
refresh
--log
all
--log-file
/data/node/filebot.log
Each of the files that Filebot has to rename is stored in a folder that contains the following string:

Shell: Select all

- TMDBID-XXXXXX (For Movies)
- ID-XXXXXX (For TvShows)
Is there a way to instruct Filebot to match the media in accordance with that given ID so that I am 100% sure about the correct match?
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Getting Media ID from Path

Post by rednoah »

:?: What does the console output say? What makes you think that FileBot isn't using the ID marker?


:idea: FileBot does use the ID marker, so TMDBID-123 already works. However, ID-123 is not a recognized pattern and thus doesn't work. You'll need to use TMDBID-123 or TVDBID-123 for your TV Shows.


:idea: Alternatively, you could use the --q option to extract the ID via a custom query expression:
rednoah wrote: 20 Aug 2020, 11:26 e.g. Match ID12345 pattern from the filename and then use it as query:

Shell: Select all

--q "{ fn.match(/ID([0-9]{5,11})/) }"
e.g.

Shell: Select all

--q "{ f.path.match(/ID-([0-9]+)/) }"
:idea: Please read the FAQ and How to Request Help.
Reneg
Posts: 36
Joined: 04 Jan 2012, 20:32

Re: Getting Media ID from Path

Post by Reneg »

I didn't know Filebot was already checking the string for the ID...does it take the ID even if it is not in the filename but in the folder that contains the file?
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Getting Media ID from Path

Post by rednoah »

Reneg wrote: 27 Aug 2023, 15:52 I didn't know Filebot was already checking the string for the ID...does it take the ID even if it is not in the filename but in the folder that contains the file?
The ID marker is typically expected in the movie folder / series folder name, but anything you throw at it should work. I would run my own tests first though. Let us know if something doesn't work as expected.
:idea: Please read the FAQ and How to Request Help.
Reneg
Posts: 36
Joined: 04 Jan 2012, 20:32

Re: Getting Media ID from Path

Post by Reneg »

thank you for the info, I wrote a script to handle it with OMV and Docker, so both JDownloader and Filebot can run in their own container:

python: Select all

import os
import logging
import subprocess
import re
import shutil

FOLDER_PATH = '/srv/dev-disk-by-uuid-5ce16d9f-6b80-4417-a53e-f0806c9ed406/downloads/jdownloader'
DESTINATION_PATH = '/srv/dev-disk-by-uuid-5ce16d9f-6b80-4417-a53e-f0806c9ed406/downloads/toArchive/ITALIAN'

FILE_EXTENSIONS = ('.mkv', '.mp4', '.avi')

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def process_files_in_folder(folder_path):
    moved_files = []  # To keep track of moved files
    for root, _, files in os.walk(folder_path):
        for filename in files:
            if any(filename.endswith(ext) for ext in FILE_EXTENSIONS):
                file_path = os.path.join(root, filename)
                if process_file(file_path):  # Check if the file was moved
                    moved_files.append(file_path)
    return moved_files

def process_file(file_path):
    logger.info(f"Processing file: {file_path}")
    if "TMDBID-" in file_path:
        id_start = file_path.index("TMDBID-") + len("TMDBID-")
        movie_id = re.search(r"TMDBID-(\d+)", file_path).group(1)
        logger.info(f"Processing movie ID: {movie_id}")

        move_file_and_folder(file_path, DESTINATION_PATH)
        return True

    elif "ID-" in file_path:
        id_start = file_path.index("ID-") + len("ID-")
        show_id = re.search(r"ID-(\d+)", file_path).group(1)
        logger.info(f"Processing TV show ID: {show_id}")
        
        move_file_and_folder(file_path, DESTINATION_PATH)
        return True
    
    return False

def move_file_and_folder(source, destination_folder):
    try:
        parent_folder = os.path.dirname(source)
        
        # Replace "ID-" with "TVDBID-" only if it's not part of a longer word
        parent_folder = re.sub(r'\bID-', 'TVDBID-', parent_folder)
        
        destination_parent = os.path.join(destination_folder, os.path.basename(parent_folder))
        
        os.makedirs(destination_parent, exist_ok=True)
        
        filename = os.path.basename(source)
        destination_path = os.path.join(destination_parent, filename)
        
        shutil.move(source, destination_path)
        
        logger.info(f"File and its parent folder moved from {source} to {destination_path}")
    except shutil.Error as e:
        logger.error(f"File and folder move failed: {e}")

def remove_empty_subfolders(root_folder):
    for folder_path, _, _ in os.walk(root_folder, topdown=False):
        if folder_path != root_folder:  # Skip the root folder itself
            try:
                os.rmdir(folder_path)
                logger.info(f"Empty subfolder removed: {folder_path}")
            except OSError as e:
                logger.error(f"Error while removing subfolder: {folder_path} - {e}")

def run_command(command):
    try:
        subprocess.run(command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        logger.error(f"Command execution failed: {e}")

if __name__ == "__main__":
    logger.info("Starting script...")
    moved_files = process_files_in_folder(FOLDER_PATH)
    logger.info(f"Number of moved files: {len(moved_files)}")
    
    if moved_files:
        remove_empty_subfolders(FOLDER_PATH)
        logger.info("Running Docker command...")
        docker_command = "docker exec filebot-node ./opt/filebot-node/task 0"
        run_command(docker_command)

        logger.info("Running rclone move command...")
        rclone_command = (
            "flock -n /tmp/rclone_lockfile.lock rclone move "
            "/mnt/workingfolder/toWikiBox1 wikibox1:/multimedia/library -v --progress "
            "--transfers 1 --delete-empty-src-dirs "
            "--log-file=/mnt/workingfolder/transferLogs/move_MANUAL.txt"
        )
        run_command(rclone_command)
    logger.info("Script finished.")

Reneg
Posts: 36
Joined: 04 Jan 2012, 20:32

Re: Getting Media ID from Path

Post by Reneg »

Ok, Looks like it works, I will monitor it for a while.

It is important for me the ID because it is part of an app I wrote so I didn't want to change it too much! It is great that Filebot already takes the ID from the PATH


Screenshot
Reneg
Posts: 36
Joined: 04 Jan 2012, 20:32

Re: Getting Media ID from Path

Post by Reneg »

Hello,

From the log it looks like the database used is not THETVDB but TMDB althought in the path there is the TVDBID-405882 markup.

Console Output: Select all

Parameter: clean = y
Parameter: skipExtract = y
Parameter: seriesFormat = italian\{ if(anime){return "ANIME SERIES"}else if(genres.contains("Animation")){return "CARTONI"}else{"TVS"} }\{plex.tail}{if (dc > 1) '.'+di}
Parameter: movieFormat = italian\{height <= 1080 ? genres.contains("Animation")  ? "ANIMATION": "MOVIES" : genres.contains("Animation")  ? "ANIMATION 4K" : "MOVIES 4K"}\{plex.tail}
Parameter: excludeList = .excludes
Argument[0]: /volume1/ITALIAN
Use excludes: /archive/.excludes (0)
Input: /volume1/ITALIAN/The Idol - Stagione 1 (2023) - 1080P-HEVC - TVDBID-405882/The.Idol.2023.S01E05.Jocelyn.Forever.ITA.ENG.1080p.MAX.WEB-DLMux.DD5.1.HEVC.mkv
Group: {Series=The Idol} => [The.Idol.2023.S01E05.Jocelyn.Forever.ITA.ENG.1080p.MAX.WEB-DLMux.DD5.1.HEVC.mkv]
Rename episodes using [TheMovieDB] with [Airdate Order]
Lookup via [The Idol] []
Fetching episode data for [The Idol]
└─ 5 episodes
[MOVE] from [/volume1/ITALIAN/The Idol - Stagione 1 (2023) - 1080P-HEVC - TVDBID-405882/The.Idol.2023.S01E05.Jocelyn.Forever.ITA.ENG.1080p.MAX.WEB-DLMux.DD5.1.HEVC.mkv] to [/archive/italian/TVS/The Idol/Season 01/The Idol - S01E05 - Jocelyn Forever.mkv]
Processed 1 file
Clean clutter files and empty folders
Keep /volume1/ITALIAN/holder (not clutter)
Delete /volume1/ITALIAN/The Idol - Stagione 1 (2023) - 1080P-HEVC - TVDBID-405882
Keep /volume1/ITALIAN (root folder)
Done ヾ(@⌒ー⌒@)ノ
Run script [fn:amc] at [Tue Sep 12 20:49:20 GMT 2023]
Any suggestion ?
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Getting Media ID from Path

Post by rednoah »

:idea: FileBot uses TheMovieDB/TV by default. The TVDBID-405882 marker in the file is used for series lookup on your chosen database which is TheMovieDB/TV unless otherwise specified. TheMovieDB/TV does support series lookup via external TheTVDB ID, so no problem there.


:arrow: Please read the Change database for each content type section of the amc script manual if you want to configure a different database:

Shell: Select all

--def movieDB=TheMovieDB seriesDB=TheTVDB animeDB=TheTVDB
:idea: Please read the FAQ and How to Request Help.
Post Reply