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
import (
"image"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
@ -16,36 +8,22 @@ import (
const maxImageEntries = 70
// LowResolutionAnimeImages ...
// LowResolutionAnimeImages filters anime with low resolution images.
func LowResolutionAnimeImages(ctx *aero.Context) string {
basePath := path.Join(arn.Root, "images/anime/original/")
files, err := ioutil.ReadDir(basePath)
year, _ := ctx.GetInt("year")
animeType := ctx.Get("type")
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error reading anime images directory", err)
lowResAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
if year != 0 && year != anime.StartDateTime().Year() {
return false
}
lowResAnime := []*arn.Anime{}
for _, file := range files {
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
continue
if animeType != "" && anime.Type != animeType {
return false
}
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)
}
}
}
return anime.Image.Width < arn.AnimeImageLargeWidth || anime.Image.Height < arn.AnimeImageLargeHeight
})
// Sort
arn.SortAnimeByQuality(lowResAnime)
@ -68,20 +46,72 @@ func LowResolutionAnimeImages(ctx *aero.Context) string {
))
}
// getImageDimensions retrieves the dimensions for the given file path.
func getImageDimensions(imagePath string) (int, int, error) {
file, err := os.Open(imagePath)
defer file.Close()
// // LowResolutionAnimeImages ...
// func LowResolutionAnimeImages(ctx *aero.Context) string {
// basePath := path.Join(arn.Root, "images/anime/original/")
// files, err := ioutil.ReadDir(basePath)
if err != nil {
return 0, 0, err
}
// if err != nil {
// return ctx.Error(http.StatusInternalServerError, "Error reading anime images directory", err)
// }
image, _, err := image.DecodeConfig(file)
// lowResAnime := []*arn.Anime{}
if err != nil {
return 0, 0, err
}
// for _, file := range files {
// 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/:type", editor.Genres)
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
l.Page("/editor/anime/maldiff", editor.CompareMAL)

View File

@ -1,46 +1,60 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
func main() {
color.Yellow("Updating anime thumbnails")
defer arn.Node.Close()
defer color.Green("Finished.")
for anime := range arn.StreamAnime() {
base := path.Join(arn.Root, "/images/anime/original/", anime.ID)
if _, err := os.Stat(base + ".png"); err == nil {
anime.Image.Extension = ".png"
anime.Save()
update(anime, base+".png")
continue
}
if _, err := os.Stat(base + ".jpg"); err == nil {
anime.Image.Extension = ".jpg"
anime.Save()
update(anime, base+".jpg")
continue
}
if _, err := os.Stat(base + ".jpeg"); err == nil {
anime.Image.Extension = ".jpg"
anime.Save()
update(anime, base+".jpg")
continue
}
if _, err := os.Stat(base + ".gif"); err == nil {
anime.Image.Extension = ".gif"
anime.Save()
update(anime, base+".gif")
continue
}
if _, err := os.Stat(base + ".webp"); err == nil {
anime.Image.Extension = ".webp"
anime.Save()
update(anime, base+".webp")
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()
}