Nintendo Switch Provider [WIP]

#2
This commit is contained in:
Felipe Martin 2020-11-19 23:18:01 +01:00
parent e3c7da17ce
commit 0b1f6d3231
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
5 changed files with 135 additions and 13 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
Output
Output
Album

View File

@ -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
```

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}