@@ -227,8 +227,8 @@ func getSteamGridDBImage(game *Game, artStyleExtensions []string, steamGridDBApi
227
227
}
228
228
229
229
const igdbImageURL = "https://images.igdb.com/igdb/image/upload/t_720p/%v.jpg"
230
- const igdbGameURL = "https://api-v3 .igdb.com/games"
231
- const igdbCoverURL = "https://api-v3 .igdb.com/covers"
230
+ const igdbGameURL = "https://api.igdb.com/v4 /games"
231
+ const igdbCoverURL = "https://api.igdb.com/v4 /covers"
232
232
const igdbGameBody = `fields name,cover; search "%v";`
233
233
const igdbCoverBody = `fields image_id; where id = %v;`
234
234
@@ -243,10 +243,37 @@ type igdbCover struct {
243
243
Image_ID string
244
244
}
245
245
246
- func igdbPostRequest (url string , body string , IGDBApiKey string ) ([]byte , error ) {
246
+ func igdbPostRequest (url string , body string , IGDBSecret string , IGDBClient string ) ([]byte , error ) {
247
+
248
+ tokenClient := & http.Client {}
249
+ reqq , err := http .NewRequest ("POST" , "https://id.twitch.tv/oauth2/token?client_id=" + IGDBClient + "&client_secret=" + IGDBSecret + "&grant_type=client_credentials" , strings .NewReader (body ))
250
+ tokenResponse , err := tokenClient .Do (reqq )
251
+ if err != nil {
252
+ return nil , err
253
+ }
254
+
255
+ tokenBody , err := ioutil .ReadAll (tokenResponse .Body )
256
+
257
+ if err != nil {
258
+ return nil , err
259
+ }
260
+
261
+ type token struct {
262
+ String string "json:\" access_token\" "
263
+ }
264
+
265
+ token1 := token {}
266
+
267
+ jsonErr := json .Unmarshal (tokenBody , & token1 )
268
+
269
+ if jsonErr != nil {
270
+ return nil , jsonErr
271
+ }
272
+
247
273
client := & http.Client {}
248
274
req , err := http .NewRequest ("POST" , url , strings .NewReader (body ))
249
- req .Header .Add ("user-key" , IGDBApiKey )
275
+ req .Header .Add ("Client-ID" , IGDBClient )
276
+ req .Header .Add ("Authorization" , "Bearer " + token1 .String )
250
277
req .Header .Add ("Accept" , "application/json" )
251
278
if err != nil {
252
279
return nil , err
@@ -266,8 +293,8 @@ func igdbPostRequest(url string, body string, IGDBApiKey string) ([]byte, error)
266
293
return responseBytes , nil
267
294
}
268
295
269
- func getIGDBImage (gameName string , IGDBApiKey string ) (string , error ) {
270
- responseBytes , err := igdbPostRequest (igdbGameURL , fmt .Sprintf (igdbGameBody , gameName ), IGDBApiKey )
296
+ func getIGDBImage (gameName string , IGDBSecret string , IGDBClient string ) (string , error ) {
297
+ responseBytes , err := igdbPostRequest (igdbGameURL , fmt .Sprintf (igdbGameBody , gameName ), IGDBSecret , IGDBClient )
271
298
if err != nil {
272
299
return "" , err
273
300
}
@@ -282,7 +309,7 @@ func getIGDBImage(gameName string, IGDBApiKey string) (string, error) {
282
309
return "" , nil
283
310
}
284
311
285
- responseBytes , err = igdbPostRequest (igdbCoverURL , fmt .Sprintf (igdbCoverBody , jsonGameResponse [0 ].Cover ), IGDBApiKey )
312
+ responseBytes , err = igdbPostRequest (igdbCoverURL , fmt .Sprintf (igdbCoverBody , jsonGameResponse [0 ].Cover ), IGDBSecret , IGDBClient )
286
313
if err != nil {
287
314
return "" , err
288
315
}
@@ -329,7 +356,7 @@ const steamCdnURLFormat = `cdn.akamai.steamstatic.com/steam/apps/%v/`
329
356
// sources. Returns the final response received and a flag indicating if it was
330
357
// from a Google search (useful because we want to log the lower quality
331
358
// images).
332
- 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 ) {
359
+ 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 ) {
333
360
from = "steam server"
334
361
if ! skipSteam {
335
362
response , err = tryDownload (fmt .Sprintf (akamaiURLFormat + artStyleExtensions [2 ], game .ID ))
@@ -361,9 +388,9 @@ func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []stri
361
388
}
362
389
363
390
// IGDB has mostly cover styles
364
- if artStyle == "Cover" && IGDBApiKey != "" && url == "" {
391
+ if artStyle == "Cover" && IGDBClient != "" && IGDBSecret != "" && url == "" {
365
392
from = "IGDB"
366
- url , err = getIGDBImage (game .Name , IGDBApiKey )
393
+ url , err = getIGDBImage (game .Name , IGDBSecret , IGDBClient )
367
394
if err != nil {
368
395
return
369
396
}
@@ -389,8 +416,8 @@ func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []stri
389
416
// DownloadImage tries to download the game images, saving it in game.ImageBytes. Returns
390
417
// flags indicating if the operation succeeded and if the image downloaded was
391
418
// from a search.
392
- func DownloadImage (gridDir string , game * Game , artStyle string , artStyleExtensions []string , skipSteam bool , steamGridDBApiKey string , IGDBApiKey string , skipGoogle bool , onlyMissingArtwork bool ) (string , error ) {
393
- response , from , err := getImageAlternatives (game , artStyle , artStyleExtensions , skipSteam , steamGridDBApiKey , IGDBApiKey , skipGoogle , onlyMissingArtwork )
419
+ func DownloadImage (gridDir string , game * Game , artStyle string , artStyleExtensions []string , skipSteam bool , steamGridDBApiKey string , IGDBSecret string , IGDBClient string , skipGoogle bool , onlyMissingArtwork bool ) (string , error ) {
420
+ response , from , err := getImageAlternatives (game , artStyle , artStyleExtensions , skipSteam , steamGridDBApiKey , IGDBSecret , IGDBClient , skipGoogle , onlyMissingArtwork )
394
421
if response == nil || err != nil {
395
422
return "" , err
396
423
}
0 commit comments