Skip to content

Commit

Permalink
Implement new command (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
habedi authored Jan 22, 2025
1 parent dfba14d commit be236f1
Show file tree
Hide file tree
Showing 15 changed files with 400 additions and 39 deletions.
3 changes: 3 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code of Conduct

We adhere to the [Go Community Code of Conduct](https://go.dev/conduct).
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ Contributions are always welcome and appreciated.
## Miscellaneous

- Run `make help` to see all available commands to manage different tasks.

## Code of Conduct

We adhere to the [Go Community Code of Conduct](https://go.dev/conduct).
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
<a href="https://goreportcard.com/report/github.com/habedi/gogg">
<img src="https://goreportcard.com/badge/github.com/habedi/gogg" alt="Go Report Card">
</a>
<a href="https://pkg.go.dev/github.com/habedi/gogg">
<img src="https://pkg.go.dev/badge/github.com/habedi/gogg.svg" alt="Go Reference">
</a>
<a href="https://codecov.io/gh/habedi/gogg">
<img src="https://codecov.io/gh/habedi/gogg/graph/badge.svg?token=1RUL13T0VE" alt="Code Coverage">
</a>
Expand Down Expand Up @@ -67,6 +64,7 @@ Additionally, it allows users to perform the following actions:
- Download files using multiple threads to speed up the process
- Resume interrupted downloads and only download missing or newer files
- Verify the integrity of downloaded files by calculating their hashes
- Calculate the total size of the files to be downloaded (for storage planning)

## Getting Started

Expand All @@ -76,7 +74,7 @@ Run `gogg -h` to see the available commands and options.

### Examples

For more detailed examples, see the content of the [examples](docs/examples/) directory.
**For more detailed examples, see the content of the [examples](docs/examples/) directory.**

#### Login to GOG

Expand Down Expand Up @@ -119,6 +117,14 @@ gogg download 1207658924 ./games --platform=windows --lang=en --dlcs=true --extr
gogg file hash ./games/the-witcher-enhanced-edition --algo=sha1
```

#### Storage Size Calculation

```bash
# Will show the total size of the files to be downloaded for `The Witcher: Enhanced Edition`
DEBUG_GOGG=false gogg file size 1207658924 --platform=windows --lang=en --dlcs=true \
--extras=false --unit=GB
```

## Demo

[![asciicast](https://asciinema.org/a/kXMGRUUV149R37IEmZKtTH7nI.svg)](https://asciinema.org/a/kXMGRUUV149R37IEmZKtTH7nI)
Expand Down
10 changes: 4 additions & 6 deletions client/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
Expand Down Expand Up @@ -71,11 +70,10 @@ func Login(loginURL string, username string, password string, headless bool) err
}

// Print the access token, refresh token, and expiration if debug mode is enabled
if os.Getenv("DEBUG_GOGG") != "" {
log.Info().Msgf("Access token: %s", token[:10])
log.Info().Msgf("Refresh token: %s", refreshToken[:10])
log.Info().Msgf("Expires at: %s", expiresAt)
}

log.Info().Msgf("Access token: %s", token[:10])
log.Info().Msgf("Refresh token: %s", refreshToken[:10])
log.Info().Msgf("Expires at: %s", expiresAt)

// Save the token record in the database
return db.UpsertTokenRecord(&db.Token{AccessToken: token, RefreshToken: refreshToken, ExpiresAt: expiresAt})
Expand Down
34 changes: 16 additions & 18 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,9 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
)

// Map of supported game languages and their native names
var gameLanguages = map[string]string{
"en": "English",
"fr": "Français", // French
"de": "Deutsch", // German
"es": "Español", // Spanish
"it": "Italiano", // Italian
"ru": "Русский", // Russian
"pl": "Polski", // Polish
"pt-BR": "Português do Brasil", // Portuguese (Brazil)
"zh-Hans": "简体中文", // Simplified Chinese
"ja": "日本語", // Japanese
"ko": "한국어", // Korean
}

// downloadCmd creates a new cobra.Command for downloading a selected game from GOG.
// It returns a pointer to the created cobra.Command.
func downloadCmd() *cobra.Command {
Expand All @@ -49,7 +35,8 @@ func downloadCmd() *cobra.Command {
return
}
downloadDir := args[1]
executeDownload(gameID, downloadDir, language, platformName, extrasFlag, dlcFlag, resumeFlag, flattenFlag, numThreads)
executeDownload(gameID, downloadDir, strings.ToLower(language), platformName, extrasFlag, dlcFlag,
resumeFlag, flattenFlag, numThreads)
},
}

Expand Down Expand Up @@ -78,6 +65,17 @@ func executeDownload(gameID int, downloadPath, language, platformName string, ex
return
}

// Check if the language is valid
if !isValidLanguage(language) {
fmt.Println("Invalid language code. Supported languages are:")
for langCode, langName := range gameLanguages {
fmt.Printf("'%s' for %s\n", langCode, langName)
}
return
} else {
language = gameLanguages[language]
}

// Try to refresh the access token
if _, err := client.RefreshToken(); err != nil {
fmt.Println("Failed to find or refresh the access token. Did you login?")
Expand Down Expand Up @@ -122,7 +120,7 @@ func executeDownload(gameID int, downloadPath, language, platformName string, ex
flattenFlag, numThreads)

// Download the game files
err = client.DownloadGameFiles(user.AccessToken, parsedGameData, downloadPath, gameLanguages[language],
err = client.DownloadGameFiles(user.AccessToken, parsedGameData, downloadPath, language,
platformName, extrasFlag, dlcFlag, resumeFlag, flattenFlag, numThreads)
if err != nil {
log.Error().Err(err).Msg("Failed to download game files.")
Expand All @@ -137,7 +135,7 @@ func logDownloadParameters(game client.Game, gameID int, downloadPath, language,
extrasFlag, dlcFlag, resumeFlag bool, flattenFlag bool, numThreads int) {
fmt.Println("================================= Download Parameters =====================================")
fmt.Printf("Downloading \"%v\" (with game ID=\"%d\") to \"%v\"\n", game.Title, gameID, downloadPath)
fmt.Printf("Platform: \"%v\", Language: '%v'\n", platformName, gameLanguages[language])
fmt.Printf("Platform: \"%v\", Language: '%v'\n", platformName, language)
fmt.Printf("Include Extras: \"%v, Include DLCs: \"%v\", Resume enabled: \"%v\"\n", extrasFlag, dlcFlag, resumeFlag)
fmt.Printf("Number of worker threads for download: \"%d\"\n", numThreads)
fmt.Printf("Flatten directory structure: \"%v\"\n", flattenFlag)
Expand Down
Loading

0 comments on commit be236f1

Please sign in to comment.