From de73d1ac31bf68078a10951ae7cfa13061cf4a21 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 28 Apr 2012 18:15:15 +0000 Subject: [PATCH] * added fetch *.artwork function to the utorrent-postprocess script --- website/scripts/artwork.tvdb.groovy | 2 +- website/scripts/utorrent-postprocess.groovy | 166 ++++++++++++++++++-- 2 files changed, 157 insertions(+), 11 deletions(-) diff --git a/website/scripts/artwork.tvdb.groovy b/website/scripts/artwork.tvdb.groovy index 441832be..95627767 100644 --- a/website/scripts/artwork.tvdb.groovy +++ b/website/scripts/artwork.tvdb.groovy @@ -19,7 +19,7 @@ def fetchBanner(outputFile, series, bannerType, bannerType2 = null, season = nul def fetchNfo(outputFile, series) { def info = TheTVDB.getSeriesInfo(series, _args.locale) - println info + println info info.applyXmlTemplate(''' $name $firstAired.year diff --git a/website/scripts/utorrent-postprocess.groovy b/website/scripts/utorrent-postprocess.groovy index c80388e5..e184d8d7 100644 --- a/website/scripts/utorrent-postprocess.groovy +++ b/website/scripts/utorrent-postprocess.groovy @@ -1,8 +1,14 @@ -// filebot -script "http://filebot.sf.net/scripts/utorrent-postprocess.groovy" "%D\%N" --output "X:/media" --action copy --conflict override -non-strict -trust-script -Xxbmc=localhost -println "Input: $args" -println "Parameters: $_args.parameters" +// filebot -script "http://filebot.sf.net/scripts/utorrent-postprocess.groovy" --output "X:/media" --action copy --conflict override -non-strict -trust-script -Xxbmc=localhost "-Xut_dir=%D" "-Xut_file=%F" "-Xut_label=%L" "-Xut_state=%S" "-Xut_kind=%K" +def input = [] -def input = args.getFiles() +// print input parameters +_args.parameters.each{ k, v -> println "Parameter: $k = $v" } + +if (ut_kind == "multi") { + input += new File(ut_dir).getFiles() // multi-file torrent +} else { + input += new File(ut_dir, ut_file) // single-file torrent +} // extract archives if necessary input += extract(file:input) @@ -10,9 +16,11 @@ input += extract(file:input) // process only media files input = input.findAll{ it.isVideo() || it.isSubtitle() } +// print input fileset +input.each{ println "Input: $it" } // group episodes/movies and rename according to XBMC standards -def groups = input.groupBy { +def groups = input.groupBy{ def tvs = detectSeriesName(it) def mov = detectMovie(it, false) println "$it.name [series: $tvs, movie: $mov]" @@ -33,12 +41,37 @@ def groups = input.groupBy { groups.each{ group, files -> // EPISODE MODE if (group.tvs && !group.mov) { - return rename(file:files, format:'TV Shows/{n}/{episode.special ? "Special" : "Season "+s}/{n} - {episode.special ? "S00E"+special.pad(2) : s00e00} - {t}', db:'TheTVDB') + def output = rename(file:files, format:'TV Shows/{n}{episode.special ? "/Special" : "/Season "+s}/{n} - {episode.special ? "S00E"+special.pad(2) : s00e00} - {t}', db:'TheTVDB') + + output*.dir.unique().each{ dir -> + println "Fetching artwork for $dir from TheTVDB" + def query = group.tvs + def sxe = output.findResult{ parseEpisodeNumber(it) } + def options = TheTVDB.search(query, _args.locale) + if (options.isEmpty()) { + println "TV Series not found: $query" + return + } + options = options.sortBySimilarity(query, { it.name }) + def series = options[0] + def seriesDir = [dir.dir, dir].sortBySimilarity(series.name, { it.name })[0] + def season = sxe && sxe.season > 0 ? sxe.season : 1 + fetchSeriesBannersAndNfo(seriesDir, dir, series, season) + } } // MOVIE MODE if (group.mov && !group.tvs) { - return rename(file:files, format:'Movies/{n} ({y}){" CD$pi"}{".$lang"}', db:'TheMovieDB') + def output = rename(file:files, format:'Movies/{n} ({y})/{n} ({y}){" CD$pi"}', db:'TheMovieDB') + + output*.dir.unique().each{ dir -> + println "Fetching artwork for $dir from TheMovieDB" + try { + fetchMovieArtworkAndNfo(dir, group.mov) + } catch(e) { + println "${e.class.simpleName}: ${e.message}" + } + } } } @@ -46,10 +79,123 @@ groups.each{ group, files -> // make XBMC scan for new content try { - telnet(xbmc, 9090) { writer, reader -> - def msg = '{"id":1,"method":"VideoLibrary.Scan","params":[],"jsonrpc":"2.0"}' - writer.println(msg) + xbmc.split(/[\s,|]+/).each{ + println "Notify XBMC: $it" + telnet(it, 9090) { writer, reader -> + def msg = '{"id":1,"method":"VideoLibrary.Scan","params":[],"jsonrpc":"2.0"}' + writer.println(msg) + } } } catch(e) { println "${e.class.simpleName}: ${e.message}" } +// END OF SCRIPT + + + + +// FUNCTIONS for TMDB and TVDB artwork/nfo +def fetchBanner(outputFile, series, bannerType, bannerType2 = null, season = null) { + // select and fetch banner + def banner = [_args.locale.language, null].findResult { TheTVDB.getBanner(series, [BannerType:bannerType, BannerType2:bannerType2, Season:season, Language:it]) } + if (banner == null) { + println "Banner not found: $outputFile / $bannerType:$bannerType2" + return null + } + println "Fetching $outputFile => $banner" + return banner.url.saveAs(outputFile) +} +def fetchSeriesNfo(outputFile, series) { + def info = TheTVDB.getSeriesInfo(series, _args.locale) + info.applyXmlTemplate(''' + $name + $firstAired.year + + -1 + + + -1 + -1 + $rating + $ratingCount + + $overview + + $runtime + $contentRating + + + $id + http://www.thetvdb.com/api/1D62F2F90030C444/series/${id}/all/''' + _args.locale.language + '''.zip + ${!genres.empty ? genres[0] : ''} + + + + $bannerUrl + $firstAired + $status + $network + + actors.each { + + $it + + + } + + + ''') + .replaceAll(/\t|\r|\n/, '') // xbmc can't handle leading/trailing whitespace properly + .saveAs(outputFile) +} +def fetchSeriesBannersAndNfo(seriesDir, seasonDir, series, season) { + println "Fetch nfo and banners for $series / Season $season" + // fetch nfo + fetchSeriesNfo(seriesDir['tvshow.nfo'], series) + // fetch series banner, fanart, posters, etc + ["680x1000", null].findResult{ fetchBanner(seriesDir['folder.jpg'], series, "poster", it) } + ["graphical", null].findResult{ fetchBanner(seriesDir['banner.jpg'], series, "series", it) } + // fetch highest resolution fanart + ["1920x1080", "1280x720", null].findResult{ fetchBanner(seriesDir["fanart.jpg"], series, "fanart", it) } + // fetch season banners + if (seasonDir != seriesDir) { + fetchBanner(seasonDir["folder.jpg"], series, "season", "season", season) + fetchBanner(seasonDir["banner.jpg"], series, "season", "seasonwide", season) + } +} + +def fetchArtwork(outputFile, movieInfo, artworkType, artworkSize) { + // select and fetch artwork + def artwork = movieInfo.images.find { it.type == artworkType && it.size == artworkSize } + if (artwork == null) { + println "Artwork not found: $outputFile" + return null + } + println "Fetching $outputFile => $artwork" + return artwork.url.saveAs(outputFile) +} +def fetchMovieNfo(outputFile, movieInfo) { + movieInfo.applyXmlTemplate(''' + $name + $released.year + $rating + $votes + $overview + $runtime + $certification + ${!genres.empty ? genres[0] : ''} + tt${imdbId.pad(7)} + + ''') + .replaceAll(/\t|\r|\n/, '') // xbmc can't handle leading/trailing whitespace properly + .saveAs(outputFile) +} +def fetchMovieArtworkAndNfo(movieDir, movie) { + println "Fetch nfo and artwork for $movie" + def movieInfo = TheMovieDB.getMovieInfo(movie, Locale.ENGLISH) + // fetch nfo + fetchMovieNfo(movieDir['movie.nfo'], movieInfo) + // fetch series banner, fanart, posters, etc + fetchArtwork(movieDir['folder.jpg'], movieInfo, 'poster', 'original') + fetchArtwork(movieDir['backdrop.jpg'], movieInfo, 'backdrop', 'original') +}