Skip to content

Commit

Permalink
Merge pull request #26 from victorfeijo/master
Browse files Browse the repository at this point in the history
Refactor using golint
  • Loading branch information
boppreh authored Jun 11, 2016
2 parents 9048785 + d97e812 commit b739476
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
7 changes: 3 additions & 4 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"strings"
)

// If a game has a custom image, backs it up by appending "(original)" to the
// BackupGame if a game has a custom image, backs it up by appending "(original)" to the
// file name.
func BackupGame(game *Game) error {
if game.ImagePath != "" && game.ImageBytes != nil {
ext := filepath.Ext(game.ImagePath)
base := filepath.Base(game.ImagePath)
backupPath := filepath.Join(filepath.Dir(game.ImagePath), strings.TrimSuffix(base, ext)+" (original)"+ext)
return ioutil.WriteFile(backupPath, game.ImageBytes, 0666)
} else {
return nil
}
}
return nil
}
13 changes: 6 additions & 7 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func getGoogleImage(gameName string) (string, error) {
matches := pattern.FindStringSubmatch(string(responseBytes))
if len(matches) >= 1 {
return matches[1], nil
} else {
return "", nil
}
return "", nil
}

// Tries to fetch a URL, returning the response only if it was positive.
Expand All @@ -73,23 +72,23 @@ func tryDownload(url string) (*http.Response, error) {
}

// Primary URL for downloading grid images.
const akamaiUrlFormat = `https://steamcdn-a.akamaihd.net/steam/apps/%v/header.jpg`
const akamaiURLFormat = `https://steamcdn-a.akamaihd.net/steam/apps/%v/header.jpg`

// The subreddit mentions this as primary, but I've found Akamai to contain
// more images and answer faster.
const steamCdnUrlFormat = `http://cdn.steampowered.com/v/gfx/apps/%v/header.jpg`
const steamCdnURLFormat = `http://cdn.steampowered.com/v/gfx/apps/%v/header.jpg`

// Tries to load the grid image for a game from a number of alternative
// 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) (response *http.Response, fromSearch bool, err error) {
response, err = tryDownload(fmt.Sprintf(akamaiUrlFormat, game.Id))
response, err = tryDownload(fmt.Sprintf(akamaiURLFormat, game.ID))
if err == nil && response != nil {
return
}

response, err = tryDownload(fmt.Sprintf(steamCdnUrlFormat, game.Id))
response, err = tryDownload(fmt.Sprintf(steamCdnURLFormat, game.ID))
if err == nil && response != nil {
return
}
Expand All @@ -107,7 +106,7 @@ func getImageAlternatives(game *Game) (response *http.Response, fromSearch bool,
return nil, false, nil
}

// Tries to download the game images, saving it in game.ImageBytes. Returns
// 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(game *Game) error {
Expand Down
28 changes: 14 additions & 14 deletions games.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"
)

// A Steam game in a library. May or may not be installed.
// Game in a steam library. May or may not be installed.
type Game struct {
// Official Steam id.
Id string
ID string
// Warning, may contain Unicode characters.
Name string
// Tags, including user-created category and Steam's "Favorite" tag.
Expand Down Expand Up @@ -43,11 +43,11 @@ func addGamesFromProfile(user User, games map[string]*Game) (err error) {
// Fetch game list from public profile.
pattern := regexp.MustCompile(profileGamePattern)
for _, groups := range pattern.FindAllStringSubmatch(profile, -1) {
gameId := groups[1]
gameID := groups[1]
gameName := groups[2]
tags := []string{""}
imagePath := ""
games[gameId] = &Game{gameId, gameName, tags, imagePath, nil, ""}
games[gameID] = &Game{gameID, gameName, tags, imagePath, nil, ""}
}

return
Expand All @@ -72,20 +72,20 @@ func addUnknownGames(user User, games map[string]*Game) {
gamePattern := regexp.MustCompile(`"([0-9]+)"\s*{[^}]+?"tags"\s*{([^}]+?)}`)
tagsPattern := regexp.MustCompile(`"[0-9]+"\s*"(.+?)"`)
for _, gameGroups := range gamePattern.FindAllStringSubmatch(sharedConf, -1) {
gameId := gameGroups[1]
gameID := gameGroups[1]
tagsText := gameGroups[2]

for _, tagGroups := range tagsPattern.FindAllStringSubmatch(tagsText, -1) {
tag := tagGroups[1]

game, ok := games[gameId]
game, ok := games[gameID]
if ok {
game.Tags = append(game.Tags, tag)
} else {
// If for some reason it wasn't included in the profile, create a new
// entry for it now. Unfortunately we don't have a name.
gameName := ""
games[gameId] = &Game{gameId, gameName, []string{tag}, "", nil, ""}
games[gameID] = &Game{gameID, gameName, []string{tag}, "", nil, ""}
}
}
}
Expand Down Expand Up @@ -117,9 +117,9 @@ func addNonSteamGames(user User, games map[string]*Game) {
// Does IEEE CRC32 of target concatenated with gameName, then convert
// to 64bit Steam ID. No idea why Steam chose this operation.
top := uint64(crc32.ChecksumIEEE(uniqueName)) | 0x80000000
gameId := strconv.FormatUint(top<<32|0x02000000, 10)
game := Game{gameId, string(gameName), []string{}, "", nil, ""}
games[gameId] = &game
gameID := strconv.FormatUint(top<<32|0x02000000, 10)
game := Game{gameID, string(gameName), []string{}, "", nil, ""}
games[gameID] = &game

tagsText := gameGroups[3]
for _, tagGroups := range tagsPattern.FindAllSubmatch(tagsText, -1) {
Expand All @@ -129,7 +129,7 @@ func addNonSteamGames(user User, games map[string]*Game) {
}
}

// Returns all games from a given user, using both the public profile and local
// GetGames returns all games from a given user, using both the public profile and local
// files to gather the data. Returns a map of game by ID.
func GetGames(user User) map[string]*Game {
games := make(map[string]*Game, 0)
Expand All @@ -152,10 +152,10 @@ func GetGames(user User) map[string]*Game {
for _, game := range games {
gridDir := filepath.Join(user.Dir, "config", "grid")
for _, suffix := range suffixes {
imagePath := filepath.Join(gridDir, game.Id+suffix)
imagePath := filepath.Join(gridDir, game.ID+suffix)
imageBytes, err := ioutil.ReadFile(imagePath)
if err == nil {
game.ImagePath = filepath.Join(gridDir, game.Id+filepath.Ext(suffix))
game.ImagePath = filepath.Join(gridDir, game.ID+filepath.Ext(suffix))
game.ImageBytes = imageBytes
if strings.HasPrefix(suffix, " (original)") {
game.ImageSource = "backup"
Expand All @@ -166,7 +166,7 @@ func GetGames(user User) map[string]*Game {
}
}
if game.ImageBytes == nil {
game.ImagePath = filepath.Join(gridDir, game.Id+".jpg")
game.ImagePath = filepath.Join(gridDir, game.ID+".jpg")
}
}

Expand Down
4 changes: 2 additions & 2 deletions overlays.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func loadImage(path string) (img image.Image, err error) {
return
}

// Loads the overlays from the given dir, returning a map of name -> image.
// LoadOverlays from the given dir, returning a map of name -> image.
func LoadOverlays(dir string) (overlays map[string]image.Image, err error) {
overlays = make(map[string]image.Image, 0)

Expand Down Expand Up @@ -62,7 +62,7 @@ func LoadOverlays(dir string) (overlays map[string]image.Image, err error) {
return
}

// Applies an overlay to the game image, depending on the category. The
// ApplyOverlay to the game image, depending on the category. The
// resulting image is saved over the original.
func ApplyOverlay(game *Game, overlays map[string]image.Image) (applied bool, err error) {
if game.ImagePath == "" || game.ImageBytes == nil || len(game.Tags) == 0 {
Expand Down
18 changes: 9 additions & 9 deletions steamgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func startApplication() {

nOverlaysApplied := 0
nDownloaded := 0
notFounds := make([]*Game, 0)
searchFounds := make([]*Game, 0)
errors := make([]*Game, 0)
errorMessages := make([]string, 0)
var notFounds []*Game
var searchFounds []*Game
var errors []*Game
var errorMessages []string

for _, user := range users {
fmt.Println("Loading games for " + user.Name)
Expand All @@ -66,13 +66,13 @@ func startApplication() {

i := 0
for _, game := range games {
i += 1
i++

var name string
if game.Name != "" {
name = game.Name
} else {
name = "unknown game with id " + game.Id
name = "unknown game with id " + game.ID
}
fmt.Printf("Processing %v (%v/%v)", name, i, len(games))

Expand Down Expand Up @@ -122,7 +122,7 @@ func startApplication() {
if len(searchFounds) >= 1 {
fmt.Printf("%v images were found with a Google search and may not be accurate:\n", len(searchFounds))
for _, game := range searchFounds {
fmt.Printf("* %v (steam id %v)\n", game.Name, game.Id)
fmt.Printf("* %v (steam id %v)\n", game.Name, game.ID)
}

fmt.Printf("\n\n")
Expand All @@ -131,7 +131,7 @@ func startApplication() {
if len(notFounds) >= 1 {
fmt.Printf("%v images could not be found anywhere:\n", len(notFounds))
for _, game := range notFounds {
fmt.Printf("- %v (id %v)\n", game.Name, game.Id)
fmt.Printf("- %v (id %v)\n", game.Name, game.ID)
}

fmt.Printf("\n\n")
Expand All @@ -140,7 +140,7 @@ func startApplication() {
if len(errors) >= 1 {
fmt.Printf("%v images were found but had errors and could not be overlaid:\n", len(errors))
for i, game := range errors {
fmt.Printf("- %v (id %v) (%v)\n", game.Name, game.Id, errorMessages[i])
fmt.Printf("- %v (id %v) (%v)\n", game.Name, game.ID, errorMessages[i])
}

fmt.Printf("\n\n")
Expand Down
31 changes: 15 additions & 16 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
// User in the local steam installation.
type User struct {
Name string
SteamId32 string
SteamId64 string
SteamID32 string
SteamID64 string
Dir string
}

// Used to convert between SteamId32 and SteamId64.
const idConversionConstant = 0x110000100000000

// Given the Steam installation dir (NOT the library!), returns all users in
// GetUsers given the Steam installation dir (NOT the library!), returns all users in
// this computer.
func GetUsers(installationDir string) ([]User, error) {
userdataDir := filepath.Join(installationDir, "userdata")
Expand All @@ -33,11 +33,11 @@ func GetUsers(installationDir string) ([]User, error) {
return nil, err
}

users := make([]User, 0)
var users []User

for _, userDir := range files {
userId := userDir.Name()
userDir := filepath.Join(userdataDir, userId)
userID := userDir.Name()
userDir := filepath.Join(userdataDir, userID)

configFile := filepath.Join(userDir, "config", "localconfig.vdf")
// Malformed user directory. Without the localconfig file we can't get
Expand Down Expand Up @@ -67,10 +67,10 @@ func GetUsers(installationDir string) ([]User, error) {
pattern := regexp.MustCompile(`"PersonaName"\s*"(.+?)"`)
username := pattern.FindStringSubmatch(string(configBytes))[1]

steamId32, err := strconv.ParseInt(userId, 10, 64)
steamId64 := steamId32 + idConversionConstant
strSteamId64 := strconv.FormatInt(steamId64, 10)
users = append(users, User{username, userId, strSteamId64, userDir})
steamID32, err := strconv.ParseInt(userID, 10, 64)
steamID64 := steamID32 + idConversionConstant
strSteamID64 := strconv.FormatInt(steamID64, 10)
users = append(users, User{username, userID, strSteamID64, userDir})
}

return users, nil
Expand All @@ -85,9 +85,9 @@ const profilePermalinkFormat = `http://steamcommunity.com/profiles/%v/games?tab=
// message.
const steamProfileErrorMessage = `The specified profile could not be found.`

// Returns the HTML profile from a user from their SteamId32.
// GetProfile returns the HTML profile from a user from their SteamId32.
func GetProfile(user User) (string, error) {
response, err := http.Get(fmt.Sprintf(profilePermalinkFormat, user.SteamId64))
response, err := http.Get(fmt.Sprintf(profilePermalinkFormat, user.SteamID64))
if err != nil {
return "", err
}
Expand All @@ -110,7 +110,7 @@ func GetProfile(user User) (string, error) {
return profile, nil
}

// Returns the Steam installation directory in Windows. Should work for
// GetSteamInstallation Returns the Steam installation directory in Windows. Should work for
// internationalized systems, 32 and 64 bits and users that moved their
// ProgramFiles folder. If a folder is given by program parameter, uses that.
func GetSteamInstallation() (path string, err error) {
Expand All @@ -119,9 +119,8 @@ func GetSteamInstallation() (path string, err error) {
_, err := os.Stat(argDir)
if err == nil {
return argDir, nil
} else {
return "", errors.New("Argument must be a valid Steam directory, or empty for auto detection. Got: " + argDir)
}
}
return "", errors.New("Argument must be a valid Steam directory, or empty for auto detection. Got: " + argDir)
}

currentUser, err := user.Current()
Expand Down

0 comments on commit b739476

Please sign in to comment.