Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Nov 21, 2021
2 parents 582d4aa + 3cd50bf commit 081da04
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ again when you get more games or want to update the category overlays.
* Add the extension `.logo`/`_hero` before the image extension for logo art `Psychonauts.logo.png`, `3830_logo.png`
4. *(optional)* Generate a some API Keys to enhance the automatic search:
* [SteamGridDB API Key](https://www.steamgriddb.com/profile/preferences)
* [IGDB API Key](https://api.igdb.com/signup)
* [IGDB API Client/Secret](https://api-docs.igdb.com/#about)
5. Run `steamgrid` and wait. No, really, it's all automatic. Not a single key press required.
* *(optional)* Append `--steamgriddb <api key>` if you've generated one before.
* *(optional)* Append `--igdb <api key>` if you've generated one before.
* *(optional)* Append `--igdbclient <igdb client>` if you've genereated one before.
* *(optional)* Append `--igdbsecret <igdb secret>` if you've genereated one before.
* *(optional)* Append `--types <preference>` to choose your preferences between animated steam covers or static ones Available choices : `animated`,`static`. Default : `static`. You can use `animated,static` to download both while preferring animated covers, and `static,animated` for preferring static covers.
* *(optional)* Append `--styles <preference>` to choose your preferences between the different covers styles from steamgriddb. Available choices : `material`,`white_logo`,`alternate`,`blurred`,`no_logo`. Default: `alternate`. You can also input multiple comma-separated choices in the same manners of the `--types` argument.
* *(optional)* Append `--appids <appid1,appid2>` to only process the specified appID(s)
Expand Down
51 changes: 39 additions & 12 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ func getSteamGridDBImage(game *Game, artStyleExtensions []string, steamGridDBApi
}

const igdbImageURL = "https://images.igdb.com/igdb/image/upload/t_720p/%v.jpg"
const igdbGameURL = "https://api-v3.igdb.com/games"
const igdbCoverURL = "https://api-v3.igdb.com/covers"
const igdbGameURL = "https://api.igdb.com/v4/games"
const igdbCoverURL = "https://api.igdb.com/v4/covers"
const igdbGameBody = `fields name,cover; search "%v";`
const igdbCoverBody = `fields image_id; where id = %v;`

Expand All @@ -243,10 +243,37 @@ type igdbCover struct {
Image_ID string
}

func igdbPostRequest(url string, body string, IGDBApiKey string) ([]byte, error) {
func igdbPostRequest(url string, body string, IGDBSecret string, IGDBClient string) ([]byte, error) {

tokenClient := &http.Client{}
reqq, err := http.NewRequest("POST", "https://id.twitch.tv/oauth2/token?client_id="+IGDBClient+"&client_secret="+IGDBSecret+"&grant_type=client_credentials", strings.NewReader(body))
tokenResponse, err := tokenClient.Do(reqq)
if err != nil {
return nil, err
}

tokenBody, err := ioutil.ReadAll(tokenResponse.Body)

if err != nil {
return nil, err
}

type token struct {
String string "json:\"access_token\""
}

token1 := token{}

jsonErr := json.Unmarshal(tokenBody, &token1)

if jsonErr != nil {
return nil, jsonErr
}

client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Add("user-key", IGDBApiKey)
req.Header.Add("Client-ID", IGDBClient)
req.Header.Add("Authorization", "Bearer "+token1.String)
req.Header.Add("Accept", "application/json")
if err != nil {
return nil, err
Expand All @@ -266,8 +293,8 @@ func igdbPostRequest(url string, body string, IGDBApiKey string) ([]byte, error)
return responseBytes, nil
}

func getIGDBImage(gameName string, IGDBApiKey string) (string, error) {
responseBytes, err := igdbPostRequest(igdbGameURL, fmt.Sprintf(igdbGameBody, gameName), IGDBApiKey)
func getIGDBImage(gameName string, IGDBSecret string, IGDBClient string) (string, error) {
responseBytes, err := igdbPostRequest(igdbGameURL, fmt.Sprintf(igdbGameBody, gameName), IGDBSecret, IGDBClient)
if err != nil {
return "", err
}
Expand All @@ -282,7 +309,7 @@ func getIGDBImage(gameName string, IGDBApiKey string) (string, error) {
return "", nil
}

responseBytes, err = igdbPostRequest(igdbCoverURL, fmt.Sprintf(igdbCoverBody, jsonGameResponse[0].Cover), IGDBApiKey)
responseBytes, err = igdbPostRequest(igdbCoverURL, fmt.Sprintf(igdbCoverBody, jsonGameResponse[0].Cover), IGDBSecret, IGDBClient)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -329,7 +356,7 @@ const steamCdnURLFormat = `cdn.akamai.steamstatic.com/steam/apps/%v/`
// sources. Returns the final response received and a flag indicating if it was
// from a Google search (useful because we want to log the lower quality
// images).
func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []string, skipSteam bool, steamGridDBApiKey string, IGDBApiKey string, skipGoogle bool, onlyMissingArtwork bool) (response *http.Response, from string, err error) {
func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []string, skipSteam bool, steamGridDBApiKey string, IGDBSecret string, IGDBClient string, skipGoogle bool, onlyMissingArtwork bool) (response *http.Response, from string, err error) {
from = "steam server"
if !skipSteam {
response, err = tryDownload(fmt.Sprintf(akamaiURLFormat+artStyleExtensions[2], game.ID))
Expand Down Expand Up @@ -361,9 +388,9 @@ func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []stri
}

// IGDB has mostly cover styles
if artStyle == "Cover" && IGDBApiKey != "" && url == "" {
if artStyle == "Cover" && IGDBClient != "" && IGDBSecret != "" && url == "" {
from = "IGDB"
url, err = getIGDBImage(game.Name, IGDBApiKey)
url, err = getIGDBImage(game.Name, IGDBSecret, IGDBClient)
if err != nil {
return
}
Expand All @@ -389,8 +416,8 @@ func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []stri
// DownloadImage tries to download the game images, saving it in game.ImageBytes. Returns
// flags indicating if the operation succeeded and if the image downloaded was
// from a search.
func DownloadImage(gridDir string, game *Game, artStyle string, artStyleExtensions []string, skipSteam bool, steamGridDBApiKey string, IGDBApiKey string, skipGoogle bool, onlyMissingArtwork bool) (string, error) {
response, from, err := getImageAlternatives(game, artStyle, artStyleExtensions, skipSteam, steamGridDBApiKey, IGDBApiKey, skipGoogle, onlyMissingArtwork)
func DownloadImage(gridDir string, game *Game, artStyle string, artStyleExtensions []string, skipSteam bool, steamGridDBApiKey string, IGDBSecret string, IGDBClient string, skipGoogle bool, onlyMissingArtwork bool) (string, error) {
response, from, err := getImageAlternatives(game, artStyle, artStyleExtensions, skipSteam, steamGridDBApiKey, IGDBSecret, IGDBClient, skipGoogle, onlyMissingArtwork)
if response == nil || err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions steamgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func main() {

func startApplication() {
steamGridDBApiKey := flag.String("steamgriddb", "", "Your personal SteamGridDB api key, get one here: https://www.steamgriddb.com/profile/preferences")
IGDBApiKey := flag.String("igdb", "", "Your personal IGDB api key, get one here: https://api.igdb.com/signup")
IGDBSecret := flag.String("igdbsecret", "", "Your personal IGDB api key, get one here: https://api.igdb.com/signup")
IGDBClient := flag.String("igdbclient", "", "Your personal IGDB api key, get one here: https://api.igdb.com/signup")
steamDir := flag.String("steamdir", "", "Path to your steam installation")
// "alternate" "blurred" "white_logo" "material" "no_logo"
steamGridDBStyles := flag.String("styles", "alternate", "Comma separated list of styles to download from SteamGridDB.\nExample: \"white_logo,material\"")
Expand Down Expand Up @@ -200,7 +201,7 @@ func startApplication() {
// Download if missing.
///////////////////////
if game.ImageSource == "" {
from, err := DownloadImage(gridDir, game, artStyle, artStyleExtensions, *skipSteam, *steamGridDBApiKey, *IGDBApiKey, *skipGoogle, *onlyMissingArtwork)
from, err := DownloadImage(gridDir, game, artStyle, artStyleExtensions, *skipSteam, *steamGridDBApiKey, *IGDBSecret, *IGDBClient, *skipGoogle, *onlyMissingArtwork)
if err != nil && err.Error() == "SteamGridDB authorization token is missing or invalid" {
// Wrong api key
*steamGridDBApiKey = ""
Expand Down

0 comments on commit 081da04

Please sign in to comment.