This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (144 loc) · 3.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
)
const metadataURL = "https://www.tibia.com/launcher/tibiametadata.json"
var wg *sync.WaitGroup
func main() {
metadataBytes, err := downloadJSON(metadataURL, "")
if err != nil {
log.Fatalln(err)
}
var metadata TMetadata
err = json.Unmarshal(metadataBytes, &metadata)
if err != nil {
log.Fatalln(err)
}
for _, build := range metadata.Launcher.Builds {
downloadBuild(build)
}
for _, client := range metadata.Packages {
for _, build := range client.Builds {
downloadBuild(build)
}
}
// wg.Wait()
}
func toPath(URL string) string {
indexPath := strings.Index(URL, "launcher")
if indexPath == -1 {
indexPath = 0
}
return URL[indexPath:]
}
func downloadJSON(URL, destination string) ([]byte, error) {
rawBytes, err := getJSON(URL)
if err != nil {
return nil, err
}
var jsonAbs string
if destination == "" {
jsonFile := toPath(URL)
jsonAbs, _ = filepath.Abs(jsonFile)
} else {
jsonAbs, _ = filepath.Abs(filepath.Join(destination, toPath(URL)))
}
// create dir if not exists
if _, err := os.Stat(jsonAbs); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(filepath.Dir(jsonAbs), 0755)
if err != nil {
return nil, err
}
}
}
rawBytes = bytes.Trim(rawBytes, "\x00")
ioutil.WriteFile(jsonAbs, rawBytes, 0644)
return rawBytes, nil
}
func downloadFile(URL, distributionFolder string) {
URL = strings.TrimSpace(strings.Replace(URL, "https:/static", "https://static", 1))
downloadJSON(URL, distributionFolder)
}
func downloadBuild(build Builds) {
distributionFolder := fmt.Sprintf("%s-%s-current", build.OS, build.Architecture)
if build.URL != "" {
downloadURL(build.URL, distributionFolder)
}
if build.AssetsURL != "" {
downloadURL(build.AssetsURL, distributionFolder)
}
}
func downloadURL(URL, distributionFolder string) {
rawBytes, err := downloadJSON(URL, distributionFolder)
if err != nil {
log.Fatalln(err)
}
var files TStructure
err = json.Unmarshal(rawBytes, &files)
if err != nil {
log.Fatalln(err)
}
for _, file := range files.Files {
fileURL := filepath.Join(strings.TrimRight(URL, "/package.json"), file.URL)
downloadFile(fileURL, distributionFolder)
}
}
func getJSON(url string) ([]byte, error) {
log.Printf("downloading %s\n", url)
client := http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
request.Header.Set("User-Agent", "Mozilla/5.0")
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return buf.Bytes(), nil
}
type TMetadata struct {
Launcher Launcher `json:"launcher"`
Packages []Packages `json:"packages"`
HardwareSurvey string `json:"hardwaresurvey"`
Hints string `json:"hints"`
}
type Launcher struct {
Name string `json:"name"`
Builds []Builds `json:"builds"`
}
type Builds struct {
OS string `json:"os"`
Architecture string `json:"architecture"`
URL string `json:"url"`
AssetsURL string `json:"assetsurl"`
}
type Packages struct {
Name string `json:"name"`
Builds []Builds `json:"builds"`
}
type TStructure struct {
Files []File `json:"files"`
}
type File struct {
URL string `json:"url"`
UnpackedHash string `json:"unpackedhash"`
UnpackedSize int `json:"unpackedsize"`
PackedHash string `json:"packedhash"`
PackedSize int `json:"packedsize"`
LocalFile string `json:"localfile"`
Executable bool `json:"executable,omitempty"`
}