-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
457 lines (411 loc) · 12.1 KB
/
app.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/net/html"
"io"
"net/http"
"net/url"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
// App struct
type App struct {
ctx context.Context
settings AppSettings
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
// Perform your setup here
a.ctx = ctx
a.settings, _ = LoadSettings()
}
// domReady is called after front-end resources have been loaded
func (a App) domReady(ctx context.Context) {
// Add your action here
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
type Cache struct {
steamIDCache map[string]string
gameCache map[string]string
}
func NewCache() *Cache {
return &Cache{
steamIDCache: make(map[string]string),
gameCache: make(map[string]string),
}
}
func (c *Cache) lookupSteamID(url string) string {
return c.steamIDCache[url]
}
func (c *Cache) addSteamID(url string, image string, ttl int) {
c.steamIDCache[url] = image
go func() {
<-time.After(time.Duration(ttl) * time.Minute)
delete(c.steamIDCache, url)
}()
}
func (c *Cache) lookupGame(url string) string {
return c.gameCache[url]
}
func (c *Cache) addGame(url string, name string, ttl int) {
c.gameCache[url] = name
go func() {
<-time.After(time.Duration(ttl) * time.Minute)
delete(c.gameCache, url)
}()
}
func getImage(profileURL string, cache *Cache) (string, error) {
cachedResult := cache.lookupSteamID(profileURL)
if cachedResult != "" && cachedResult != "NO_BACKGROUND" {
return cachedResult, nil
}
client := &http.Client{}
req, err := http.NewRequest("GET", profileURL, nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %v", err)
}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
responseBody := string(body)
doc, err := html.Parse(strings.NewReader(responseBody))
if err != nil {
panic(err)
}
var divsWithBackgroundImage []*html.Node
var findDivsWithBackgroundImage func(*html.Node)
findDivsWithBackgroundImage = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "div" {
for _, a := range n.Attr {
if a.Key == "class" && strings.Contains(a.Val, "profile_page") && strings.Contains(a.Val, "has_profile_background") {
divsWithBackgroundImage = append(divsWithBackgroundImage, n)
break
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findDivsWithBackgroundImage(c)
}
}
findDivsWithBackgroundImage(doc)
for _, divWithBackgroundImage := range divsWithBackgroundImage {
var backgroundImageURL string
for _, a := range divWithBackgroundImage.Attr {
if a.Key == "style" {
style := strings.TrimSpace(a.Val)
re := regexp.MustCompile(`background-image:\s*url\(\s*'?(.*?)'\s*?\);`)
matches := re.FindStringSubmatch(style)
if len(matches) < 2 {
backgroundImageURL = ""
} else {
backgroundImageURL = strings.TrimSpace(matches[1])
}
}
}
if backgroundImageURL != "" {
cache.addSteamID(profileURL, backgroundImageURL, 7)
return backgroundImageURL, nil
} else {
videoSourceURL := ""
for profileAnimatedBackground := divWithBackgroundImage.FirstChild; profileAnimatedBackground != nil; profileAnimatedBackground = profileAnimatedBackground.NextSibling {
if profileAnimatedBackground.Type == html.ElementNode && profileAnimatedBackground.Data == "div" {
for _, a := range profileAnimatedBackground.Attr {
if a.Key == "class" && strings.Contains(a.Val, "profile_animated_background") {
for videoNode := profileAnimatedBackground.FirstChild; videoNode != nil; videoNode = videoNode.NextSibling {
if videoNode != nil && videoNode.Type == html.ElementNode && videoNode.Data == "video" {
for c := videoNode.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode && c.Data == "source" {
for _, sourceAttr := range c.Attr {
if sourceAttr.Key == "type" && sourceAttr.Val == "video/mp4" {
videoSourceURL = c.Attr[0].Val
break
}
}
}
}
}
}
}
}
if videoSourceURL != "" {
cache.addSteamID(profileURL, videoSourceURL, 7)
return videoSourceURL, nil
}
}
}
}
}
cache.addSteamID(profileURL, "NO_BACKGROUND", 1)
return "Profile has no background", nil
}
func (a *App) GetAppId(url string) string {
return getAppIDGolang(url)
}
func (a *App) GetGameName(url string, cache *Cache) (string, error) {
return getGameName(url, cache)
}
func (a *App) GetBackground(url string, cache *Cache) (string, error) {
return getImage(url, cache)
}
func (a *App) GetPageBodyViaGolang(url string) (string, error) {
return getPageBodyViaGolang(url)
}
func (a *App) GetSteam32IDViaGolang(username string, key string) (string, error) {
if key != "" {
steam64ID, err := getSteam64IDViaAPI(username, key)
if err != nil {
if strings.Contains(err.Error(), "API key not authorized to access this resource") {
return "", fmt.Errorf("API key not authorized to access Steam API")
}
return getSteam32IDViaScrapper(username)
}
steam32ID, err := steam64ToSteam32(steam64ID)
if err != nil {
return getSteam32IDViaScrapper(username)
}
return steam32ID, nil
}
return getSteam32IDViaScrapper(username)
}
func (a *App) GetEquippedItemsViaGolang(steam64ID string, language string) ([]EquippedItem, error) {
return GetEquippedItems(steam64ID, language)
}
func (a *App) OpenCustomURLViaGolang(url string, fallbackUrl string, appName string) error {
return openCustomUrl(url, fallbackUrl, appName)
}
func (a *App) AddMarketURIToEquippedItemsViaGolang(items []EquippedItem, currency int) []EquippedItem {
return addMarketURIToEquippedItems(items, currency)
}
func (a *App) PutMarketPriceToEquippedItemViaGolang(item EquippedItem, currency int) EquippedItem {
return putMarketPriceToEquippedItem(item, currency)
}
func openCustomUrl(url string, fallbackUrl string, appName string) error {
var err error
if len(fallbackUrl) == 0 {
err = openUrl(url)
return err
}
if isAppRunning(appName) {
err = openUrl(url)
} else {
err = openUrl(fallbackUrl)
}
return err
}
func isAppRunning(appName string) bool {
var cmd *exec.Cmd
switch runtime.GOOS {
case "linux", "darwin":
cmd = exec.Command("pgrep", appName)
case "windows":
cmd = exec.Command("tasklist", "/FI", "IMAGENAME eq "+appName+".exe")
default:
return false
}
output, err := cmd.CombinedOutput()
if err != nil {
return false
}
if runtime.GOOS == "windows" {
return strings.Contains(string(output), appName)
} else {
pid, err := strconv.Atoi(strings.TrimSpace(string(output)))
if err != nil {
return false
}
return pid > 0
}
}
func openUrl(url string) error {
var err error
switch runtime.GOOS {
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "linux":
err = exec.Command("xdg-open", url).Start()
default:
err = exec.Command("open", url).Start()
}
return err
}
func getSteam64IDViaAPI(username string, key string) (string, error) {
requestURL := fmt.Sprintf("http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=%s&vanityurl=%s", key, username)
resp, err := http.Get(requestURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == 403 {
return "", fmt.Errorf("API key not authorized to access this resource")
}
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return "", err
}
response, ok := data["response"].(map[string]interface{})
if !ok {
str, _ := json.Marshal(data)
return "", fmt.Errorf("response not found in data " + string(str))
}
steamid, ok := response["steamid"].(string)
if !ok {
str, _ := json.Marshal(data)
return "", fmt.Errorf("SteamID not found in response " + string(str))
}
return steamid, nil
}
func steam64ToSteam32(steam64 string) (string, error) {
const steam64Base = 76561197960265728
id, err := strconv.ParseUint(steam64, 10, 64)
if err != nil {
return "", err
}
steam32 := strconv.FormatUint(id-steam64Base, 10)
return steam32, nil
}
func getSteam32IDViaScrapper(username string) (string, error) {
var profileURL string
if match, _ := regexp.MatchString(`^7\d{15}([02468])$`, username); match {
profileURL = "https://steamcommunity.com/profiles/" + username
} else {
profileURL = "https://steamcommunity.com/id/" + username
}
htmlString, err := getPageBodyViaGolang("https://steamid.xyz/" + profileURL)
if err != nil {
return "", fmt.Errorf("failed to get page body: %v", err)
}
doc, err := html.Parse(strings.NewReader(htmlString))
if err != nil {
return "", fmt.Errorf("failed to parse html: %v", err)
}
steam32Regex := regexp.MustCompile(`^STEAM_0:(\d+):(\d+)$`)
var steam32Input *html.Node
var findSteam32Input func(*html.Node)
findSteam32Input = func(n *html.Node) {
if steam32Input != nil {
return
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, attr := range n.Attr {
if attr.Key == "value" && strings.HasPrefix(attr.Val, "STEAM_") {
steam32Input = n
return
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findSteam32Input(c)
}
}
findSteam32Input(doc)
if err != nil || steam32Input == nil {
return "", fmt.Errorf("input not found: %v", err)
}
steam32Value := ""
for _, attr := range steam32Input.Attr {
if attr.Key == "value" {
steam32Value = attr.Val
break
}
}
matches := steam32Regex.FindStringSubmatch(steam32Value)
if err != nil {
return "", fmt.Errorf("value not found: %v", err)
}
a, _ := strconv.Atoi(matches[1])
b, _ := strconv.Atoi(matches[2])
steam32ID := strconv.Itoa(b*2 + a)
return steam32ID, nil
}
func getPageBodyViaGolang(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
return string(body), nil
}
func getAppIDGolang(url string) string {
pattern := regexp.MustCompile(`(\d+)(?:/|\.[\da-z]+$)`)
matcher := pattern.FindStringSubmatch(url)
if len(matcher) < 1 {
return ""
}
return matcher[1]
}
func getGameName(imageURL string, cache *Cache) (string, error) {
cachedResult := cache.lookupGame(imageURL)
if cachedResult != "" && cachedResult != "NOT_EXIST" {
return cachedResult, nil
}
appId := getAppIDGolang(imageURL)
if appId == "" {
return "", nil
}
resp, err := http.Get(fmt.Sprintf("https://store.steampowered.com/api/appdetails?appids=%s", appId))
if err != nil {
return "", fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
responseBody := string(body)
if strings.Contains(responseBody, `"success":true`) {
pattern := regexp.MustCompile(`name":"(.*?)"`)
nameMatcher := pattern.FindStringSubmatch(responseBody)
if len(nameMatcher) > 1 {
name := nameMatcher[1]
cache.addGame(imageURL, name, 32)
return name, nil
} else {
cache.addGame(imageURL, "NOT_EXIST", 7)
return "", nil
}
} else {
cache.addGame(imageURL, "NOT_EXIST", 7)
return "", nil
}
}
func (a *App) invalidURL(profileURL string) bool {
u, err := url.Parse(profileURL)
if err != nil {
return true
}
return u.Host != "steamcommunity.com"
}