hmm it's been a while since i made the script.. any idea what makes this script broken in newer versions ?
Code: Select all
def override = _args.conflict == 'override'
import static groovy.json.StringEscapeUtils.*
/**
* XBMC helper functions
*/
def scanVideoLibrary(host, port) {
tryLogCatch {
telnet(host, port) { writer, reader ->
writer.println("""{"jsonrpc":"2.0","method":"VideoLibrary.Scan","id":1}""")
}
}
}
def showNotification(host, port, title, message, image) {
tryLogCatch {
telnet(host, port) { writer, reader ->
writer.println("""{"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"${escapeJavaScript(title)}","message":"${escapeJavaScript(message)}", "image":"${escapeJavaScript(image)}"},"id":1}""")
}
}
}
/**
* Plex helpers
*/
/**
* TheTVDB artwork/nfo helpers
*/
def fetchSeriesBanner(outputFile, seriesId, bannerType, bannerType2, season, override, locale) {
if (outputFile.exists() && !override) {
log.finest "Banner already exists: $outputFile"
return outputFile
}
// select and fetch banner
def artwork = TheTVDB.getArtwork(seriesId, bannerType, locale)
def banner = [locale.language, null].findResult { lang -> artwork.find{ it.matches(bannerType2, season, lang) } }
if (banner == null) {
log.finest "Banner not found: $outputFile / $bannerType:$bannerType2"
return null
}
log.finest "Fetching $outputFile => $banner"
return banner.url.saveAs(outputFile)
}
def fetchSeriesFanart(outputFile, seriesId, type, season, override, locale) {
if (outputFile.exists() && !override) {
log.finest "Fanart already exists: $outputFile"
return outputFile
}
def artwork = FanartTV.getArtwork(seriesId, "tv", locale)
def fanart = [locale.language, null].findResult{ lang -> artwork.find{ it.matches(type, season, lang) } }
if (fanart == null) {
log.finest "Fanart not found: $outputFile / $type"
return null
}
log.finest "Fetching $outputFile => $fanart"
return fanart.url.saveAs(outputFile)
}
def fetchSeriesNfo(outputFile, i, locale) {
log.fine "Generate Series NFO: $i.name [$i.id]"
def xml = XML {
tvshow {
title(i.name)
sorttitle([i.name, i.startDate as String].findAll{ it?.length() > 0 }.findResults{ it.sortName('$2') }.join(' :: '))
year(i.startDate?.year)
rating(i.rating)
votes(i.ratingCount)
plot(i.overview)
runtime(i.runtime)
mpaa(i.certification)
id(i.id)
i.genres.each{
genre(it)
}
thumb(i.bannerUrl)
premiered(i.startDate)
status(i.status)
studio(i.network)
tvdb(id:i.id, "https://www.thetvdb.com/?tab=series&id=${i.id}")
/** Kodi requires an <episodeguide> element with a TheTVDB API (v1) Series Record XML URL **/
episodeguide {
url(cache:"${i.id}.xml", "https://www.thetvdb.com/api/1D62F2F90030C444/series/${i.id}/all/${locale.language}.zip")
}
}
}
xml.saveAs(outputFile)
}
def fetchSeriesArtworkAndNfo(seriesDir, seasonDir, seriesId, season, override = false, locale = Locale.ENGLISH) {
tryLogCatch {
// fetch nfo
def seriesInfo = TheTVDB.getSeriesInfo(seriesId, locale)
fetchSeriesNfo(seriesDir.resolve('tvshow.nfo'), seriesInfo, locale)
// fetch series banner, fanart, posters, etc
['680x1000', null].findResult{ fetchSeriesBanner(seriesDir.resolve('poster.jpg'), seriesId, 'poster', it, null, override, locale) }
['graphical', null].findResult{ fetchSeriesBanner(seriesDir.resolve('banner.jpg'), seriesId, 'series', it, null, override, locale) }
// fetch highest resolution fanart
['1920x1080', '1280x720', null].findResult{ fetchSeriesBanner(seriesDir.resolve('fanart.jpg'), seriesId, 'fanart', it, null, override, locale) }
// fetch season banners
if (seasonDir != seriesDir) {
fetchSeriesBanner(seasonDir.resolve('poster.jpg'), seriesId, 'season', 'season', season, override, locale)
fetchSeriesBanner(seasonDir.resolve('banner.jpg'), seriesId, 'seasonwide', 'seasonwide', season, override, locale)
// folder image (resuse series poster if possible)
copyIfPossible(seasonDir.resolve('poster.jpg'), seasonDir.resolve('folder.jpg'))
}
// fetch fanart
['hdclearart', 'clearart'].findResult{ type -> fetchSeriesFanart(seriesDir.resolve('clearart.png'), seriesId, type, null, override, locale) }
['hdtvlogo', 'clearlogo'].findResult{ type -> fetchSeriesFanart(seriesDir.resolve('logo.png'), seriesId, type, null, override, locale) }
fetchSeriesFanart(seriesDir.resolve('landscape.jpg'), seriesId, 'tvthumb', null, override, locale)
// fetch season fanart
if (seasonDir != seriesDir) {
fetchSeriesFanart(seasonDir.resolve('landscape.jpg'), seriesId, 'seasonthumb', season, override, locale)
}
// folder image (resuse series poster if possible)
copyIfPossible(seriesDir.resolve('poster.jpg'), seriesDir.resolve('folder.jpg'))
}
}
/**
* TheMovieDB artwork/nfo helpers
*/
def copyIfPossible(File src, File dst) {
if (src.exists() && !dst.exists()) {
src.copyAs(dst)
}
}
args.eachMediaFolder{ dir ->
// fetch only missing artwork by default
if (!override && dir.hasFile{it.name == 'banner.jpg'}) {
println "Skipping $dir"
return
}
def videos = dir.listFiles{ it.isVideo() }
def query = _args.query ?: detectSeriesName(videos)
def sxe = videos.findResult{ parseEpisodeNumber(it) }
if (query == null) {
query = dir.dir.hasFile{ it.name =~ /Season/ && it.isDirectory() } ? dir.dir.name : dir.name
}
println "$dir => Search by $query"
def options = TheTVDB.search(query, _args.locale)
if (options.isEmpty()) {
println "TV Series not found: $query"
return
}
// sort by relevance
options = options.sortBySimilarity(query, { it.name })
// auto-select series
def series = options[0]
// maybe require user input
if (options.size() != 1 && !_args.nonStrict && !java.awt.GraphicsEnvironment.headless) {
series = javax.swing.JOptionPane.showInputDialog(null, 'Please select TV Show:', dir.path, 3, null, options.toArray(), series)
if (series == null) return
}
// auto-detect structure
def seriesDir = [dir.dir, dir].sortBySimilarity(series.name, { it.name })[0]
def season = sxe && sxe.season > 0 ? sxe.season : 1
println "$dir => $series"
fetchSeriesArtworkAndNfo(seriesDir, dir, series.id, season, false, _args.locale ?: Locale.ENGLISH)
}