Improved low resolution search

This commit is contained in:
Eduard Urbach 2018-03-20 01:43:23 +01:00
parent bf118c5f10
commit 947752333d
3 changed files with 102 additions and 56 deletions

View File

@ -1,14 +1,6 @@
package editor package editor
import ( import (
"image"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
@ -16,36 +8,22 @@ import (
const maxImageEntries = 70 const maxImageEntries = 70
// LowResolutionAnimeImages ... // LowResolutionAnimeImages filters anime with low resolution images.
func LowResolutionAnimeImages(ctx *aero.Context) string { func LowResolutionAnimeImages(ctx *aero.Context) string {
basePath := path.Join(arn.Root, "images/anime/original/") year, _ := ctx.GetInt("year")
files, err := ioutil.ReadDir(basePath) animeType := ctx.Get("type")
if err != nil { lowResAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
return ctx.Error(http.StatusInternalServerError, "Error reading anime images directory", err) if year != 0 && year != anime.StartDateTime().Year() {
} return false
lowResAnime := []*arn.Anime{}
for _, file := range files {
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
continue
} }
fullPath := path.Join(basePath, file.Name()) if animeType != "" && anime.Type != animeType {
width, height, _ := getImageDimensions(fullPath) return false
if width < arn.AnimeImageLargeWidth*2 || height < arn.AnimeImageLargeHeight*2 {
animeID := file.Name()
animeID = strings.TrimSuffix(animeID, filepath.Ext(animeID))
anime, err := arn.GetAnime(animeID)
if err == nil {
lowResAnime = append(lowResAnime, anime)
}
} }
}
return anime.Image.Width < arn.AnimeImageLargeWidth || anime.Image.Height < arn.AnimeImageLargeHeight
})
// Sort // Sort
arn.SortAnimeByQuality(lowResAnime) arn.SortAnimeByQuality(lowResAnime)
@ -68,20 +46,72 @@ func LowResolutionAnimeImages(ctx *aero.Context) string {
)) ))
} }
// getImageDimensions retrieves the dimensions for the given file path. // // LowResolutionAnimeImages ...
func getImageDimensions(imagePath string) (int, int, error) { // func LowResolutionAnimeImages(ctx *aero.Context) string {
file, err := os.Open(imagePath) // basePath := path.Join(arn.Root, "images/anime/original/")
defer file.Close() // files, err := ioutil.ReadDir(basePath)
if err != nil { // if err != nil {
return 0, 0, err // return ctx.Error(http.StatusInternalServerError, "Error reading anime images directory", err)
} // }
image, _, err := image.DecodeConfig(file) // lowResAnime := []*arn.Anime{}
if err != nil { // for _, file := range files {
return 0, 0, err // if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
} // continue
// }
return image.Width, image.Height, nil // fullPath := path.Join(basePath, file.Name())
} // width, height, _ := getImageDimensions(fullPath)
// if width < arn.AnimeImageLargeWidth*2 || height < arn.AnimeImageLargeHeight*2 {
// animeID := file.Name()
// animeID = strings.TrimSuffix(animeID, filepath.Ext(animeID))
// anime, err := arn.GetAnime(animeID)
// if err == nil {
// lowResAnime = append(lowResAnime, anime)
// }
// }
// }
// // Sort
// arn.SortAnimeByQuality(lowResAnime)
// // Limit
// count := len(lowResAnime)
// if count > maxImageEntries {
// lowResAnime = lowResAnime[:maxImageEntries]
// }
// return ctx.HTML(components.AnimeEditorListFull(
// "Anime with low resolution images",
// lowResAnime,
// count,
// "/editor/anime/missing/hiresimage",
// func(anime *arn.Anime) string {
// return "https://www.google.com/search?q=" + anime.Title.Canonical + "&tbm=isch"
// },
// ))
// }
// // getImageDimensions retrieves the dimensions for the given file path.
// func getImageDimensions(imagePath string) (int, int, error) {
// file, err := os.Open(imagePath)
// defer file.Close()
// if err != nil {
// return 0, 0, err
// }
// image, _, err := image.DecodeConfig(file)
// if err != nil {
// return 0, 0, err
// }
// return image.Width, image.Height, nil
// }

View File

@ -255,6 +255,8 @@ func Configure(app *aero.Application) {
l.Page("/editor/anime/missing/genres/:year", editor.Genres) l.Page("/editor/anime/missing/genres/:year", editor.Genres)
l.Page("/editor/anime/missing/genres/:year/:type", editor.Genres) l.Page("/editor/anime/missing/genres/:year/:type", editor.Genres)
l.Page("/editor/anime/missing/hiresimage", editor.LowResolutionAnimeImages) l.Page("/editor/anime/missing/hiresimage", editor.LowResolutionAnimeImages)
l.Page("/editor/anime/missing/hiresimage/:year", editor.LowResolutionAnimeImages)
l.Page("/editor/anime/missing/hiresimage/:year/:type", editor.LowResolutionAnimeImages)
// Editor - MALdiff // Editor - MALdiff
l.Page("/editor/anime/maldiff", editor.CompareMAL) l.Page("/editor/anime/maldiff", editor.CompareMAL)

View File

@ -1,46 +1,60 @@
package main package main
import ( import (
"fmt"
"io/ioutil"
"os" "os"
"path" "path"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/fatih/color"
) )
func main() { func main() {
color.Yellow("Updating anime thumbnails")
defer arn.Node.Close() defer arn.Node.Close()
defer color.Green("Finished.")
for anime := range arn.StreamAnime() { for anime := range arn.StreamAnime() {
base := path.Join(arn.Root, "/images/anime/original/", anime.ID) base := path.Join(arn.Root, "/images/anime/original/", anime.ID)
if _, err := os.Stat(base + ".png"); err == nil { if _, err := os.Stat(base + ".png"); err == nil {
anime.Image.Extension = ".png" update(anime, base+".png")
anime.Save()
continue continue
} }
if _, err := os.Stat(base + ".jpg"); err == nil { if _, err := os.Stat(base + ".jpg"); err == nil {
anime.Image.Extension = ".jpg" update(anime, base+".jpg")
anime.Save()
continue continue
} }
if _, err := os.Stat(base + ".jpeg"); err == nil { if _, err := os.Stat(base + ".jpeg"); err == nil {
anime.Image.Extension = ".jpg" update(anime, base+".jpg")
anime.Save()
continue continue
} }
if _, err := os.Stat(base + ".gif"); err == nil { if _, err := os.Stat(base + ".gif"); err == nil {
anime.Image.Extension = ".gif" update(anime, base+".gif")
anime.Save()
continue continue
} }
if _, err := os.Stat(base + ".webp"); err == nil { if _, err := os.Stat(base + ".webp"); err == nil {
anime.Image.Extension = ".webp" update(anime, base+".webp")
anime.Save()
continue continue
} }
} }
} }
func update(anime *arn.Anime, filePath string) {
fmt.Println(anime.ID, anime)
data, err := ioutil.ReadFile(filePath)
if err != nil {
return
}
anime.SetImageBytes(data)
anime.Save()
}