2019-05-01 06:14:42 -04:00
|
|
|
#!/usr/bin/env filebot -script
|
|
|
|
|
|
|
|
import static org.apache.commons.io.FileUtils.*
|
|
|
|
|
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
scaleFactor = [1, 2]
|
|
|
|
thumbnailSize = [48, 48]
|
|
|
|
|
|
|
|
|
2019-05-01 06:14:42 -04:00
|
|
|
void ls(f) {
|
2019-05-01 09:10:11 -04:00
|
|
|
if (f.exists()) {
|
|
|
|
log.info "$f (${byteCountToDisplaySize(f.length())})"
|
|
|
|
} else {
|
|
|
|
log.warning "[FILE NOT FOUND] $f"
|
|
|
|
}
|
2019-05-01 06:14:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-01 16:03:05 -04:00
|
|
|
File getOriginalPath(db, id) {
|
|
|
|
return _args.outputPath.resolve("images/${db}/original/poster/${id}.jpg")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
File getThumbnailPath(db, id, scale) {
|
2019-05-18 11:10:12 -04:00
|
|
|
// e.g. 42.png or 42@2x.png
|
|
|
|
def n = scale == 1 ? id : id + '@' + scale + 'x'
|
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
return _args.outputPath.resolve("images/${db}/thumb/poster/${n}.png")
|
2019-05-01 16:03:05 -04:00
|
|
|
}
|
2019-05-01 06:14:42 -04:00
|
|
|
|
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
void createThumbnail(original, thumb, scale) {
|
2019-05-03 08:02:18 -04:00
|
|
|
thumb.dir.mkdirs()
|
2019-05-16 08:55:10 -04:00
|
|
|
|
|
|
|
def size = thumbnailSize*.multiply(scale).join('x')
|
|
|
|
execute 'convert', '-strip', original, '-thumbnail', size, '-gravity', 'center', '-background', 'transparent', '-extent', size, 'PNG8:' + thumb
|
2019-05-01 16:03:05 -04:00
|
|
|
}
|
2019-05-01 06:14:42 -04:00
|
|
|
|
|
|
|
|
2019-05-04 09:32:31 -04:00
|
|
|
void createIndexFile(db) {
|
|
|
|
def indexFile = _args.outputPath.resolve("images/${db}/thumb/poster/index.txt")
|
2019-05-16 09:12:59 -04:00
|
|
|
def index = indexFile.dir.listFiles()*.getNameWithoutExtension().findAll{ it ==~ /\d+/ }*.toInteger() as TreeSet
|
2019-05-04 09:32:31 -04:00
|
|
|
|
|
|
|
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-01 16:03:05 -04:00
|
|
|
}
|
2019-05-01 06:33:35 -04:00
|
|
|
|
2019-05-01 06:14:42 -04:00
|
|
|
|
2019-05-01 16:03:05 -04:00
|
|
|
|
2019-05-04 06:26:08 -04:00
|
|
|
|
2019-05-01 16:03:05 -04:00
|
|
|
void build(ids, section, db, query) {
|
2019-05-08 16:16:10 -04:00
|
|
|
def files = []
|
|
|
|
|
2019-05-01 16:03:05 -04:00
|
|
|
ids.each{ id ->
|
2019-05-19 05:55:01 -04:00
|
|
|
def original = getOriginalPath(section, id)
|
|
|
|
|
|
|
|
if (original.length() == 0 && original.exists() && System.currentTimeMillis() - original.lastModified() > 90 * 24 * 60 * 60 * 1000) {
|
|
|
|
log.finest "[SKIP] $id"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-18 11:46:13 -04:00
|
|
|
scaleFactor.each{ scale ->
|
2019-05-16 08:55:10 -04:00
|
|
|
def thumb = getThumbnailPath(section, id, scale)
|
2019-05-04 06:46:49 -04:00
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
if (thumb.exists()) {
|
2019-05-19 05:55:01 -04:00
|
|
|
log.finest "[SKIP] $id (@${scale}x)"
|
2019-05-16 08:55:10 -04:00
|
|
|
return
|
2019-05-03 08:02:18 -04:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
if (original.length() == 0 || !original.exists()) {
|
|
|
|
def artwork = retry(2, 60000) {
|
2019-05-01 16:03:05 -04:00
|
|
|
try {
|
2019-05-16 08:55:10 -04:00
|
|
|
return db.getArtwork(id, query, Locale.ENGLISH)
|
2019-05-01 16:03:05 -04:00
|
|
|
} catch (FileNotFoundException e) {
|
2019-05-16 08:55:10 -04:00
|
|
|
log.warning "[ARTWORK NOT FOUND] $e"
|
2019-05-01 16:03:05 -04:00
|
|
|
return null
|
|
|
|
}
|
2019-05-01 10:34:23 -04:00
|
|
|
}
|
2019-05-04 06:26:08 -04:00
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
artwork?.findResult{ a ->
|
|
|
|
return retry(2, 60000) {
|
2019-05-18 09:48:36 -04:00
|
|
|
sleep(2000)
|
2019-05-16 08:55:10 -04:00
|
|
|
try {
|
|
|
|
log.fine "Fetch $a"
|
|
|
|
return a.url.saveAs(original)
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
log.warning "[IMAGE NOT FOUND] $e"
|
|
|
|
return null
|
2019-05-19 06:12:00 -04:00
|
|
|
} finally {
|
|
|
|
ls original
|
2019-05-16 08:55:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 06:26:08 -04:00
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
// create empty placeholder if there is no artwork
|
|
|
|
if (original.length() == 0 || !original.exists()) {
|
|
|
|
original.createNewFile()
|
|
|
|
original.setLastModified(System.currentTimeMillis())
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 16:16:10 -04:00
|
|
|
|
2019-05-16 08:55:10 -04:00
|
|
|
if (original.length() > 0 && !thumb.exists()) {
|
|
|
|
createThumbnail(original, thumb, scale)
|
|
|
|
files << thumb
|
|
|
|
|
|
|
|
ls thumb
|
|
|
|
}
|
2019-05-01 16:03:05 -04:00
|
|
|
}
|
2019-05-01 06:14:42 -04:00
|
|
|
}
|
2019-05-01 06:33:35 -04:00
|
|
|
|
2019-05-08 16:16:10 -04:00
|
|
|
if (files) {
|
|
|
|
createIndexFile(section)
|
|
|
|
}
|
2019-05-01 06:14:42 -04:00
|
|
|
}
|
2019-05-01 06:33:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-01 16:03:05 -04:00
|
|
|
|
2019-05-18 09:48:36 -04:00
|
|
|
build(MediaDetection.animeIndex.object.id as HashSet, 'anidb', AniDB, 'poster')
|
2019-05-02 11:10:38 -04:00
|
|
|
build(MediaDetection.seriesIndex.object.id as HashSet, 'thetvdb', TheTVDB, 'poster')
|
|
|
|
build(MediaDetection.movieIndex.object.tmdbId as HashSet, 'themoviedb', TheMovieDB, 'posters')
|