Created internal/cmd folders

This commit is contained in:
Felipe M 2021-05-08 11:37:52 +02:00
parent ef03e13e86
commit d27e4947ae
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
11 changed files with 71 additions and 67 deletions

1
.gitignore vendored
View File

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

View File

@ -1,2 +1,5 @@
build:
go build ./...
clean:
rm -rf build
build: clean
go build -o build/games-screenshot-manager cmd/games-screenshot-manager/*.go

View File

@ -0,0 +1,9 @@
package main
import (
"github.com/fmartingr/games-screenshot-manager/internal/cli"
)
func main() {
cli.Start()
}

View File

@ -8,8 +8,8 @@ import (
"path/filepath"
"strings"
"github.com/fmartingr/games-screenshot-manager/pkg/games"
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers/minecraft"
"github.com/fmartingr/games-screenshot-manager/pkg/providers/nintendo_switch"
"github.com/fmartingr/games-screenshot-manager/pkg/providers/playstation4"
@ -28,7 +28,7 @@ const defaultDryRun bool = false
const defaultDownloadCovers bool = false
func Start() {
cliOptions := games.CLIOptions{
cliOptions := providers.ProviderOptions{
OutputPath: flag.String("output-path", defaultOutputPath, "The destination path of the screenshots"),
InputPath: flag.String("input-path", defaultInputPath, "Input path for the provider that requires it"),
DownloadCovers: flag.Bool("download-covers", defaultDownloadCovers, "use to enable the download of covers (if the provider supports it)"),
@ -46,8 +46,8 @@ func Start() {
}
}
func getGamesFromProvider(provider string, cliOptions games.CLIOptions) []games.Game {
var games []games.Game
func getGamesFromProvider(provider string, cliOptions providers.ProviderOptions) []providers.Game {
var games []providers.Game
switch provider {
case "steam":
games = append(games, steam.GetGames(cliOptions)...)
@ -63,7 +63,7 @@ func getGamesFromProvider(provider string, cliOptions games.CLIOptions) []games.
return games
}
func processGames(games []games.Game, cliOptions games.CLIOptions) {
func processGames(games []providers.Game, cliOptions providers.ProviderOptions) {
for _, game := range games {
destinationPath := filepath.Join(helpers.ExpandUser(*cliOptions.OutputPath), game.Platform)
if len(game.Name) > 0 {
@ -112,7 +112,7 @@ func processGames(games []games.Game, cliOptions games.CLIOptions) {
continue
}
if bytes.Compare(sourceMd5, destinationMd5) != 0 {
if !bytes.Equal(sourceMd5, destinationMd5) {
// Images are not equal, we should copy it anyway, but how?
log.Println("Found different screenshot with equal timestamp for game ", game.Name, screenshot.Path)
}

View File

@ -1,9 +0,0 @@
package main
import (
"github.com/fmartingr/games-screenshot-manager/pkg/cli"
)
func main() {
cli.Start()
}

View File

@ -8,11 +8,11 @@ import (
"runtime"
"strings"
"github.com/fmartingr/games-screenshot-manager/pkg/games"
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
)
func getScreenshotsFromPath(game *games.Game, path string) {
func getScreenshotsFromPath(game *providers.Game, path string) {
path = helpers.ExpandUser(path)
if _, err := os.Stat(path); !os.IsNotExist(err) {
files, err := ioutil.ReadDir(path)
@ -22,22 +22,22 @@ func getScreenshotsFromPath(game *games.Game, path string) {
for _, file := range files {
if strings.Contains(file.Name(), ".png") {
game.Screenshots = append(game.Screenshots, games.Screenshot{Path: path + "/" + file.Name(), DestinationName: file.Name()})
game.Screenshots = append(game.Screenshots, providers.Screenshot{Path: path + "/" + file.Name(), DestinationName: file.Name()})
}
}
}
}
func GetGames(cliOptions games.CLIOptions) []games.Game {
var result []games.Game
func GetGames(cliOptions providers.ProviderOptions) []providers.Game {
var result []providers.Game
// Standalone minecraft
minecraftStandalone := games.Game{Name: "Minecraft", Platform: "PC", Notes: "Standalone"}
minecraftStandalone := providers.Game{Name: "Minecraft", Platform: "PC", Notes: "Standalone"}
if runtime.GOOS == "linux" {
getScreenshotsFromPath(&minecraftStandalone, "~/.minecraft/screenshots")
// Flatpak minecraft
minecraftFlatpak := games.Game{Name: "Minecraft", Platform: "PC", Notes: "Flatpak"}
minecraftFlatpak := providers.Game{Name: "Minecraft", Platform: "PC", Notes: "Flatpak"}
for _, path := range [2]string{"~/.var/app/com.mojang.Minecraft/.minecraft/screenshots", "~/.var/app/com.mojang.Minecraft/data/minecraft/screenshots"} {
getScreenshotsFromPath(&minecraftFlatpak, path)
}

View File

@ -9,8 +9,8 @@ import (
"strings"
"time"
"github.com/fmartingr/games-screenshot-manager/pkg/games"
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
)
const providerName = "nintendo-switch"
@ -25,7 +25,7 @@ type SwitchGame struct {
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) {
if strings.EqualFold(game.EncryptedGameID, encryptedGameID) {
gameFound = game
}
}
@ -54,13 +54,13 @@ func getSwitchGameList() []SwitchGame {
log.Fatal(jsonErr)
}
log.Printf("Updated Nintendo Switch game list. Found %d games.", len(switchGameList))
log.Printf("Updated Nintendo Switch game list. Found %d providers.", len(switchGameList))
return switchGameList
}
func addScreenshotToGame(userGames []games.Game, switchGame SwitchGame, screenshot games.Screenshot) []games.Game {
var foundGame games.Game
func addScreenshotToGame(userGames []providers.Game, switchGame SwitchGame, screenshot providers.Screenshot) []providers.Game {
var foundGame providers.Game
for gameIndex, game := range userGames {
if game.ID == switchGame.EncryptedGameID {
foundGame = game
@ -69,7 +69,7 @@ func addScreenshotToGame(userGames []games.Game, switchGame SwitchGame, screensh
}
if foundGame.ID == "" {
foundGame := games.Game{Name: switchGame.Name, ID: switchGame.EncryptedGameID, Platform: platformName, Provider: providerName}
foundGame := providers.Game{Name: switchGame.Name, ID: switchGame.EncryptedGameID, Platform: platformName, Provider: providerName}
foundGame.Screenshots = append(foundGame.Screenshots, screenshot)
userGames = append(userGames, foundGame)
}
@ -77,9 +77,9 @@ func addScreenshotToGame(userGames []games.Game, switchGame SwitchGame, screensh
return userGames
}
func GetGames(cliOptions games.CLIOptions) []games.Game {
func GetGames(cliOptions providers.ProviderOptions) []providers.Game {
switchGames := getSwitchGameList()
var userGames []games.Game
var userGames []providers.Game
err := filepath.Walk(*cliOptions.InputPath,
func(path string, info os.FileInfo, err error) error {
@ -100,7 +100,7 @@ func GetGames(cliOptions games.CLIOptions) []games.Game {
log.Panic("Could not parse filename: ", err)
}
screenshot := games.Screenshot{Path: path, DestinationName: destinationName.Format(games.DatetimeFormat) + extension}
screenshot := providers.Screenshot{Path: path, DestinationName: destinationName.Format(providers.DatetimeFormat) + extension}
userGames = addScreenshotToGame(userGames, switchGame, screenshot)
}
return nil

View File

@ -6,16 +6,15 @@ import (
"path/filepath"
"time"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
"github.com/rwcarlsen/goexif/exif"
"github.com/fmartingr/games-screenshot-manager/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
func addScreenshotToGame(userGames []providers.Game, gameName string, screenshot providers.Screenshot) []providers.Game {
var foundGame providers.Game
for gameIndex, game := range userGames {
if game.Name == gameName {
foundGame = game
@ -25,7 +24,7 @@ func addScreenshotToGame(userGames []games.Game, gameName string, screenshot gam
// Game not found
if foundGame.Name == "" {
foundGame := games.Game{Name: gameName, ID: gameName, Platform: platformName, Provider: providerName}
foundGame := providers.Game{Name: gameName, ID: gameName, Platform: platformName, Provider: providerName}
foundGame.Screenshots = append(foundGame.Screenshots, screenshot)
userGames = append(userGames, foundGame)
}
@ -33,8 +32,8 @@ func addScreenshotToGame(userGames []games.Game, gameName string, screenshot gam
return userGames
}
func GetGames(cliOptions games.CLIOptions) []games.Game {
var userGames []games.Game
func GetGames(cliOptions providers.ProviderOptions) []providers.Game {
var userGames []providers.Game
err := filepath.Walk(*cliOptions.InputPath,
func(filePath string, info os.FileInfo, err error) error {
@ -63,14 +62,14 @@ func GetGames(cliOptions games.CLIOptions) []games.Game {
defer fileDescriptor.Close()
exifDateTime, _ := exifData.DateTime()
destinationName = exifDateTime.Format(games.DatetimeFormat)
destinationName = exifDateTime.Format(providers.DatetimeFormat)
} else if extension == ".mp4" {
if len(fileName) >= len(layout)+len(extension) {
videoDatetime, err := time.Parse(layout, fileName[len(fileName)-len(extension)-len(layout):len(fileName)-len(extension)])
if err == nil {
destinationName = videoDatetime.Format(games.DatetimeFormat)
destinationName = videoDatetime.Format(providers.DatetimeFormat)
} else {
log.Printf("[Warning] File does not follow datetime convention: %s. (%s) skipping...", fileName, err)
return nil
@ -81,7 +80,7 @@ func GetGames(cliOptions games.CLIOptions) []games.Game {
}
}
screenshot := games.Screenshot{Path: filePath, DestinationName: destinationName + extension}
screenshot := providers.Screenshot{Path: filePath, DestinationName: destinationName + extension}
userGames = addScreenshotToGame(userGames, gameName, screenshot)
}

View File

@ -23,8 +23,8 @@ import (
"strings"
"time"
"github.com/fmartingr/games-screenshot-manager/pkg/games"
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
)
const providerName = "retroarch"
@ -106,8 +106,8 @@ func readPlaylists(playlistsPath string) map[string]RetroArchPlaylist {
return result
}
func findScreenshotsForGame(item RetroArchPlaylistItem) []games.Screenshot {
var result []games.Screenshot
func findScreenshotsForGame(item RetroArchPlaylistItem) []providers.Screenshot {
var result []providers.Screenshot
filePath := filepath.Dir(item.Path)
fileName := strings.Replace(filepath.Base(item.Path), filepath.Ext(item.Path), "", 1)
files, err := ioutil.ReadDir(filePath)
@ -130,43 +130,43 @@ func findScreenshotsForGame(item RetroArchPlaylistItem) []games.Screenshot {
if strings.Contains(file.Name(), "-cheevo-") {
filenameParts := strings.Split(file.Name(), "-")
achievementID := strings.Replace(filenameParts[len(filenameParts)-1], extension, "", 1)
screenshotDestinationName = file.ModTime().Format(games.DatetimeFormat) + "_retroachievement-" + achievementID + extension
screenshotDestinationName = file.ModTime().Format(providers.DatetimeFormat) + "_retroachievement-" + achievementID + extension
} else {
screenshotDate, err := time.Parse(datetimeLayout, file.Name()[len(file.Name())-len(extension)-len(datetimeLayout):len(file.Name())-len(extension)])
if err == nil {
screenshotDestinationName = screenshotDate.Format(games.DatetimeFormat) + extension
screenshotDestinationName = screenshotDate.Format(providers.DatetimeFormat) + extension
} else {
log.Printf("[error] Formatting screenshot %s: %s", file.Name(), err)
continue
}
}
result = append(result, games.Screenshot{Path: filepath.Join(filePath, file.Name()), DestinationName: screenshotDestinationName})
result = append(result, providers.Screenshot{Path: filepath.Join(filePath, file.Name()), DestinationName: screenshotDestinationName})
}
}
return result
}
func GetGames(cliOptions games.CLIOptions) []games.Game {
var userGames []games.Game
func GetGames(cliOptions providers.ProviderOptions) []providers.Game {
var userGames []providers.Game
playlists := readPlaylists(*cliOptions.InputPath)
for playlistName := range playlists {
for _, item := range playlists[playlistName].Items {
var cover games.Screenshot
var cover providers.Screenshot
if *cliOptions.DownloadCovers {
coverURL := formatLibretroBoxartURL(playlistName, item.Label)
boxartPath, err := helpers.DownloadURLIntoTempFile(coverURL)
if err == nil {
cover = games.Screenshot{Path: boxartPath, DestinationName: ".cover"}
cover = providers.Screenshot{Path: boxartPath, DestinationName: ".cover"}
} else {
log.Printf("[error] Error downloading cover for %s: %s", item.Label, err)
}
}
userGames = append(userGames, games.Game{
userGames = append(userGames, providers.Game{
Platform: cleanPlatformName(playlistName),
Name: cleanGameName(item.Label),
Provider: providerName,

View File

@ -3,6 +3,7 @@ package steam
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
@ -11,13 +12,13 @@ import (
"strconv"
"strings"
"github.com/fmartingr/games-screenshot-manager/pkg/games"
"github.com/fmartingr/games-screenshot-manager/pkg/helpers"
"github.com/fmartingr/games-screenshot-manager/pkg/providers"
)
const providerName string = "steam"
const gameListURL string = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"
const baseGameHeaderURL string = "https://cdn.cloudflare.steamstatic.com/steam/apps/{id}/header.jpg"
const baseGameHeaderURL string = "https://cdn.cloudflare.steamstatic.com/steam/apps/%s/header.jpg"
type SteamApp struct {
AppID uint64 `json:"appid"`
@ -29,7 +30,7 @@ type SteamAppList struct {
}
func (appList SteamAppList) FindID(id string) (SteamApp, error) {
GameIDNotFound := errors.New("Game ID not found")
GameIDNotFound := errors.New("game ID not found")
for _, game := range appList.Apps {
uintGameID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
@ -83,13 +84,13 @@ func getSteamAppList(c chan SteamAppList) {
log.Fatal(jsonErr)
}
log.Printf("Updated Steam game list. Found %d games.", len(steamListResponse.AppList.Apps))
log.Printf("Updated Steam game list. Found %d apps.", len(steamListResponse.AppList.Apps))
c <- steamListResponse.AppList
}
func downloadGameHeaderImage(appId string) (string, error) {
url := "https://cdn.cloudflare.steamstatic.com/steam/apps/" + appId + "/header.jpg"
url := fmt.Sprintf(baseGameHeaderURL, appId)
coverURL, err := helpers.DownloadURLIntoTempFile(url)
if err != nil {
return "", err
@ -137,7 +138,7 @@ func GetGamesFromUser(user string) []string {
return userGames
}
func GetScreenshotsForGame(user string, game *games.Game) {
func GetScreenshotsForGame(user string, game *providers.Game) {
path := filepath.Join(getBasePathForOS(), "userdata", user, "/760/remote/", game.ID, "screenshots")
files, err := ioutil.ReadDir(path)
if err != nil {
@ -146,7 +147,7 @@ func GetScreenshotsForGame(user string, game *games.Game) {
for _, file := range files {
if strings.Contains(file.Name(), ".jpg") {
game.Screenshots = append(game.Screenshots, games.Screenshot{Path: path + "/" + file.Name()})
game.Screenshots = append(game.Screenshots, providers.Screenshot{Path: path + "/" + file.Name()})
// log.Printf("Found screenshot for user %s and game %d: %s", user, game.ID, path+"/"+file.Name())
}
}
@ -156,8 +157,8 @@ func GetScreenshotsForGame(user string, game *games.Game) {
}
}
func GetGames(cliOptions games.CLIOptions) []games.Game {
var localGames []games.Game
func GetGames(cliOptions providers.ProviderOptions) []providers.Game {
var localGames []providers.Game
c := make(chan SteamAppList)
go getSteamAppList(c)
users := GuessUsers()
@ -169,12 +170,12 @@ func GetGames(cliOptions games.CLIOptions) []games.Game {
if err != nil {
log.Print("[ERROR] Steam game ID not found: ", userGameID)
}
userGame := games.Game{ID: userGameID, Name: steamGame.Name, Provider: providerName, Platform: "PC"}
userGame := providers.Game{ID: userGameID, Name: steamGame.Name, Provider: providerName, Platform: "PC"}
if *cliOptions.DownloadCovers {
coverPath, err := downloadGameHeaderImage(userGameID)
if err == nil {
userGame.Cover = games.Screenshot{Path: coverPath, DestinationName: ".cover"}
userGame.Cover = providers.Screenshot{Path: coverPath, DestinationName: ".cover"}
}
}

View File

@ -1,4 +1,4 @@
package games
package providers
import (
"log"
@ -8,7 +8,7 @@ import (
const DatetimeFormat = "2006-01-02_15-04-05"
type CLIOptions struct {
type ProviderOptions struct {
OutputPath *string
InputPath *string
DownloadCovers *bool