From 0b1f6d32316a80b844f96f6da2cb6693f14f57d9 Mon Sep 17 00:00:00 2001 From: Felipe Martin Date: Thu, 19 Nov 2020 23:18:01 +0100 Subject: [PATCH] Nintendo Switch Provider [WIP] #2 --- .gitignore | 3 +- README.md | 16 ++-- pkg/cli/cli.go | 14 +-- pkg/games/structs.go | 4 +- pkg/providers/nintendo_switch/switch.go | 111 ++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 13 deletions(-) create mode 100644 pkg/providers/nintendo_switch/switch.go diff --git a/.gitignore b/.gitignore index cd0df9c..859be33 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -Output \ No newline at end of file +Output +Album diff --git a/README.md b/README.md index f2d4f11..10465a4 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,15 @@ A simple tool to collect and sort games screenshots from different platforms. -## Supported platforms +## Supported providers -| Name | Supported OS | -| --- | --- | -| Steam | Linux, macOS, Windows -| Minecraft | Linux, Linux Flatpak, macOS, Windows +Use the appropriate ID with the `-provider` flag. [See examples below](#Usage) + +| Name | ID | Notes | +| --- | --- | --- | +| Steam | `steam` | Linux, macOS, Windows +| Minecraft | `minecraft` | Linux, Linux Flatpak, macOS, Windows +| Nintendo Switch | `nintend-switch` | Requires `-input-path` | ## How it works @@ -36,4 +39,7 @@ games-screenshot-manager -provider steam -output-path ./Output # Perform a dry run (see what's gonna get copied where) games-screenshot-mananger -provider steam -dry-run + +# Parse all Nintendo Switch screenshots +games-screenshot-manager -provider nintendo-switch -input-path ./Album ``` diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 5eeb96e..8f843ab 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -7,45 +7,47 @@ import ( "os" "path" "path/filepath" - "strconv" "strings" "github.com/fmartingr/games-screenshot-mananger/pkg/games" "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/steam" ) -var allowedProviders = [...]string{"steam", "minecraft"} +var allowedProviders = [...]string{"steam", "minecraft", "nintendo-switch"} const defaultOutputPath string = "./Output" -// const defaultInputPath string = "./Input" +const defaultInputPath string = "" const defaultProvider string = "steam" const defaultDryRun bool = false func Start() { var provider = flag.String("provider", defaultProvider, "steam") var outputPath = flag.String("output-path", defaultOutputPath, "The destination path of the screenshots") - // var inputPath = flag.String("input-path", defaultInputPath, "Input path for the provider that requires it") + var inputPath = flag.String("input-path", defaultInputPath, "Input path for the provider that requires it") var dryRun = flag.Bool("dry-run", defaultDryRun, "Use to disable write actions on filesystem") flag.Parse() if helpers.SliceContainsString(allowedProviders[:], *provider, nil) { - games := getGamesFromProvider(*provider) + games := getGamesFromProvider(*provider, *inputPath) processGames(games, *outputPath, *dryRun) } else { log.Printf("Provider %s not found!", *provider) } } -func getGamesFromProvider(provider string) []games.Game { +func getGamesFromProvider(provider string, inputPath string) []games.Game { var games []games.Game switch provider { case "steam": games = append(games, steam.GetGames()...) case "minecraft": games = append(games, minecraft.GetGames()...) + case "nintendo-switch": + games = append(games, nintendo_switch.GetGames(inputPath)...) } return games } diff --git a/pkg/games/structs.go b/pkg/games/structs.go index 79c89c4..e05fdcd 100644 --- a/pkg/games/structs.go +++ b/pkg/games/structs.go @@ -6,6 +6,8 @@ import ( "path" ) +const DatetimeFormat = "2006-01-02_15-04-05" + type Game struct { ID string Name string @@ -28,5 +30,5 @@ func (screenshot Screenshot) GetDestinationName() string { if statErr != nil { log.Fatal(statErr) } - return fileStat.ModTime().Format("2006-01-02_15-04-05") + path.Ext(screenshot.Path) + return fileStat.ModTime().Format(DatetimeFormat) + path.Ext(screenshot.Path) } diff --git a/pkg/providers/nintendo_switch/switch.go b/pkg/providers/nintendo_switch/switch.go new file mode 100644 index 0000000..187b47c --- /dev/null +++ b/pkg/providers/nintendo_switch/switch.go @@ -0,0 +1,111 @@ +package nintendo_switch + +import ( + "encoding/json" + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" + "time" + + "github.com/fmartingr/games-screenshot-mananger/pkg/games" + "github.com/fmartingr/games-screenshot-mananger/pkg/helpers" +) + +const providerName = "nintendo-switch" +const gameListURL = "https://fmartingr.github.io/switch-games-json/switch_games.json" + +type SwitchGame struct { + Name string `json:"description"` + EncryptedGameID string `json:"encrypted_game_id"` +} + +func findGameByEncryptedID(gameList []SwitchGame, encryptedGameID string) SwitchGame { + var gameFound SwitchGame = SwitchGame{EncryptedGameID: encryptedGameID} + for _, game := range gameList { + if strings.ToUpper(game.EncryptedGameID) == strings.ToUpper(encryptedGameID) { + gameFound = game + } + } + + return gameFound +} + +func getSwitchGameList() []SwitchGame { + response, err := helpers.DoRequest("GET", gameListURL) + if err != nil { + log.Panic(err) + } + + if response.Body != nil { + defer response.Body.Close() + } + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Panic(err) + } + + switchGameList := []SwitchGame{} + jsonErr := json.Unmarshal(body, &switchGameList) + if jsonErr != nil { + log.Fatal(jsonErr) + } + + log.Printf("Updated Nintendo Switch game list. Found %d games.", len(switchGameList)) + + return switchGameList +} + +func addScreenshotToGame(userGames []games.Game, switchGame SwitchGame, screenshot games.Screenshot) []games.Game { + var foundGame games.Game + for gameIndex, game := range userGames { + if game.ID == switchGame.EncryptedGameID { + foundGame = game + userGames[gameIndex].Screenshots = append(userGames[gameIndex].Screenshots, screenshot) + } + } + + if foundGame.ID == "" { + foundGame := games.Game{Name: switchGame.Name, ID: switchGame.EncryptedGameID, Platform: "Nintendo Switch", Provider: providerName} + foundGame.Screenshots = append(foundGame.Screenshots, screenshot) + userGames = append(userGames, foundGame) + } + + return userGames +} + +func GetGames(inputPath string) []games.Game { + switchGames := getSwitchGameList() + var userGames []games.Game + + err := filepath.Walk(inputPath, + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() { + filename := filepath.Base(path) + extension := filepath.Ext(filepath.Base(path)) + + filenameParsed := strings.Split(filename[:len(filename)-len(extension)], "-") + switchGame := findGameByEncryptedID(switchGames, filenameParsed[1]) + + layout := "20060102150405" + destinationName, err := time.Parse(layout, filenameParsed[0][0:14]) + + if err != nil { + log.Panic("Could not parse filename: ", err) + } + + screenshot := games.Screenshot{Path: path, DestinationName: destinationName.Format(games.DatetimeFormat) + extension} + userGames = addScreenshotToGame(userGames, switchGame, screenshot) + } + return nil + }) + if err != nil { + log.Panic(err) + } + return userGames +}