2012-07-30 12:59:09 -04:00
|
|
|
// filebot -script "fn:utorrent-postprocess" --output "X:/media" --action copy --conflict override --def subtitles=true artwork=true xbmc=localhost plex=10.0.0.1 gmail=username:password "ut_dir=%D" "ut_file=%F" "ut_kind=%K" "ut_title=%N" "ut_label=%L" "ut_state=%S"
|
2012-04-28 14:15:15 -04:00
|
|
|
def input = []
|
2012-07-08 02:29:07 -04:00
|
|
|
def failOnError = _args.conflict == 'fail'
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-04-28 14:15:15 -04:00
|
|
|
// print input parameters
|
2012-07-30 12:59:09 -04:00
|
|
|
_args.bindings?.each{ _log.finest("Parameter: $it.key = $it.value") }
|
2012-10-18 12:34:38 -04:00
|
|
|
args.each{ _log.finest("Argument: $it") }
|
2012-04-28 14:15:15 -04:00
|
|
|
|
2012-12-10 13:07:03 -05:00
|
|
|
// check user-defined pre-condition
|
|
|
|
if (tryQuietly{ ut_state != ut_state_allow }) {
|
|
|
|
throw new Exception("Invalid state: ut_state = $ut_state (expected $ut_state_allow)")
|
|
|
|
}
|
|
|
|
|
2012-08-01 02:04:47 -04:00
|
|
|
// enable/disable features as specified via --def parameters
|
2012-07-30 06:01:03 -04:00
|
|
|
def subtitles = tryQuietly{ subtitles.toBoolean() }
|
|
|
|
def artwork = tryQuietly{ artwork.toBoolean() }
|
2012-12-16 08:26:39 -05:00
|
|
|
def backdrops = tryQuietly{ backdrops.toBoolean() }
|
2013-01-04 23:25:21 -05:00
|
|
|
def clean = tryQuietly{ clean.toBoolean() }
|
2012-07-30 06:01:03 -04:00
|
|
|
|
|
|
|
// array of xbmc/plex hosts
|
2012-08-01 02:04:47 -04:00
|
|
|
def xbmc = tryQuietly{ xbmc.split(/[ ,|]+/) }
|
|
|
|
def plex = tryQuietly{ plex.split(/[ ,|]+/) }
|
2012-07-30 06:01:03 -04:00
|
|
|
|
2012-12-09 08:37:27 -05:00
|
|
|
// myepisodes updates and email notifications
|
|
|
|
def myepisodes = tryQuietly { myepisodes.split(':', 2) }
|
2012-07-30 12:59:09 -04:00
|
|
|
def gmail = tryQuietly{ gmail.split(':', 2) }
|
|
|
|
|
2012-10-14 01:19:35 -04:00
|
|
|
|
|
|
|
// series/anime/movie format expressions
|
|
|
|
def format = [
|
2012-11-26 04:11:14 -05:00
|
|
|
tvs: tryQuietly{ seriesFormat } ?: '''TV Shows/{n}/{episode.special ? "Special" : "Season "+s}/{n} - {episode.special ? "S00E"+special.pad(2) : s00e00} - {t}''',
|
|
|
|
anime: tryQuietly{ animeFormat } ?: '''Anime/{n}/{n} - {sxe} - {t}''',
|
2013-01-10 14:30:16 -05:00
|
|
|
mov: tryQuietly{ movieFormat } ?: '''Movies/{n} ({y})/{n} ({y}){" CD$pi"}{".$lang"}''',
|
|
|
|
music: tryQuietly{ musicFormat } ?: '''Music/{n}/{album}/{n} - {t}'''
|
2012-10-14 01:19:35 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2012-07-30 12:59:09 -04:00
|
|
|
// force movie/series/anime logic
|
|
|
|
def forceMovie(f) {
|
|
|
|
tryQuietly{ ut_label } =~ /^(?i:Movie|Couch.Potato)/
|
|
|
|
}
|
|
|
|
|
|
|
|
def forceSeries(f) {
|
2012-11-18 13:26:37 -05:00
|
|
|
parseEpisodeNumber(f) || parseDate(f) || tryQuietly{ ut_label } =~ /^(?i:TV|Kids.Shows)/
|
2012-07-30 12:59:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def forceAnime(f) {
|
|
|
|
tryQuietly{ ut_label } =~ /^(?i:Anime)/
|
|
|
|
}
|
|
|
|
|
|
|
|
def forceIgnore(f) {
|
2013-01-11 05:03:27 -05:00
|
|
|
tryQuietly{ ut_label } =~ /^(?i:Ebook|other|ignore)/
|
2012-07-30 12:59:09 -04:00
|
|
|
}
|
|
|
|
|
2012-07-30 06:01:03 -04:00
|
|
|
|
2012-10-30 05:48:34 -04:00
|
|
|
// specify how to resolve input folders, e.g. grab files from all folders except disk folders
|
|
|
|
def resolveInput(f) {
|
|
|
|
if (f.isDirectory() && !f.isDisk())
|
|
|
|
return f.listFiles().toList().findResults{ resolveInput(it) }
|
|
|
|
else
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2012-07-30 06:01:03 -04:00
|
|
|
// collect input fileset as specified by the given --def parameters
|
2012-10-18 12:34:38 -04:00
|
|
|
if (args.empty) {
|
2012-11-09 06:19:35 -05:00
|
|
|
// assume we're called with utorrent parameters (account for older and newer versions of uTorrents)
|
|
|
|
if (ut_kind == 'single' || (ut_kind != 'multi' && ut_dir && ut_file)) {
|
2012-07-13 10:33:39 -04:00
|
|
|
input += new File(ut_dir, ut_file) // single-file torrent
|
2012-07-16 08:19:13 -04:00
|
|
|
} else {
|
2012-10-30 05:48:34 -04:00
|
|
|
input += resolveInput(ut_dir as File) // multi-file torrent
|
2012-07-13 10:33:39 -04:00
|
|
|
}
|
2012-04-28 14:15:15 -04:00
|
|
|
} else {
|
2012-07-13 10:33:39 -04:00
|
|
|
// assume we're called normally with arguments
|
2012-10-30 05:48:34 -04:00
|
|
|
input += args.findResults{ resolveInput(it) }
|
2012-04-28 14:15:15 -04:00
|
|
|
}
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-07-31 12:17:15 -04:00
|
|
|
|
2012-10-30 05:48:34 -04:00
|
|
|
// flatten nested file structure
|
|
|
|
input = input.flatten()
|
|
|
|
|
2012-07-31 12:17:15 -04:00
|
|
|
// extract archives (zip, rar, etc) that contain at least one video file
|
2013-01-10 14:30:16 -05:00
|
|
|
input += extract(file: input.findAll{ it.isArchive() }, output: null, conflict: 'override', filter: { it.isVideo() || it.isAudio() }, forceExtractAll: true)
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-08-13 04:03:11 -04:00
|
|
|
// sanitize input
|
|
|
|
input = input.findAll{ it?.exists() }.collect{ it.canonicalFile }.unique()
|
|
|
|
|
2012-04-26 07:25:58 -04:00
|
|
|
// process only media files
|
2013-01-10 14:30:16 -05:00
|
|
|
input = input.findAll{ it.isVideo() || it.isSubtitle() || it.isDisk() || it.isAudio() }
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-05-31 08:10:50 -04:00
|
|
|
// ignore clutter files
|
2012-12-02 00:15:50 -05:00
|
|
|
input = input.findAll{ !(it.path =~ /\b(?i:sample|trailer|extras|deleted.scenes|music.video|scrapbook|behind.the.scenes)\b/) }
|
2012-05-31 08:10:50 -04:00
|
|
|
|
2012-04-28 14:15:15 -04:00
|
|
|
// print input fileset
|
2012-07-30 12:59:09 -04:00
|
|
|
input.each{ f -> _log.finest("Input: $f") }
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-07-28 06:21:30 -04:00
|
|
|
// artwork/nfo utility
|
2012-11-09 06:19:35 -05:00
|
|
|
include('fn:lib/htpc')
|
2012-04-29 01:28:38 -04:00
|
|
|
|
2012-04-26 07:25:58 -04:00
|
|
|
// group episodes/movies and rename according to XBMC standards
|
2012-07-08 08:01:19 -04:00
|
|
|
def groups = input.groupBy{ f ->
|
2012-07-30 12:59:09 -04:00
|
|
|
// skip auto-detection if possible
|
|
|
|
if (forceIgnore(f))
|
|
|
|
return []
|
2013-01-10 14:30:16 -05:00
|
|
|
if (f.isAudio()) // PROCESS MUSIC FOLDER BY FOLDER
|
|
|
|
return [music: f.dir.name]
|
2012-07-30 12:59:09 -04:00
|
|
|
if (forceMovie(f))
|
2012-08-09 01:06:28 -04:00
|
|
|
return [mov: detectMovie(f, false)]
|
2012-07-30 12:59:09 -04:00
|
|
|
if (forceSeries(f))
|
2012-08-09 01:06:28 -04:00
|
|
|
return [tvs: detectSeriesName(f) ?: detectSeriesName(f.dir.listFiles{ it.isVideo() })]
|
2012-07-30 12:59:09 -04:00
|
|
|
if (forceAnime(f))
|
2012-08-09 01:06:28 -04:00
|
|
|
return [anime: detectSeriesName(f) ?: detectSeriesName(f.dir.listFiles{ it.isVideo() })]
|
2012-07-30 12:59:09 -04:00
|
|
|
|
|
|
|
|
2012-07-08 08:01:19 -04:00
|
|
|
def tvs = detectSeriesName(f)
|
2012-07-30 12:59:09 -04:00
|
|
|
def mov = detectMovie(f, false)
|
2012-07-08 08:01:19 -04:00
|
|
|
println "$f.name [series: $tvs, movie: $mov]"
|
2012-04-26 07:25:58 -04:00
|
|
|
|
|
|
|
// DECIDE EPISODE VS MOVIE (IF NOT CLEAR)
|
|
|
|
if (tvs && mov) {
|
2012-07-21 10:32:55 -04:00
|
|
|
def norm = { s -> s.lower().space(' ') }
|
2012-07-26 22:14:49 -04:00
|
|
|
def dn = norm(guessMovieFolder(f)?.name ?: '')
|
2012-07-21 10:32:55 -04:00
|
|
|
def fn = norm(f.nameWithoutExtension)
|
|
|
|
def sn = norm(tvs)
|
|
|
|
def mn = norm(mov.name)
|
|
|
|
|
|
|
|
// S00E00 | 2012.07.21 | One Piece 217 | Firefly - Serenity | [Taken 1, Taken 2, Taken 3, Taken 4, ..., Taken 10]
|
|
|
|
if (parseEpisodeNumber(fn, true) || parseDate(fn) || (fn =~ sn && parseEpisodeNumber(fn.after(sn), false)) || fn.after(sn) =~ / - .+/ || f.dir.listFiles{ it.isVideo() && norm(it.name) =~ sn && it.name =~ /\b\d{1,3}\b/}.size() >= 10) {
|
2012-04-26 07:25:58 -04:00
|
|
|
println "Exclude Movie: $mov"
|
|
|
|
mov = null
|
2012-07-26 22:03:44 -04:00
|
|
|
} else if ((detectMovie(f, true) && [dn, fn].find{ it =~ /(19|20)\d{2}/ }) || [dn, fn].find{ it =~ mn && !(it.after(mn) =~ /\b\d{1,3}\b/) }) {
|
2012-07-08 09:13:18 -04:00
|
|
|
println "Exclude Series: $tvs"
|
|
|
|
tvs = null
|
2012-04-26 07:25:58 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-08 02:29:07 -04:00
|
|
|
|
|
|
|
// CHECK CONFLICT
|
|
|
|
if (((mov && tvs) || (!mov && !tvs)) && failOnError) {
|
|
|
|
throw new Exception("Media detection failed")
|
|
|
|
}
|
|
|
|
|
2012-07-30 12:59:09 -04:00
|
|
|
return [tvs: tvs, mov: mov, anime: null]
|
2012-04-26 07:25:58 -04:00
|
|
|
}
|
|
|
|
|
2012-07-30 12:59:09 -04:00
|
|
|
// log movie/series/anime detection results
|
2012-07-30 20:00:46 -04:00
|
|
|
groups.each{ group, files -> _log.finest("Group: $group => ${files*.name}") }
|
2012-07-30 12:59:09 -04:00
|
|
|
|
|
|
|
// process each batch
|
2012-04-26 07:25:58 -04:00
|
|
|
groups.each{ group, files ->
|
2012-04-29 01:28:38 -04:00
|
|
|
// fetch subtitles
|
2012-07-30 06:01:03 -04:00
|
|
|
if (subtitles) {
|
|
|
|
files += getMissingSubtitles(file:files, output:"srt", encoding:"utf-8")
|
|
|
|
}
|
2012-04-29 01:28:38 -04:00
|
|
|
|
2012-04-26 07:25:58 -04:00
|
|
|
// EPISODE MODE
|
2012-07-30 12:59:09 -04:00
|
|
|
if ((group.tvs || group.anime) && !group.mov) {
|
|
|
|
// choose series / anime config
|
2012-11-23 19:45:47 -05:00
|
|
|
def config = group.tvs ? [name:group.tvs, format:format.tvs, db:'TheTVDB', seasonFolder:true ]
|
2012-11-24 16:56:09 -05:00
|
|
|
: [name:group.anime, format:format.anime, db:'AniDB', seasonFolder:false]
|
2012-07-30 12:59:09 -04:00
|
|
|
def dest = rename(file: files, format: config.format, db: config.db)
|
2012-07-30 06:01:03 -04:00
|
|
|
if (dest && artwork) {
|
2012-07-08 02:29:07 -04:00
|
|
|
dest.mapByFolder().each{ dir, fs ->
|
2012-05-31 08:10:50 -04:00
|
|
|
println "Fetching artwork for $dir from TheTVDB"
|
2012-07-27 02:29:28 -04:00
|
|
|
def sxe = fs.findResult{ eps -> parseEpisodeNumber(eps) }
|
2012-07-30 12:59:09 -04:00
|
|
|
def options = TheTVDB.search(config.name)
|
2012-05-31 08:10:50 -04:00
|
|
|
if (options.isEmpty()) {
|
2012-07-30 12:59:09 -04:00
|
|
|
println "TV Series not found: $config.name"
|
2012-05-31 08:10:50 -04:00
|
|
|
return
|
|
|
|
}
|
2012-07-30 12:59:09 -04:00
|
|
|
options = options.sortBySimilarity(config.name, { s -> s.name })
|
2012-11-23 19:45:47 -05:00
|
|
|
fetchSeriesArtworkAndNfo(config.seasonFolder ? dir.dir : dir, dir, options[0], sxe && sxe.season > 0 ? sxe.season : 1)
|
2012-04-28 14:15:15 -04:00
|
|
|
}
|
|
|
|
}
|
2012-07-30 06:01:03 -04:00
|
|
|
if (dest == null && failOnError) {
|
2012-07-30 12:59:09 -04:00
|
|
|
throw new Exception("Failed to rename series: $config.name")
|
2012-07-30 06:01:03 -04:00
|
|
|
}
|
2012-04-26 07:25:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MOVIE MODE
|
2012-07-30 12:59:09 -04:00
|
|
|
if (group.mov && !group.tvs && !group.anime) {
|
2012-10-14 01:19:35 -04:00
|
|
|
def dest = rename(file:files, format:format.mov, db:'TheMovieDB')
|
2012-07-30 06:01:03 -04:00
|
|
|
if (dest && artwork) {
|
2012-07-08 02:29:07 -04:00
|
|
|
dest.mapByFolder().each{ dir, fs ->
|
2012-05-31 08:10:50 -04:00
|
|
|
println "Fetching artwork for $dir from TheMovieDB"
|
2012-12-16 08:26:39 -05:00
|
|
|
fetchMovieArtworkAndNfo(dir, group.mov, fs.findAll{ it.isVideo() }.sort{ it.length() }.reverse().findResult{ it }, backdrops)
|
2012-05-31 08:10:50 -04:00
|
|
|
}
|
2012-04-28 14:15:15 -04:00
|
|
|
}
|
2012-07-30 06:01:03 -04:00
|
|
|
if (dest == null && failOnError) {
|
|
|
|
throw new Exception("Failed to rename movie: $group.mov")
|
|
|
|
}
|
2012-04-26 07:25:58 -04:00
|
|
|
}
|
2013-01-10 14:30:16 -05:00
|
|
|
|
|
|
|
// MUSIC MODE
|
|
|
|
if (group.music) {
|
|
|
|
def dest = rename(file:files, format:format.music, db:'AcoustID')
|
|
|
|
if (dest == null && failOnError) {
|
|
|
|
throw new Exception("Failed to rename music: $group.music")
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 07:25:58 -04:00
|
|
|
}
|
|
|
|
|
2013-01-05 23:03:59 -05:00
|
|
|
// skip notifications if nothing was renamed anyway
|
|
|
|
if (getRenameLog().isEmpty()) {
|
|
|
|
return
|
|
|
|
}
|
2012-04-26 07:25:58 -04:00
|
|
|
|
2012-07-28 06:21:30 -04:00
|
|
|
// make xbmc or plex scan for new content
|
2012-07-30 06:01:03 -04:00
|
|
|
xbmc?.each{
|
2012-05-01 12:33:55 -04:00
|
|
|
println "Notify XBMC: $it"
|
2012-04-29 01:28:38 -04:00
|
|
|
invokeScanVideoLibrary(it)
|
2012-07-30 06:01:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
plex?.each{
|
2012-07-28 06:21:30 -04:00
|
|
|
println "Notify Plex: $it"
|
|
|
|
refreshPlexLibrary(it)
|
2012-04-28 14:15:15 -04:00
|
|
|
}
|
2012-07-30 12:59:09 -04:00
|
|
|
|
2012-12-07 11:47:54 -05:00
|
|
|
// mark episodes as 'acquired'
|
|
|
|
if (myepisodes) {
|
2013-01-04 23:25:21 -05:00
|
|
|
println 'Update MyEpisodes'
|
2012-12-08 23:28:49 -05:00
|
|
|
include('fn:update-mes', [login:myepisodes.join(':'), addshows:false], getRenameLog().values())
|
2012-12-07 11:47:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-30 12:59:09 -04:00
|
|
|
// send status email
|
2013-01-05 23:03:59 -05:00
|
|
|
if (gmail) {
|
2012-07-30 12:59:09 -04:00
|
|
|
// ant/mail utility
|
2012-11-09 06:19:35 -05:00
|
|
|
include('fn:lib/ant')
|
2012-07-30 12:59:09 -04:00
|
|
|
|
|
|
|
// send html mail
|
|
|
|
def renameLog = getRenameLog()
|
2012-10-29 10:46:25 -04:00
|
|
|
def emailTitle = tryQuietly { ut_title } ?: input*.dir.name.unique()
|
2012-07-30 12:59:09 -04:00
|
|
|
|
|
|
|
sendGmail(
|
2012-10-29 05:56:17 -04:00
|
|
|
subject: "[FileBot] ${emailTitle}",
|
2012-07-30 12:59:09 -04:00
|
|
|
message: XML {
|
|
|
|
html {
|
|
|
|
body {
|
2012-10-29 05:56:17 -04:00
|
|
|
p("FileBot finished processing ${emailTitle} (${renameLog.size()} files).");
|
2012-07-30 12:59:09 -04:00
|
|
|
hr(); table {
|
|
|
|
th("Parameter"); th("Value")
|
|
|
|
_args.bindings.findAll{ param -> param.key =~ /^ut_/ }.each{ param ->
|
|
|
|
tr { [param.key, param.value].each{ td(it)} }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hr(); table {
|
|
|
|
th("Original Name"); th("New Name"); th("New Location")
|
|
|
|
renameLog.each{ from, to ->
|
2012-08-12 00:38:14 -04:00
|
|
|
tr { [from.name, to.name, to.parent].each{ cell -> td{ nobr{ code(cell) } } } }
|
2012-07-30 12:59:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hr(); small("// Generated by ${net.sourceforge.filebot.Settings.applicationIdentifier} on ${new Date().dateString} at ${new Date().timeString}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
messagemimetype: "text/html",
|
2012-07-31 03:46:33 -04:00
|
|
|
to: tryQuietly{ mailto } ?: gmail[0] + '@gmail.com', // mail to self by default
|
2012-07-30 12:59:09 -04:00
|
|
|
user: gmail[0], password: gmail[1]
|
|
|
|
)
|
|
|
|
}
|
2013-01-04 23:25:21 -05:00
|
|
|
|
|
|
|
// clean empty folders, clutter files, etc after move
|
|
|
|
if (clean) {
|
|
|
|
println 'Clean clutter files and empty folders'
|
|
|
|
include('fn:cleaner', [:], !args.empty ? args : ut_kind == 'multi' && ut_dir ? [ut_dir as File] : [])
|
|
|
|
}
|