1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-11 20:05:04 -05:00
filebot/build-data/BuildArtwork.groovy

111 lines
2.5 KiB
Groovy
Raw Normal View History

#!/usr/bin/env filebot -script
import static org.apache.commons.io.FileUtils.*
void ls(f) {
if (f.exists()) {
log.info "$f (${byteCountToDisplaySize(f.length())})"
} else {
log.warning "[FILE NOT FOUND] $f"
}
}
File getOriginalPath(db, id) {
return _args.outputPath.resolve("images/${db}/original/poster/${id}.jpg")
}
File getThumbnailPath(db, id) {
return _args.outputPath.resolve("images/${db}/thumb/poster/${id}.png")
}
void createThumbnail(original, thumb) {
thumb.dir.mkdirs()
execute 'convert', '-strip', original, '-thumbnail', '48x48', '-gravity', 'center', '-background', 'transparent', '-extent', '48x48', 'PNG8:' + thumb
}
2019-05-04 09:32:31 -04:00
void createIndexFile(db) {
def indexFile = _args.outputPath.resolve("images/${db}/thumb/poster/index.txt")
def index = indexFile.dir.listFiles{ it.image }.collect{ it.nameWithoutExtension as int }.toSorted()
index.join('\n').saveAs(indexFile)
2019-05-04 11:54:36 -04:00
execute 'xz', indexFile, '--force', '--keep'
2019-05-04 09:32:31 -04:00
println "Index: ${index.size()}"
indexFile.dir.listFiles{ !it.image }.each{ ls it }
}
2019-05-04 06:26:08 -04:00
void build(ids, section, db, query) {
def files = []
ids.each{ id ->
def original = getOriginalPath(section, id)
def thumb = getThumbnailPath(section, id)
2019-05-04 06:46:49 -04:00
if (thumb.exists()) {
log.finest "[SKIP] $id"
return
}
2019-05-04 08:26:43 -04:00
if (original.length() == 0 && original.exists() && System.currentTimeMillis() - original.lastModified() > 90 * 24 * 60 * 60 * 1000) {
log.finest "[SKIP] $id"
return
}
2019-05-04 08:38:26 -04:00
if (original.length() == 0 || !original.exists()) {
def artwork = retry(2, 60000) {
try {
return db.getArtwork(id, query, Locale.ENGLISH)
} catch (FileNotFoundException e) {
log.warning "[ARTWORK NOT FOUND] $e"
return null
}
}
2019-05-04 08:38:26 -04:00
artwork?.findResult{ a ->
return retry(2, 60000) {
try {
log.fine "Fetch $a"
return a.url.saveAs(original)
} catch (FileNotFoundException e) {
log.warning "[IMAGE NOT FOUND] $e"
return null
}
}
}
2019-05-04 06:26:08 -04:00
// create empty placeholder if there is no artwork
2019-05-04 08:38:26 -04:00
if (original.length() == 0 || !original.exists()) {
2019-05-04 06:26:08 -04:00
original.createNewFile()
2019-05-04 08:38:26 -04:00
original.setLastModified(System.currentTimeMillis())
2019-05-04 06:26:08 -04:00
}
ls original
}
if (original.length() > 0 && !thumb.exists()) {
createThumbnail(original, thumb)
files << thumb
ls thumb
}
}
if (files) {
createIndexFile(section)
}
}
build(MediaDetection.seriesIndex.object.id as HashSet, 'thetvdb', TheTVDB, 'poster')
build(MediaDetection.movieIndex.object.tmdbId as HashSet, 'themoviedb', TheMovieDB, 'posters')