Download steam game covers (optional flag)
This commit is contained in:
parent
ae632293f9
commit
ae6f7372a2
|
@ -23,27 +23,31 @@ const defaultOutputPath string = "./Output"
|
||||||
const defaultInputPath string = ""
|
const defaultInputPath string = ""
|
||||||
const defaultProvider string = "steam"
|
const defaultProvider string = "steam"
|
||||||
const defaultDryRun bool = false
|
const defaultDryRun bool = false
|
||||||
|
const defaultDownloadCovers bool = false
|
||||||
|
|
||||||
|
// TODO: Set CLI options into an options struct
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
var provider = flag.String("provider", defaultProvider, "steam")
|
var provider = flag.String("provider", defaultProvider, "steam")
|
||||||
var outputPath = flag.String("output-path", defaultOutputPath, "The destination path of the screenshots")
|
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 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")
|
var dryRun = flag.Bool("dry-run", defaultDryRun, "Use to disable write actions on filesystem")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if helpers.SliceContainsString(allowedProviders[:], *provider, nil) {
|
if helpers.SliceContainsString(allowedProviders[:], *provider, nil) {
|
||||||
games := getGamesFromProvider(*provider, *inputPath)
|
games := getGamesFromProvider(*provider, *inputPath, *downloadCovers)
|
||||||
processGames(games, *outputPath, *dryRun)
|
processGames(games, *outputPath, *dryRun, *downloadCovers)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Provider %s not found!", *provider)
|
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
|
var games []games.Game
|
||||||
switch provider {
|
switch provider {
|
||||||
case "steam":
|
case "steam":
|
||||||
games = append(games, steam.GetGames()...)
|
games = append(games, steam.GetGames(downloadCovers)...)
|
||||||
case "minecraft":
|
case "minecraft":
|
||||||
games = append(games, minecraft.GetGames()...)
|
games = append(games, minecraft.GetGames()...)
|
||||||
case "nintendo-switch":
|
case "nintendo-switch":
|
||||||
|
@ -52,7 +56,7 @@ func getGamesFromProvider(provider string, inputPath string) []games.Game {
|
||||||
return games
|
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 {
|
for _, game := range games {
|
||||||
destinationPath := filepath.Join(helpers.ExpandUser(outputPath), game.Platform)
|
destinationPath := filepath.Join(helpers.ExpandUser(outputPath), game.Platform)
|
||||||
if len(game.Name) > 0 {
|
if len(game.Name) > 0 {
|
||||||
|
@ -67,6 +71,14 @@ func processGames(games []games.Game, outputPath string, dryRun bool) {
|
||||||
os.MkdirAll(destinationPath, 0711)
|
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)
|
log.Printf("=> Proceesing screenshots for %s %s", game.Name, game.Notes)
|
||||||
for _, screenshot := range game.Screenshots {
|
for _, screenshot := range game.Screenshots {
|
||||||
destinationPath := filepath.Join(destinationPath, screenshot.GetDestinationName())
|
destinationPath := filepath.Join(destinationPath, screenshot.GetDestinationName())
|
||||||
|
|
|
@ -15,6 +15,7 @@ type Game struct {
|
||||||
Provider string
|
Provider string
|
||||||
Screenshots []Screenshot
|
Screenshots []Screenshot
|
||||||
Notes string
|
Notes string
|
||||||
|
Cover Screenshot
|
||||||
}
|
}
|
||||||
|
|
||||||
type Screenshot struct {
|
type Screenshot struct {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
|
|
||||||
const providerName string = "steam"
|
const providerName string = "steam"
|
||||||
const gameListURL string = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"
|
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 {
|
type SteamApp struct {
|
||||||
AppID uint64 `json:"appid"`
|
AppID uint64 `json:"appid"`
|
||||||
|
@ -87,6 +88,31 @@ func getSteamAppList(c chan SteamAppList) {
|
||||||
c <- steamListResponse.AppList
|
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 {
|
func GuessUsers() []string {
|
||||||
var users []string
|
var users []string
|
||||||
var path string = filepath.Join(getBasePathForOS(), "userdata")
|
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
|
var localGames []games.Game
|
||||||
c := make(chan SteamAppList)
|
c := make(chan SteamAppList)
|
||||||
go getSteamAppList(c)
|
go getSteamAppList(c)
|
||||||
|
@ -160,7 +186,12 @@ func GetGames() []games.Game {
|
||||||
log.Print("[ERROR] Steam game ID not found: ", userGameID)
|
log.Print("[ERROR] Steam game ID not found: ", userGameID)
|
||||||
}
|
}
|
||||||
userGame := games.Game{ID: userGameID, Name: steamGame.Name, Provider: providerName, Platform: "PC"}
|
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)
|
GetScreenshotsForGame(userID, &userGame)
|
||||||
localGames = append(localGames, userGame)
|
localGames = append(localGames, userGame)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue