Board index Scripting and Automation Automated Media Center

Automated Media Center

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc

Post 01 Aug 2012, 13:04
rednoah User avatar
The Source

Posts: 5330
Location: Taipei

Overview:
Here's a script that you can use with µTorrent as "Run program when torrent finishes" command to automatically sort out your downloads. It'll fully automatically organize newly downloaded TV shows and movies and is smart enough to detect what is what. The script will only touch video/subtitle files and will leave everything else as is.

  1. Unrar archives
  2. Group movies and episodes and then handle them seperately (auto-detect episode-vs-movie file)
  3. Fetch subtitles and transcode to SubRip/UTF-8
  4. Copy and Organize episodes, movies and music files into folders and rename files properly
    • Movies will be sorted into {output}/Movies/Name (Year)/Name (Year) [CD123].ext
    • TV Shows will be sorted into {output}/TV Shows/Name/Season N/Name - S00E00 - Title.ext
    • Music will be sorted into {output}/Music/Artist/Album/Artist - Title.ext
  5. Fetch artwork and generate .nfo files
  6. Notify XBMC (via TCP on port 9090) and/or Plex (via HTTP on port 32400) to rescan it's video library
  7. Log into MyEpisodes and mark newly downloaded episodes as 'acquired'
  8. Send a report about newly added files per email

There's setup instructions for µTorrent, rTorrent, qBittorrent, Vuze, Deluge and Transmission and since filebot/amc is really just a cmdline tool you can probably make it work with any software that can run external programs, or just call it every once in a while with scheduler or cron.


Setup Instructions for µTorrent:
1. Install FileBot and µTorrent and if you're on 64-bit make sure you have 64-bit Java installed.

2. Setup µTorrent directories. Setup Location of Downloaded Files and make sure the path does not end with backslash.

3. Setup µTorrent to run FileBot. Paste the following command into µTorrent Run Program settings.

µTorrent -> Run Program -> Run this program when torrent finishes:
filebot -script fn:amc --output "X:/path/to/media" --log-file amc.log --action copy --conflict override -non-strict --def music=y subtitles=en artwork=y "ut_label=%L" "ut_state=%S" "ut_title=%N" "ut_kind=%K" "ut_file=%F" "ut_dir=%D"

4. Change --output to where you want to copy/organize your files. Input is provided by µTorrent variables.

5. Done! Setting this up will take less then a minute. Now wait and enjoy how things will be organized fully automatically.

6. Restart! If you have just installed FileBot or Java you may need to restart your computer to avoid PATH issues.

utorrent-directories.png
µTorrent / Preferences / Directories
utorrent-directories.png (7.53 KiB) Viewed 95094 times

utorrent-run-program.png
µTorrent / Preferences / Run Program / Run this program when torrent finishes
utorrent-run-program.png (7.06 KiB) Viewed 134320 times


Run from cmdline:
Want to try it out first? Running directly from cmdline is even easier and if you set --action test you can do a dry-run and see if everything gets matched up correctly:
filebot -script fn:amc --output "/path/to/output" --log-file amc.log --action copy -non-strict "/path/to/input" --def excludeList=amc.txt

Here's a video tutorial for that. Note that if you call this script in intervals every hour or every day you must set --def excludeList or you will get banned.


Options:
--conflict skip Never override existing files if the same media is downloaded multiple times
--conflict auto Override existing media only if new media is better
--def music=y Process music files as well
--def subtitles=en,de,fr Download subtitles for the given languages
--def artwork=y Fetch artwork/nfo
--def extras=y Generate *.url files and fetch all available backdrops
--def xbmc=host Tell the given Kodi/XBMC instance to rescan it's library
--def plex=host[:token] Tell the given Plex instance to rescan it's library. Plex Home instances require an authentication token.
--def pushover=userkey Send update notifications to your devices via Pushover
--def pushbullet=apikey Send full reports to all your PushBullet devices
--def gmail=username:password Use the following gmail account to send and receive full reports. You must use an App Password for security reasons.
--def mail=host:port:from Send email via private mail server
--def mailto=email Send report to the given email address (optional, email to yourself if not set)
--def myepisodes=username:password Use the following MyEpisodes account to mark episodes as 'acquired'
--def reportError=y Report errors via email
--def storeReport=y Save reports to local filesystem
--def skipExtract=y Do not extract archives
--def deleteAfterExtract=y Delete archives after extraction
--def clean=y Automatically remove empty folders and clutter files that may be left behind after moving the video files or temporary extracted files after copying
--def "exec=touch '{file}'" Run program on newly processed files
--def unsorted=y Move media files that cannot be identified to a separate folder
--def ignore=regex Ignore filepaths that match the given regular expression
--def minFileSize=0 Only process video files larger than the given number (in bytes). Defaults to 50 MB.
--def minLengthMS=0 Only process videos longer than the given number (in milliseconds). Defaults to 10 minutes.
--def excludeList=amc-input.txt Keep a list of previously processed files to be ignored in future runs (if you run this script on the same files repeatedly you must enable this option)


Change how files will be organized and renamed:
This script uses multiple format expressions, so the simple --format option is not supported. Instead can override the default naming schemes via --def seriesFormat, animeFormat or movieFormat but make sure you understand how cmdline arguments work since you must escape double quotes via \", dollar signs, ampersands, etc otherwise the cmdline arguments will not be passed to FileBot as you may think. Relative paths will be resolved against --output and you can use absolute paths if you want to use multiple drives.
--def "seriesFormat=X:/TV/{n}/{'S'+s}/{fn}" "animeFormat=X:/Anime/{n}/{fn}" "movieFormat=Z:/Movies/{n} {y}/{fn}" "musicFormat=Z:/Music/{n}/{fn}"

If your format contains quotes, e.g. {"-$fn"}, and you just copy and paste it, e.g. "movieFormat={"-$fn"}", the quotes in the format will mess with cmdline argument parsing. Hence you MUST ESCAPE DOUBLE QUOTES, e.g. "movieFormat={\"-$fn\"}".

:idea: I wrote the Escape Tool to help you escape arguments.


Common Problems
  • The folder paths to Location of Downloaded Files (--output) must not end with backslash \ because the value gets passed through as the %D token. So the final backslash \ will escape the double-quote " and mess up everything
  • It is recommended to use / as separator and not \ since it's also used to escape characters, e.g. --output "X:\" will mess up the cmdline in unexpected ways
  • If you have PATH issues, and it almost always is PATH issues, try using the absolute path to the executable, e.g. "C:/Program Files/Filebot/filebot.exe"
  • If you have installed 32-bit Java you will need a 32-bit FileBot install for 7zip and MediaInfo integration to work. If you're on 64-bit, then only install 64-bit Java and 64-bit FileBot.
  • You can force Movie/TV Show/Anime mode or force ignore files via labels, e.g. label as movie to force TheMovieDB, or tv to force TheTVDB, or anime to force AniDB, or other to ignore all files
  • If you use more than one XBMC/Plex you can specify multiple hosts or ips like this xbmc=host1,host2,host3,etc
  • Use --conflict fail to make the script terminate on any conflicts or failures rather than gracefully ignore these errors and continue
  • Using --action keeplink won't work because µTorrent has a known bug that prevents it from opening and seeding symbolic links
  • You must not use your download folder %D (or any other folder inside) as --output folder
  • fn:amc will automatically fetch the script and auto-update but you can also specify a local path to run your own customized script

Advanced Fine-Tuning
You can use your own --filter rules to fine-tune matching to your needs and avoid mismatches. For example, if one show is confused with another due to bad naming or missing data you can simply exclude the bad one, or if you only process recently aired episodes you could exclude any episode of any show that has been aired for more than a few days.

e.g. if you only process recently aired episodes you could add these options:
--filter "age < 7" --def "ut_label=TV"



Troubleshooting
If something is not working, first read the Notes above, then just run the command manually in new cmd window and see what happens. But you cannot just copy & paste the template command for µTorrent with all the %D, %F, etc variables into cmd. The variables have to be replaced with actual values first. You can find the actual cmdline call in the µTorrents Logger tab so you can just copy it from there. You may need to enable the Logger tab first (just right-click one of the other tabs like Info).

*** If you need help start a new thread and always include cmdline output! ***

It's always helpful to check the exact command utorrent ends up executing in the Logger tab. It'll look something like this:
[2013-03-05 20:44:24]  Executing: filebot -script fn:amc --output "D:/Organized Media" --log-file amc.log --action duplicate --conflict override -non-strict --def "ut_dir=D:\Media\Avatar" "ut_file=" "ut_kind=multi" "ut_title=Avatar" "ut_label=" "ut_state=5"



Feedback
I'd love to get some feedback on how well this script works for everybody. Should work perfectly for most, but there's always tricky cases that might not be covered yet. Also if you've set the script up to work with deluge, transmission, cron, automator, etc I'd like to hear from you. ;)


View Script:
http://filebot.net/scripts/amc.groovy
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 5330
Location: Taipei

If you're using this script with µTorrent on Windows you might find it annoying that it'll popup a cmd window stealing the focus each time.

Instead of using filebot.exe you can try using the GUI executable filebot.launcher.exe like this:
filebot.launcher -script fn:amc --output "X:/path/to/media" --log-file amc.log --action copy --conflict override -non-strict --def "ut_dir=%D" "ut_file=%F" "ut_kind=%K" "ut_title=%N" "ut_label=%L" "ut_state=%S"

:idea: If you have PATH issues you try using the absolute path to the executable. :idea:
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 5330
Location: Taipei

Since the utorrent-postprocess integration as described above only works on Windows you might be wondering how to set things up on Linux or Mac with other bittorrent clients. Here's a little tutorial how to set this up with Transmission BitTorrent Client.


This guide assumes that you have already installed the filebot console tools so make sure filebot -version works.


1. Create On Torrent Completion Script

Create a On Torrent Completion shell script transmission-postprocess that'll call filebot like this and make sure to chmod +rx it:
#!/bin/bash
filebot -script fn:amc --output "$HOME/Media" --log-file amc.log --action duplicate --conflict override -non-strict --def artwork=y "ut_dir=$TR_TORRENT_DIR/$TR_TORRENT_NAME" "ut_kind=multi" "ut_title=$TR_TORRENT_NAME"


2. Set Transmission to call our shell script on completion

Setup via GUI on Mac:
Open Transmission ► Preferences ► Transfers ► Management and set it to call our script when download completes:
Image

Setup via Terminal on Linux:
Open ~/.config/transmission/settings.json and set it to call our shell script on download complete:
"script-torrent-done-enabled": true,
"script-torrent-done-filename": "/path/to/transmission-postprocess",

Transmission Configuration Files:
https://trac.transmissionbt.com/wiki/ConfigFiles
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 5330
Location: Taipei

Here's how to set things up with the Deluge BitTorrent Client. Should work on Mac, Linux, BSD etc

Create a shell script deluge-postprocess that'll call filebot like this and make sure to chmod +rx it:
#!/bin/bash
TORRENT_ID=$1
TORRENT_NAME=$2
TORRENT_PATH=$3

filebot -script fn:amc --output "$HOME/Media" --log-file amc.log --action duplicate --conflict override -non-strict --def artwork=y "ut_dir=$TORRENT_PATH/$TORRENT_NAME" "ut_kind=multi" "ut_title=$TORRENT_NAME"

Open Deluge ► Preferences ► Plugins and activate the Execute plugin. Then go to Preferences ► Execute, add a new Torrent Complete event and set it to call our shell script:
deluge-execute.png
Deluge / Preferences / Execute / Torrent Complete
deluge-execute.png (34 KiB) Viewed 142717 times
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 5330
Location: Taipei

If you're using rTorrent on your headless machine it's just as easy. You'll just need to edit .rtorrent.rc a bit.

Create a shell script rtorrent-postprocess that'll call filebot like this and make sure to chmod +rx it:
#!/bin/bash
TORRENT_PATH=$1
TORRENT_NAME=$2
TORRENT_LABEL=$3

filebot -script fn:amc --output "$HOME/Media" --log-file amc.log --action duplicate --conflict override -non-strict --def artwork=y "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=$TORRENT_LABEL" &

Edit ~/.rtorrent.rc and add this line at the bottom:
system.method.set_key=event.download.finished,filebot_amc,"execute={/path/to/rtorrent-postprocess,$d.get_base_path=,$d.get_name=,$d.get_custom1=}"

And make sure to replace /path/to/rtorrent-postprocess with the actual path. ;)
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

rednoah User avatar
The Source

Posts: 5330
Location: Taipei

The qBittorrent setup is pretty similar to µTorrent, except there are less variables that can be passed along to the script. Just paste the following command into the Run an external program on torrent completion setting. You can find a more detailed Guide for qBittorrent here.

Options ► Downloads ► Run an external program on torrent completion:
filebot -script fn:amc --output "/path/to/media" --log-file amc.log --action copy --conflict override -non-strict --def excludeList=amc.txt music=y subtitles=en artwork=y "ut_kind=multi" "ut_dir=%f"
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image

Post 28 Oct 2014, 10:25
rednoah User avatar
The Source

Posts: 5330
Location: Taipei

If you're have any issues...

1. Make a NEW thread
2. Include log output
3. Include full paths when reporting mismatches

thx :ugeek:
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image


Return to Scripting and Automation