Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.

Commit c58a495

Browse files
committed
refactor and show filesize
1 parent 4583f35 commit c58a495

File tree

2 files changed

+89
-57
lines changed

2 files changed

+89
-57
lines changed

aria2.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dir=genshin-patch
22
check-integrity=true
33
console-log-level=warn
44
summary-interval=0
5+
referer=https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?channel_id=1&key=gcStgarh&launcher_id=10&sub_channel_id=0

main.go

Lines changed: 88 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,134 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
7-
"log"
86
"net/http"
97
"os"
108
"os/exec"
9+
"strconv"
1110
)
1211

13-
type GameData struct {
12+
const (
13+
KB = 1024
14+
MB = 1024 * KB
15+
GB = 1024 * MB
16+
17+
URL = "https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?channel_id=1&key=gcStgarh&launcher_id=10&sub_channel_id=0"
18+
)
19+
20+
type GenshinData struct {
1421
Data struct {
1522
Game struct {
1623
Latest struct {
1724
Version string `json:"version"`
18-
} `json:"latest"`
25+
}
1926
Diffs []struct {
20-
Version string `json:"version"`
21-
Path string `json:"path"`
22-
MD5 string `json:"md5"`
23-
VoicePacks []VoicePack `json:"voice_packs"`
27+
Name string `json:"name"`
28+
Version string `json:"version"`
29+
Path string `json:"path"`
30+
Size string `json:"package_size"`
31+
Hash string `json:"md5"`
32+
Voice []struct {
33+
Language string `json:"language"`
34+
Name string `json:"name"`
35+
Path string `json:"path"`
36+
Size string `json:"package_size"`
37+
Hash string `json:"md5"`
38+
} `json:"voice_packs"`
2439
} `json:"diffs"`
2540
} `json:"game"`
2641
} `json:"data"`
2742
}
2843

29-
type VoicePack struct {
30-
Path string `json:"path"`
31-
MD5 string `json:"md5"`
32-
}
33-
3444
func main() {
35-
url := "https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?channel_id=1&key=gcStgarh&launcher_id=10&sub_channel_id=0"
45+
// create http client with 10s timeout
46+
client := &http.Client{
47+
Timeout: 10e9,
48+
}
3649

37-
// Fetch the JSON data
38-
response, err := http.Get(url)
50+
// create http request
51+
req, err := http.NewRequest("GET", URL, http.NoBody)
3952
if err != nil {
40-
log.Fatal(err)
53+
fmt.Println(err)
54+
return
4155
}
42-
defer response.Body.Close()
4356

44-
body, err := ioutil.ReadAll(response.Body)
57+
// send http request
58+
resp, err := client.Do(req)
4559
if err != nil {
46-
log.Fatal(err)
60+
fmt.Println(err)
61+
return
4762
}
63+
defer resp.Body.Close()
4864

49-
// Parse the JSON data
50-
var data GameData
51-
err = json.Unmarshal(body, &data)
65+
// parse http response
66+
var data GenshinData
67+
err = json.NewDecoder(resp.Body).Decode(&data)
5268
if err != nil {
53-
log.Fatal(err)
69+
fmt.Println(err)
70+
return
5471
}
5572

56-
// Extract the required values
73+
// extract the required values
5774
oldVer := data.Data.Game.Diffs[0].Version
5875
newVer := data.Data.Game.Latest.Version
76+
gameData := data.Data.Game.Diffs[0]
5977

60-
fmt.Printf("Genshin Impact Patch Downloader\nupdate version : %s to %s\n\n", oldVer, newVer)
61-
fmt.Println("1. Game Update")
62-
fmt.Println("2. Chinese Voice")
63-
fmt.Println("3. English Voice")
64-
fmt.Println("4. Japanese Voice")
65-
fmt.Println("5. Korean Voice")
78+
// print available updates
79+
fmt.Printf("Genshin Impact Patch Downloader\nUpdate Version : %s to %s\n\n", oldVer, newVer)
80+
fmt.Printf("1. game update (%s)\n", convertSize(gameData.Size))
81+
for i, voiceData := range gameData.Voice {
82+
fmt.Printf("%d. %s voice (%s)\n", i+2, voiceData.Language, convertSize(voiceData.Size))
83+
}
84+
85+
var name, path, hash string
6686

67-
// Prompt user to select the option
68-
var option int
69-
validOption := false
70-
for !validOption {
71-
fmt.Print("select an option (1-5): ")
72-
_, err = fmt.Scanln(&option)
73-
if err != nil || option < 1 || option > 5 {
74-
fmt.Println("invalid option selected")
87+
// ask user to select update
88+
for {
89+
var choice int
90+
fmt.Print("\nSelect Update : ")
91+
fmt.Scanln(&choice)
92+
if choice == 1 {
93+
name = gameData.Name
94+
path = gameData.Path
95+
hash = gameData.Hash
96+
} else if choice > 1 && choice < len(gameData.Voice)+2 {
97+
name = gameData.Voice[choice-2].Name
98+
path = gameData.Voice[choice-2].Path
99+
hash = gameData.Voice[choice-2].Hash
100+
} else {
101+
fmt.Println("Wrong Choice")
75102
continue
76103
}
77-
78-
validOption = true
79-
}
80-
81-
// Download the selected file
82-
path := ""
83-
md5 := ""
84-
if option == 1 {
85-
path = data.Data.Game.Diffs[0].Path
86-
md5 = data.Data.Game.Diffs[0].MD5
87-
} else if option >= 2 && option <= 5 {
88-
index := option - 2
89-
path = data.Data.Game.Diffs[0].VoicePacks[index].Path
90-
md5 = data.Data.Game.Diffs[0].VoicePacks[index].MD5
104+
break
91105
}
92106

93-
fmt.Println("\ndownloading...")
94-
downloadCommand := fmt.Sprintf("aria2c.exe --conf-path=aria2.conf --checksum=md5=%s %s", md5, path)
95-
cmd := exec.Command("cmd", "/C", downloadCommand)
107+
// download update file
108+
fmt.Println("Downloading:", name)
109+
command := fmt.Sprintf("aria2c.exe --conf-path=aria2.conf --checksum=md5=%s %s", hash, path)
110+
cmd := exec.Command("cmd", "/C", command)
96111
cmd.Stdout = os.Stdout
97112
cmd.Stderr = os.Stderr
98113
err = cmd.Run()
99114
if err != nil {
100-
log.Fatal(err)
115+
fmt.Println(err)
116+
return
101117
}
102118

103119
fmt.Print("\npress enter to exit...")
104120
fmt.Scanln()
105121
}
122+
123+
// convert size from string bytes to human readable format
124+
func convertSize(s string) string {
125+
size, _ := strconv.ParseInt(s, 10, 64)
126+
switch {
127+
case size > GB:
128+
return fmt.Sprintf("%.2f GB", float64(size)/GB)
129+
case size > MB:
130+
return fmt.Sprintf("%d MB", size/MB)
131+
case size > KB:
132+
return fmt.Sprintf("%d KB", size/KB)
133+
default:
134+
return fmt.Sprintf("%d B", size)
135+
}
136+
}

0 commit comments

Comments
 (0)