Download steam game covers (optional flag)

This commit is contained in:
Felipe M 2021-01-05 15:19:21 +01:00
parent ae632293f9
commit ae6f7372a2
3 changed files with 51 additions and 7 deletions

View File

@ -23,27 +23,31 @@ const defaultOutputPath string = "./Output"
const defaultInputPath string = ""
const defaultProvider string = "steam"
const defaultDryRun bool = false
const defaultDownloadCovers bool = false
// TODO: Set CLI options into an options struct
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 downloadCovers = flag.Bool("download-covers", defaultDownloadCovers, "use to enable the download of covers (if the provider supports 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, *inputPath)
processGames(games, *outputPath, *dryRun)
games := getGamesFromProvider(*provider, *inputPath, *downloadCovers)
processGames(games, *outputPath, *dryRun, *downloadCovers)
} else {
log.Printf("Provider %s not found!", *provider)
}
}
func getGamesFromProvider(provider string, inputPath string) []games.Game {
func getGamesFromProvider(provider string, inputPath string, downloadCovers bool) []games.Game {
var games []games.Game
switch provider {
case "steam":
games = append(games, steam.GetGames()...)
games = append(games, steam.GetGames(downloadCovers)...)
case "minecraft":
games = append(games, minecraft.GetGames()...)
case "nintendo-switch":
@ -52,7 +56,7 @@ func getGamesFromProvider(provider string, inputPath string) []games.Game {
return games
}
func processGames(games []games.Game, outputPath string, dryRun bool) {
func processGames(games []games.Game, outputPath string, dryRun bool, downloadCovers bool) {
for _, game := range games {
destinationPath := filepath.Join(helpers.ExpandUser(outputPath), game.Platform)
if len(game.Name) > 0 {
@ -67,6 +71,14 @@ func processGames(games []games.Game, outputPath string, dryRun bool) {
os.MkdirAll(destinationPath, 0711)
}
if downloadCovers && !dryRun {
destinationCoverPath := filepath.Join(destinationPath, game.Cover.DestinationName)
if _, err := os.Stat(destinationCoverPath); os.IsNotExist(err) {
helpers.CopyFile(game.Cover.Path, destinationCoverPath)
}
}
log.Printf("=> Proceesing screenshots for %s %s", game.Name, game.Notes)
for _, screenshot := range game.Screenshots {
destinationPath := filepath.Join(destinationPath, screenshot.GetDestinationName())

View File

@ -15,6 +15,7 @@ type Game struct {
Provider string
Screenshots []Screenshot
Notes string
Cover Screenshot
}
type Screenshot struct {

View File

@ -17,6 +17,7 @@ import (
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"
type SteamApp struct {
AppID uint64 `json:"appid"`
@ -87,6 +88,31 @@ func getSteamAppList(c chan SteamAppList) {
c <- steamListResponse.AppList
}
func downloadGameHeaderImage(appId string) string {
response, err := helpers.DoRequest("GET", "https://cdn.cloudflare.steamstatic.com/steam/apps/"+appId+"/header.jpg")
if err != nil {
panic(err)
}
if response.Body != nil {
defer response.Body.Close()
}
tmpfile, err := ioutil.TempFile("", "games-screenshot-manager-cover")
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
tmpfile.Write(body)
return tmpfile.Name()
}
func GuessUsers() []string {
var users []string
var path string = filepath.Join(getBasePathForOS(), "userdata")
@ -146,7 +172,7 @@ func GetScreenshotsForGame(user string, game *games.Game) {
}
}
func GetGames() []games.Game {
func GetGames(downloadCovers bool) []games.Game {
var localGames []games.Game
c := make(chan SteamAppList)
go getSteamAppList(c)
@ -160,7 +186,12 @@ func GetGames() []games.Game {
log.Print("[ERROR] Steam game ID not found: ", userGameID)
}
userGame := games.Game{ID: userGameID, Name: steamGame.Name, Provider: providerName, Platform: "PC"}
log.Printf("Found Steam game for user %s: %s (%d)", userID, userGame.Name, userGame.ID)
if downloadCovers {
userGame.Cover = games.Screenshot{Path: downloadGameHeaderImage(userGameID), DestinationName: ".cover"}
}
log.Printf("Found Steam game for user %s: %s (%s)", userID, userGame.Name, userGame.ID)
GetScreenshotsForGame(userID, &userGame)
localGames = append(localGames, userGame)
}