Skip to content

Commit

Permalink
statisfy golint
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucki authored and boppreh committed Mar 1, 2021
1 parent 0cab526 commit cb81b59
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 119 deletions.
22 changes: 11 additions & 11 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// BackupGame if a game has a custom image, backs it up by appending "(original)" to the
// file name.
func BackupGame(gridDir string, game *Game, artStyleExtensions []string) error {
func backupGame(gridDir string, game *Game, artStyleExtensions []string) error {
if game.CleanImageBytes != nil {
return ioutil.WriteFile(getBackupPath(gridDir, game, artStyleExtensions), game.CleanImageBytes, 0666)
}
Expand All @@ -25,17 +25,17 @@ func getBackupPath(gridDir string, game *Game, artStyleExtensions []string) stri
hash := sha256.Sum256(game.OverlayImageBytes)
// [:] is required to convert a fixed length byte array to a byte slice.
hexHash := hex.EncodeToString(hash[:])
return filepath.Join(gridDir, "originals", game.ID + artStyleExtensions[0] + " " + hexHash+game.ImageExt)
return filepath.Join(gridDir, "originals", game.ID+artStyleExtensions[0]+" "+hexHash+game.ImageExt)
}

func RemoveExisting(gridDir string, gameId string, artStyleExtensions []string) error {
images, err := filepath.Glob(filepath.Join(gridDir, gameId + artStyleExtensions[0] + ".*"))
func removeExisting(gridDir string, gameID string, artStyleExtensions []string) error {
images, err := filepath.Glob(filepath.Join(gridDir, gameID+artStyleExtensions[0]+".*"))
if err != nil {
return err
}
images = filterForImages(images)

backups, err := filepath.Glob(filepath.Join(gridDir, "originals", gameId + artStyleExtensions[0] + " *.*"))
backups, err := filepath.Glob(filepath.Join(gridDir, "originals", gameID+artStyleExtensions[0]+" *.*"))
if err != nil {
return err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func loadImage(game *Game, sourceName string, imagePath string) error {
}

// https://wenzr.wordpress.com/2018/04/09/go-glob-case-insensitive/
func InsensitiveFilepath(path string) string {
func insensitiveFilepath(path string) string {
if runtime.GOOS == "windows" {
return path
}
Expand Down Expand Up @@ -95,8 +95,8 @@ func filterForImages(paths []string) []string {
return matchedPaths
}

func LoadExisting(overridePath string, gridDir string, game *Game, artStyleExtensions []string) {
overridenIDs, _ := filepath.Glob(filepath.Join(overridePath, game.ID + artStyleExtensions[0] + ".*"))
func loadExisting(overridePath string, gridDir string, game *Game, artStyleExtensions []string) {
overridenIDs, _ := filepath.Glob(filepath.Join(overridePath, game.ID+artStyleExtensions[0]+".*"))
if overridenIDs != nil && len(overridenIDs) > 0 {
loadImage(game, "local file in directory 'games'", overridenIDs[0])
return
Expand All @@ -106,15 +106,15 @@ func LoadExisting(overridePath string, gridDir string, game *Game, artStyleExten
if game.Name != "" {
re := regexp.MustCompile(`\W+`)
globName := re.ReplaceAllString(game.Name, "*")
overridenNames, _ := filepath.Glob(filepath.Join(overridePath, InsensitiveFilepath(globName) + artStyleExtensions[1] + ".*"))
overridenNames, _ := filepath.Glob(filepath.Join(overridePath, insensitiveFilepath(globName)+artStyleExtensions[1]+".*"))
if overridenNames != nil && len(overridenNames) > 0 {
loadImage(game, "local file in directory games/", overridenNames[0])
return
}
}

// If there are any old-style backups (without hash), load them over the existing (with overlay) images.
oldBackups, err := filepath.Glob(filepath.Join(gridDir, game.ID + artStyleExtensions[0] + " (original)*"))
oldBackups, err := filepath.Glob(filepath.Join(gridDir, game.ID+artStyleExtensions[0]+" (original)*"))
if err == nil && len(oldBackups) > 0 {
err = loadImage(game, "legacy backup (now converted)", oldBackups[0])
if err == nil {
Expand All @@ -123,7 +123,7 @@ func LoadExisting(overridePath string, gridDir string, game *Game, artStyleExten
}
}

files, err := filepath.Glob(filepath.Join(gridDir, game.ID + artStyleExtensions[0] + ".*"))
files, err := filepath.Glob(filepath.Join(gridDir, game.ID+artStyleExtensions[0]+".*"))
files = filterForImages(files)
if err == nil && len(files) > 0 {
err = loadImage(game, "manual customization", files[0])
Expand Down
129 changes: 64 additions & 65 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,43 @@ func getGoogleImage(gameName string, artStyleExtensions []string) (string, error
}

// https://www.steamgriddb.com/api/v2
type SteamGridDBResponse struct {
type steamGridDBResponse struct {
Success bool
Data []struct {
Id int
Score int
Style string
Url string
Thumb string
Tags []string
Data []struct {
ID int
Score int
Style string
URL string
Thumb string
Tags []string
Author struct {
Name string
Name string
Steam64 string
Avatar string
Avatar string
}
}
}

type SteamGridDBSearchResponse struct {
type steamGridDBSearchResponse struct {
Success bool
Data []struct {
Id int
Name string
Types []string
Data []struct {
ID int
Name string
Types []string
Verified bool
}
}

// Search SteamGridDB for cover image
const SteamGridDBBaseURL = "https://www.steamgriddb.com/api/v2"
const steamGridDBBaseURL = "https://www.steamgriddb.com/api/v2"

func SteamGridDBGetRequest(url string, steamGridDBApiKey string) ([]byte, error) {
func steamGridDBGetRequest(url string, steamGridDBApiKey string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer " + steamGridDBApiKey)
req.Header.Add("Authorization", "Bearer "+steamGridDBApiKey)

response, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -130,67 +130,66 @@ func getSteamGridDBImage(game *Game, artStyleExtensions []string, steamGridDBApi
// Try for HQ, then for LQ
// It's possible to request both dimensions in one go but that'll give us scrambled results with no indicator which result has which size.
for i := 0; i < 3; i += 2 {
filter := steamGridFilter + "&dimensions=" + artStyleExtensions[3 + i] + "x" + artStyleExtensions[4 + i]
filter := steamGridFilter + "&dimensions=" + artStyleExtensions[3+i] + "x" + artStyleExtensions[4+i]

// Try with game.ID which is probably steams appID
var baseUrl string
var baseURL string
switch artStyleExtensions[1] {
case ".banner":
baseUrl = SteamGridDBBaseURL + "/grids"
baseURL = steamGridDBBaseURL + "/grids"
case ".cover":
baseUrl = SteamGridDBBaseURL + "/grids"
baseURL = steamGridDBBaseURL + "/grids"
case ".hero":
baseUrl = SteamGridDBBaseURL + "/heroes"
baseURL = steamGridDBBaseURL + "/heroes"
case ".logo":
baseUrl = SteamGridDBBaseURL + "/logos"
baseURL = steamGridDBBaseURL + "/logos"
}
url := baseUrl + "/steam/" + game.ID + filter
url := baseURL + "/steam/" + game.ID + filter

var jsonResponse SteamGridDBResponse
var jsonResponse steamGridDBResponse
var responseBytes []byte
var err error

// Skip requests with appID for custom games
if !game.Custom {
responseBytes, err = SteamGridDBGetRequest(url, steamGridDBApiKey)
responseBytes, err = steamGridDBGetRequest(url, steamGridDBApiKey)
} else {
err = errors.New("404")
}

// Authorization token is missing or invalid
if err != nil && err.Error() == "401" {
if err != nil && err.Error() == "401" {
return "", errors.New("SteamGridDB authorization token is missing or invalid")
// Could not find game with that id
// Could not find game with that id
} else if err != nil && err.Error() == "404" {
// Try searching for the name…
url = SteamGridDBBaseURL + "/search/autocomplete/" + game.Name + filter
responseBytes, err = SteamGridDBGetRequest(url, steamGridDBApiKey)
url = steamGridDBBaseURL + "/search/autocomplete/" + game.Name + filter
responseBytes, err = steamGridDBGetRequest(url, steamGridDBApiKey)
if err != nil && err.Error() == "401" {
return "", errors.New("SteamGridDB authorization token is missing or invalid")
} else if err != nil {
return "", err
}

var jsonSearchResponse SteamGridDBSearchResponse
var jsonSearchResponse steamGridDBSearchResponse
err = json.Unmarshal(responseBytes, &jsonSearchResponse)
if err != nil {
return "", errors.New("Best search match doesn't has a requested type or style")
}

SteamGridDBGameId := -1
SteamGridDBGameID := -1
if jsonSearchResponse.Success && len(jsonSearchResponse.Data) >= 1 {
// First match should be the best one
SteamGridDBGameId = jsonSearchResponse.Data[0].Id
SteamGridDBGameID = jsonSearchResponse.Data[0].ID
}

if SteamGridDBGameId == -1 {
if SteamGridDBGameID == -1 {
return "", nil
}


// …and get the url of the top result.
url = baseUrl + "/game/" + strconv.Itoa(SteamGridDBGameId) + filter
responseBytes, err = SteamGridDBGetRequest(url, steamGridDBApiKey)
url = baseURL + "/game/" + strconv.Itoa(SteamGridDBGameID) + filter
responseBytes, err = steamGridDBGetRequest(url, steamGridDBApiKey)
if err != nil {
return "", err
}
Expand All @@ -204,31 +203,31 @@ func getSteamGridDBImage(game *Game, artStyleExtensions []string, steamGridDBApi
}

if jsonResponse.Success && len(jsonResponse.Data) >= 1 {
return jsonResponse.Data[0].Url, nil
return jsonResponse.Data[0].URL, nil
}
}

return "", nil
}

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 IGDBGameBody = `fields name,cover; search "%v";`
const IGDBCoverBody = `fields image_id; where id = %v;`
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 igdbGameBody = `fields name,cover; search "%v";`
const igdbCoverBody = `fields image_id; where id = %v;`

type IGDBGame struct {
Id int
type igdbGame struct {
ID int
Cover int
Name string
Name string
}

type IGDBCover struct {
Id int
Image_id string
type igdbCover struct {
ID int
Image_ID string
}

func IGDBPostRequest(url string, body string, IGDBApiKey string) ([]byte, error) {
func igdbPostRequest(url string, body string, IGDBApiKey string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(body))
req.Header.Add("user-key", IGDBApiKey)
Expand All @@ -252,12 +251,12 @@ func IGDBPostRequest(url string, body string, IGDBApiKey string) ([]byte, error)
}

func getIGDBImage(gameName string, IGDBApiKey string) (string, error) {
responseBytes, err := IGDBPostRequest(IGDBGameURL, fmt.Sprintf(IGDBGameBody, gameName), IGDBApiKey)
responseBytes, err := igdbPostRequest(igdbGameURL, fmt.Sprintf(igdbGameBody, gameName), IGDBApiKey)
if err != nil {
return "", err
}

var jsonGameResponse []IGDBGame
var jsonGameResponse []igdbGame
err = json.Unmarshal(responseBytes, &jsonGameResponse)
if err != nil {
return "", nil
Expand All @@ -267,19 +266,19 @@ 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), IGDBApiKey)
if err != nil {
return "", err
}

var jsonCoverResponse []IGDBCover
var jsonCoverResponse []igdbCover
err = json.Unmarshal(responseBytes, &jsonCoverResponse)
if err != nil {
return "", nil
}

if len(jsonCoverResponse) >= 1 {
return fmt.Sprintf(IGDBImageURL, jsonCoverResponse[0].Image_id), nil
return fmt.Sprintf(igdbImageURL, jsonCoverResponse[0].Image_ID), nil
}

return "", nil
Expand Down Expand Up @@ -317,12 +316,12 @@ const steamCdnURLFormat = `cdn.akamai.steamstatic.com/steam/apps/%v/`
func getImageAlternatives(game *Game, artStyle string, artStyleExtensions []string, skipSteam bool, steamGridDBApiKey string, steamGridFilter string, IGDBApiKey string, skipGoogle bool) (response *http.Response, from string, err error) {
from = "steam server"
if !skipSteam {
response, err = tryDownload(fmt.Sprintf(akamaiURLFormat + artStyleExtensions[2], game.ID))
response, err = tryDownload(fmt.Sprintf(akamaiURLFormat+artStyleExtensions[2], game.ID))
if err == nil && response != nil {
return
}

response, err = tryDownload(fmt.Sprintf(steamCdnURLFormat + artStyleExtensions[2], game.ID))
response, err = tryDownload(fmt.Sprintf(steamCdnURLFormat+artStyleExtensions[2], game.ID))
if err == nil && response != nil {
return
}
Expand Down Expand Up @@ -400,13 +399,13 @@ func DownloadImage(gridDir string, game *Game, artStyle string, artStyleExtensio
return "", err
}
imageSize := image.Bounds().Max
if (artStyle == "Banner" && imageSize.X < imageSize.Y) {
if artStyle == "Banner" && imageSize.X < imageSize.Y {
return "", nil
} else if (artStyle == "Cover" && imageSize.X > imageSize.Y) {
} else if artStyle == "Cover" && imageSize.X > imageSize.Y {
return "", nil
}

game.ImageSource = from;
game.ImageSource = from

game.CleanImageBytes = imageBytes
return from, nil
Expand All @@ -415,8 +414,8 @@ func DownloadImage(gridDir string, game *Game, artStyle string, artStyleExtensio
// Get game name from SteamDB as last resort.
const steamDBFormat = `https://steamdb.info/app/%v`

func GetGameName(gameId string) string {
response, err := tryDownload(fmt.Sprintf(steamDBFormat, gameId))
func getGameName(gameID string) string {
response, err := tryDownload(fmt.Sprintf(steamDBFormat, gameID))
if err != nil || response == nil {
return ""
}
Expand All @@ -430,7 +429,7 @@ func GetGameName(gameId string) string {
match := pattern.FindStringSubmatch(string(page))
if match == nil || len(match) == 0 {
return ""
} else {
return match[1]
}

return match[1]
}
2 changes: 1 addition & 1 deletion games.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func addNonSteamGames(user User, games map[string]*Game) {
target := gameGroups[2]
uniqueName := bytes.Join([][]byte{target, gameName}, []byte(""))
// Does IEEE CRC32 of target concatenated with gameName. No idea why Steam chose this operation.
gameID := strconv.FormatUint(uint64(crc32.ChecksumIEEE(uniqueName)) | 0x80000000, 10)
gameID := strconv.FormatUint(uint64(crc32.ChecksumIEEE(uniqueName))|0x80000000, 10)
game := Game{gameID, string(gameName), []string{}, "", nil, nil, "", true}
games[gameID] = &game

Expand Down

0 comments on commit cb81b59

Please sign in to comment.