Skip to content

Commit

Permalink
update igdb to api v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Firestorm7893 authored and boppreh committed Nov 1, 2021
1 parent c796e61 commit 0765b6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
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 0765b6c

Please sign in to comment.