-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (77 loc) · 2.12 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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"github.com/gocolly/colly/v2"
)
const (
// Separators
steam = " (Steam)"
epic = " (Epic Games Store)"
// Methods
scrap = "scrap"
list = "list"
)
func main() {
search := flag.String("search", "", "Search if given game is available.")
method := flag.String("method", scrap, fmt.Sprintf("Search for game with [%s] or [%s] method.", scrap, list))
flag.Parse()
if *search == "" {
flag.PrintDefaults()
exit(fmt.Errorf("\nmissing required -search flag"), 2)
}
var f func(query string) error
switch *method {
case scrap:
f = scrapAndSearch
case list:
f = listAndSearch
default:
flag.PrintDefaults()
exit(fmt.Errorf("\nplease specify a valid search method [%s] or [%s]", scrap, list), 2)
}
exit(f(*search), 1)
}
func listAndSearch(query string) error {
games, err := ListGames(context.Background())
if err != nil {
return err
}
for _, game := range games {
if strings.Contains(strings.ToLower(game.Title), strings.ToLower(query)) {
fmt.Printf("🎮 \t%s: Optimized? %t\tStore: %s\tStatus: %s\t%s\n", game.Title, game.IsFullyOptimized, game.Store, game.Status, game.SteamURL)
}
}
return nil
}
func scrapAndSearch(query string) error {
c := colly.NewCollector(colly.AllowedDomains("www.nvidia.com"))
var htmlNewGamesListRaw string
c.OnHTML(`div.text-center.tab-text-center div.body-text.description.color-body-copy p`, func(element *colly.HTMLElement) {
// We have some paragraphs, have to parse them with CSS
if element.Attr("style") == "text-align: center;" && htmlNewGamesListRaw == "" {
htmlNewGamesListRaw = element.Text
}
})
if err := c.Visit("https://www.nvidia.com/en-eu/geforce-now/games/"); err != nil {
return err
}
s := strings.ReplaceAll(htmlNewGamesListRaw, steam, "<CUT_HERE>")
s = strings.ReplaceAll(s, epic, "<CUT_HERE>")
for _, game := range strings.Split(s, "<CUT_HERE>") {
if strings.Contains(strings.ToLower(game), strings.ToLower(query)) {
fmt.Printf("🎮 \t%s\n", game)
}
}
return nil
}
func exit(err error, code int) {
if err == nil {
return
}
_, _ = fmt.Fprint(os.Stderr, err)
os.Exit(code)
}