diff --git a/README.md b/README.md index c55f04a..9d4a0a9 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Use the appropriate ID with the `-provider` flag. [See examples below](#Usage) | Steam | `steam` | Linux, macOS, Windows | Yes [Example](https://steamcdn-a.akamaihd.net/steam/apps/377840/header.jpg) | | Minecraft | `minecraft` | Linux, Linux Flatpak, macOS, Windows | No | | Nintendo Switch | `nintendo-switch` | Requires `-input-path` | No | +| PlayStation 4 | `playstation-4` | Requires `-input-path` | No | ## How it works diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 4258f0e..c55733b 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -13,11 +13,12 @@ import ( "github.com/fmartingr/games-screenshot-mananger/pkg/helpers" "github.com/fmartingr/games-screenshot-mananger/pkg/providers/minecraft" "github.com/fmartingr/games-screenshot-mananger/pkg/providers/nintendo_switch" + "github.com/fmartingr/games-screenshot-mananger/pkg/providers/playstation4" "github.com/fmartingr/games-screenshot-mananger/pkg/providers/steam" "github.com/gosimple/slug" ) -var allowedProviders = [...]string{"steam", "minecraft", "nintendo-switch"} +var allowedProviders = [...]string{"steam", "minecraft", "nintendo-switch", "playstation-4"} const defaultOutputPath string = "./Output" @@ -53,6 +54,8 @@ func getGamesFromProvider(provider string, inputPath string, downloadCovers bool games = append(games, minecraft.GetGames()...) case "nintendo-switch": games = append(games, nintendo_switch.GetGames(inputPath)...) + case "playstation-4": + games = append(games, playstation4.GetGames(inputPath)...) } return games } diff --git a/pkg/providers/playstation4/playstation4.go b/pkg/providers/playstation4/playstation4.go new file mode 100644 index 0000000..7f0bda8 --- /dev/null +++ b/pkg/providers/playstation4/playstation4.go @@ -0,0 +1,69 @@ +package playstation4 + +import ( + "log" + "os" + "path/filepath" + "time" + + "github.com/fmartingr/games-screenshot-mananger/pkg/games" +) + +const providerName = "playstation-4" +const platformName = "PlayStation 4" + +func addScreenshotToGame(userGames []games.Game, gameName string, screenshot games.Screenshot) []games.Game { + var foundGame games.Game + for gameIndex, game := range userGames { + if game.Name == gameName { + foundGame = game + userGames[gameIndex].Screenshots = append(userGames[gameIndex].Screenshots, screenshot) + } + } + + // Game not found + if foundGame.Name == "" { + foundGame := games.Game{Name: gameName, ID: gameName, Platform: platformName, Provider: providerName} + foundGame.Screenshots = append(foundGame.Screenshots, screenshot) + userGames = append(userGames, foundGame) + } + + return userGames +} + +func GetGames(inputPath string) []games.Game { + var userGames []games.Game + + err := filepath.Walk(inputPath, + func(filePath string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + gameName := filepath.Base(filepath.Dir(filePath)) + fileName := filepath.Base(filePath) + extension := filepath.Ext(filepath.Base(filePath)) + layout := "20060102150405" + + if len(fileName) >= len(layout)+len(extension) { + destinationName, err := time.Parse(layout, fileName[len(fileName)-len(extension)-len(layout):len(fileName)-len(extension)]) + + if err == nil { + screenshot := games.Screenshot{Path: filePath, DestinationName: destinationName.Format(games.DatetimeFormat) + extension} + userGames = addScreenshotToGame(userGames, gameName, screenshot) + } else { + log.Printf("File doesn't follow datetime convention: %s. (%s) skipping...", filePath, err) + } + } else { + log.Printf("File doesn't follow datetime convention: %s, skipping...", filePath) + } + } + + return nil + }) + if err != nil { + log.Panic(err) + } + return userGames +}