@@ -9,50 +9,54 @@ import (
9
9
"path/filepath"
10
10
"strings"
11
11
12
- ps "github.com/lucasepe/go-ps4"
13
- "github.com/lucasepe/go-ps4/utils"
12
+ ps "github.com/lucasepe/playstation"
13
+ "github.com/lucasepe/playstation/utils"
14
+ browser "github.com/pkg/browser"
14
15
)
15
16
16
17
var (
17
18
version string
18
19
)
19
20
20
21
/**
21
- * NIX: CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static" -X main.version=0.5.0 '
22
+ * NIX: CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static" -X main.version=0.5.1 '
22
23
*
23
24
* WIN (powershell):
24
25
* $env:CGO_ENABLED="0"
25
- * go build -ldflags "-X main.version=0.5.0 "
26
+ * go build -ldflags "-X main.version=0.5.1 "
26
27
*/
27
- func main () {
28
28
29
+ func main () {
29
30
flag .Usage = func () {
30
31
fmt .Printf ("PS Store CLI (v.%s)\n " , version )
31
32
fmt .Printf ("Usage: %s [OPTIONS] [Game Title]\n " , filepath .Base (os .Args [0 ]))
32
33
flag .PrintDefaults ()
33
34
}
34
35
35
- optLang := flag .String ("lang " , "it" , "language (it, en)" )
36
+ optRegion := flag .String ("region " , "it" , "region (it, en, at, de )" ) // should probably renamed to optCountry
36
37
optAddons := flag .Bool ("addons" , false , "show also extra contents" )
37
38
optFree := flag .Bool ("free" , false , "show only free titles" )
38
39
optWeeklyDeals := flag .Bool ("weekly-deals" , false , "show only weekly deals titles" )
39
40
41
+ urls := []string {}
42
+
40
43
flag .Parse ()
41
44
42
45
searchFor := len (flag .Args ()) > 0 && strings .TrimSpace (flag .Args ()[0 ]) != ""
43
46
44
47
var uriPath string
45
48
if * optAddons {
46
- uriPath = ps .AddonsUrls [* optLang ]
49
+ uriPath = ps .AddonsUrls [* optRegion ]
47
50
} else if searchFor {
48
- uriPath = ps .SearchUrls [* optLang ]
51
+ uriPath = ps .SearchUrls [* optRegion ]
49
52
} else if * optWeeklyDeals {
50
- uriPath = ps .WeeklyDealsUrls [* optLang ]
53
+ uriPath = ps .WeeklyDealsUrls [* optRegion ]
51
54
} else {
52
- uriPath = ps .AllGamesUrls [* optLang ]
55
+ uriPath = ps .AllGamesUrls [* optRegion ]
53
56
}
54
57
55
- u , err := url .Parse (fmt .Sprintf ("https://store.playstation.com/%s" , uriPath ))
58
+ u , err := url .Parse (fmt .Sprintf ("https://store.playstation.com/%s" ,
59
+ uriPath ))
56
60
if err != nil {
57
61
log .Fatal (err )
58
62
}
@@ -77,19 +81,38 @@ func main() {
77
81
if ! * optWeeklyDeals {
78
82
if * optFree {
79
83
q .Add ("price" , "0-0" )
80
- } else if * optLang == "it" {
84
+ } else if * optRegion == "it" {
81
85
q .Add ("price" , "1000-1999,2000-2999,3000-3999" )
82
86
}
83
87
}
84
88
85
89
u .RawQuery = q .Encode ()
86
90
}
87
91
88
- doSearch (u )
92
+ urls = doSearch (u )
93
+ prompt (urls )
89
94
}
90
95
91
- func doSearch (u * url.URL ) {
96
+ func doSearch (u * url.URL ) []string {
97
+ urls := []string {}
98
+ idx := 0
92
99
for g := range ps .Visit (u .String ()) {
93
- fmt .Printf ("%s %s\n " , utils .RightPad (g .Title , 68 , "." ), utils .RightPad (g .Price , 7 , " " ))
100
+ fmt .Printf ("%s %s %s\n " , padNumberWithSpaces (idx ), utils .RightPad (g .Title , 66 , "." ), utils .RightPad (g .Price , 7 , " " ))
101
+ urls = append (urls , g .Url )
102
+ idx ++
94
103
}
104
+ return urls
105
+ }
106
+
107
+ // https://stackoverflow.com/a/48089636/1548552
108
+ func padNumberWithSpaces (value int ) string {
109
+ return fmt .Sprintf ("%3v" , value )
110
+ }
111
+
112
+ func prompt (urls []string ) {
113
+ var idx int
114
+ fmt .Println ("" )
115
+ fmt .Println ("Which game should be opened in the browser? Enter number" )
116
+ fmt .Scan (& idx )
117
+ browser .OpenURL ("https://store.playstation.com" + urls [idx ])
95
118
}
0 commit comments