From 1d766a843e665e2bd8dbda2365343d9371221034 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 5 Jan 2026 16:03:14 +0100 Subject: [PATCH] implemented game art download --- cmd/oplcli.go | 7 ++++ internal/art.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++ internal/oplcli.go | 14 +++++++ 3 files changed, 115 insertions(+) create mode 100644 internal/art.go diff --git a/cmd/oplcli.go b/cmd/oplcli.go index 3cf9a27..0b06891 100644 --- a/cmd/oplcli.go +++ b/cmd/oplcli.go @@ -13,9 +13,14 @@ type RenameCmd struct { File string `arg:"positional"` } +type ArtCmd struct { + File string `arg:"positional"` +} + type args struct { Scan *ScanCmd `arg:"subcommand:scan"` Rename *RenameCmd `arg:"subcommand:rename"` + Art *ArtCmd `arg:"subcommand:art"` } func Run() { @@ -27,5 +32,7 @@ func Run() { oplcli.Scan(args.Scan.File, oplcli.Config{}) case args.Rename != nil: oplcli.Rename(args.Rename.File, oplcli.Config{}) + case args.Art != nil: + oplcli.DownloadGameArt(args.Art.File, oplcli.Config{}) } } diff --git a/internal/art.go b/internal/art.go new file mode 100644 index 0000000..f81da73 --- /dev/null +++ b/internal/art.go @@ -0,0 +1,94 @@ +package oplcli + +import ( + "fmt" + "io" + "net/http" + "os" + "path" +) + +type artUrls struct { + cover string + cover2 string + background string + disc string + spine string + logo string + screenshot string +} + +func getArtUrlForGameFromGithub(gameID string) artUrls { + const baseURL = "https://raw.githubusercontent.com/Luden02/psx-ps2-opl-art-database/refs/heads/main/PS2" + + prefix := gameID[0:4] + num1 := gameID[5:8] + num2 := gameID[8:10] + + newGameID := fmt.Sprintf("%s_%s.%s", prefix, num1, num2) + + genURL := func(kind string) string { + return fmt.Sprintf("%s/%s/%s_%s.png", baseURL, newGameID, newGameID, kind) + } + + return artUrls{ + cover: genURL("COV"), + cover2: genURL("COV2"), + background: genURL("BG_00"), + disc: genURL("ICO"), + spine: genURL("LAB"), + logo: genURL("LGO"), + screenshot: genURL("SCR_00"), + } +} + +func downloadArtForGame(gameID string, targetDir string) error { + + urls := getArtUrlForGameFromGithub(gameID) + + dl := func(url, kind string) (bool, error) { + return downloadFile(url, path.Join(targetDir, fmt.Sprintf("%s_%s.png", gameID, kind))) + } + + dl(urls.cover, "COV") + dl(urls.cover2, "COV2") + dl(urls.disc, "ICO") + dl(urls.background, "BG") + dl(urls.screenshot, "SCR") + dl(urls.spine, "LAB") + + return nil +} + +func downloadFile(url, target string) (bool, error) { + out, err := os.Create(target) + if err != nil { + return false, err + } + defer out.Close() + + resp, err := http.Get(url) + if err != nil { + return false, err + } + defer resp.Body.Close() + + if resp.StatusCode == 404 { + return false, nil + } + + if resp.StatusCode >= 400 { + return false, fmt.Errorf("status code: %s", resp.Status) + } + + n, err := io.Copy(out, resp.Body) + if err != nil { + return false, err + } + + if n == 0 { + return false, fmt.Errorf("Got empty file") + } + + return true, nil +} diff --git a/internal/oplcli.go b/internal/oplcli.go index 8b8e9a2..089d968 100644 --- a/internal/oplcli.go +++ b/internal/oplcli.go @@ -57,3 +57,17 @@ func Rename(file string, config Config) { fmt.Printf("Renamed %s to %s", file, newPath) } + +func DownloadGameArt(file string, config Config) { + fmt.Printf("Scanning file: %s\n", file) + + gameID, err := scanGameFileForID(file) + if err != nil { + fmt.Printf("Failed to scan game file: %s", err) + os.Exit(1) + } + + fmt.Printf("Found game ID: %s\n", gameID) + + downloadArtForGame(gameID, "./art") +}