package main import ( "fmt" _ "golang.org/x/image/webp" "image" _ "image/png" "io/ioutil" "os" "path/filepath" "strings" ) func main() { files, _ := ioutil.ReadDir("./static/images") for _, imgFile := range files { fullPath := filepath.Join("./static/images", imgFile.Name()) if strings.Contains(fullPath, ".gif") || strings.Contains(fullPath, ".jpg") || strings.Contains(fullPath, ".svg") { err := os.Remove(fullPath) if err != nil { panic(err) } continue } if reader, err := os.Open(fullPath); err == nil { im, _, err := image.DecodeConfig(reader) if err != nil { panic(err) } err = reader.Close() if err != nil { panic(err) } if im.Height < 256 || im.Width < 256 { err := os.Remove(fullPath) if err != nil { panic(err) } continue } if strings.Contains(imgFile.Name(), "(") { newName := imgFile.Name()[:strings.Index(imgFile.Name(), "(")-1] + imgFile.Name()[strings.Index(imgFile.Name(), ")")+1:] err := os.Rename(fullPath, filepath.Join("./static/images", newName)) if err != nil { panic(err) } } } else { panic(err) } } pokemonListBytes, err := ioutil.ReadFile("./cmd/init/pokemon-list.txt") if err != nil { panic(err) } pokemonList := strings.Split(string(pokemonListBytes), "\r\n") shinesEnabledBytes, err := ioutil.ReadFile("./cmd/init/shiny-enabled.txt") if err != nil { panic(err) } shinesEnabled := strings.Split(string(shinesEnabledBytes), "\r\n") pokemonCount := 0 for i := 1; i < 867; i++ { shinyImageName := getFileName(fmt.Sprintf("%03d-00-shiny", i)) imageName := getFileName(fmt.Sprintf("%03d-00", i)) if doesFileExist(imageName) { pokemonCount++ fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyImageName, imageName) } shinyAlolaImageName := getFileName(fmt.Sprintf("%03d-31-shiny", i)) alolaImageName := getFileName(fmt.Sprintf("%03d-31", i)) if doesFileExist(alolaImageName) { pokemonCount++ fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "alola", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyAlolaImageName, alolaImageName) } shinyGalarImageName := getFileName(fmt.Sprintf("%03d-61-shiny", i)) galarImageName := getFileName(fmt.Sprintf("%03d-61", i)) if doesFileExist(galarImageName) { pokemonCount++ fmt.Printf("INSERT INTO pokemon (id, dex_number, region, name, shiny_enabled, shiny_image_name, image_name) \n VALUES (%d, %d, '%s', '%s', %t, '%s', '%s'); \n \n", pokemonCount, i, "galar", pokemonList[i-1], isShinyEnabled(shinesEnabled, pokemonList[i-1]), shinyGalarImageName, galarImageName) } } } func getFileName(filename string) string { if doesFileExist(fmt.Sprintf("%s.png", filename)) { return fmt.Sprintf("%s.png", filename) } else if doesFileExist(fmt.Sprintf("%s.webp", filename)) { return fmt.Sprintf("%s.webp", filename) } return "" } func doesFileExist(fileName string) bool { if _, err := os.Stat(fmt.Sprintf("./static/images/%s", fileName)); os.IsNotExist(err) || fileName == "" { return false } return true } func isShinyEnabled(shinesEnabled []string, pokemon string) bool { for _, shinyEnabled := range shinesEnabled { if pokemon == shinyEnabled { return true } } return false }