Skip to content

Commit 3199856

Browse files
committed
renamed lang to region,
added AT and DE region, added prompt to open specific game in browser
1 parent 58548ba commit 3199856

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ This tool use [Colly](https://github.com/gocolly/colly) the Lightning Fast and E
1818

1919
```shell
2020
$ ps4 -h
21-
PS Store CLI (v.0.5.0)
21+
PS Store CLI (v.0.5.1)
2222
Usage: go-ps4 [OPTIONS] [Game Title]
2323
-addons
2424
show also extra contents
2525
-free
2626
show only free titles
27-
-lang string
28-
language (it, en) (default "it")
27+
-region string
28+
store region (it, en, at, de) (default "it" - used also for language)
2929
-weekly-deals
3030
show only weekly deals titles
3131
```

cmd/ps4/main.go

+38-15
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,54 @@ import (
99
"path/filepath"
1010
"strings"
1111

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"
1415
)
1516

1617
var (
1718
version string
1819
)
1920

2021
/**
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'
2223
*
2324
* WIN (powershell):
2425
* $env:CGO_ENABLED="0"
25-
* go build -ldflags "-X main.version=0.5.0"
26+
* go build -ldflags "-X main.version=0.5.1"
2627
*/
27-
func main() {
2828

29+
func main() {
2930
flag.Usage = func() {
3031
fmt.Printf("PS Store CLI (v.%s)\n", version)
3132
fmt.Printf("Usage: %s [OPTIONS] [Game Title]\n", filepath.Base(os.Args[0]))
3233
flag.PrintDefaults()
3334
}
3435

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
3637
optAddons := flag.Bool("addons", false, "show also extra contents")
3738
optFree := flag.Bool("free", false, "show only free titles")
3839
optWeeklyDeals := flag.Bool("weekly-deals", false, "show only weekly deals titles")
3940

41+
urls := []string{}
42+
4043
flag.Parse()
4144

4245
searchFor := len(flag.Args()) > 0 && strings.TrimSpace(flag.Args()[0]) != ""
4346

4447
var uriPath string
4548
if *optAddons {
46-
uriPath = ps.AddonsUrls[*optLang]
49+
uriPath = ps.AddonsUrls[*optRegion]
4750
} else if searchFor {
48-
uriPath = ps.SearchUrls[*optLang]
51+
uriPath = ps.SearchUrls[*optRegion]
4952
} else if *optWeeklyDeals {
50-
uriPath = ps.WeeklyDealsUrls[*optLang]
53+
uriPath = ps.WeeklyDealsUrls[*optRegion]
5154
} else {
52-
uriPath = ps.AllGamesUrls[*optLang]
55+
uriPath = ps.AllGamesUrls[*optRegion]
5356
}
5457

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))
5660
if err != nil {
5761
log.Fatal(err)
5862
}
@@ -77,19 +81,38 @@ func main() {
7781
if !*optWeeklyDeals {
7882
if *optFree {
7983
q.Add("price", "0-0")
80-
} else if *optLang == "it" {
84+
} else if *optRegion == "it" {
8185
q.Add("price", "1000-1999,2000-2999,3000-3999")
8286
}
8387
}
8488

8589
u.RawQuery = q.Encode()
8690
}
8791

88-
doSearch(u)
92+
urls = doSearch(u)
93+
prompt(urls)
8994
}
9095

91-
func doSearch(u *url.URL) {
96+
func doSearch(u *url.URL) []string {
97+
urls := []string{}
98+
idx := 0
9299
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++
94103
}
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])
95118
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/gobwas/glob v0.2.3 // indirect
1111
github.com/gocolly/colly v1.2.0
1212
github.com/kennygrant/sanitize v1.2.4 // indirect
13+
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
1314
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
1415
github.com/temoto/robotstxt v0.0.0-20180810133444-97ee4a9ee6ea // indirect
1516
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect

store.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@ var (
88
WeeklyDealsUrls = map[string]string{
99
"it": "it-IT/grid/STORE-MSF75508-DOTW1",
1010
"en": "en-US/grid/STORE-MSF77008-WEEKLYDEALS",
11+
"at": "de-AT/grid/STORE-MSF75508-DOTW2/1",
12+
"de": "de-DE/grid/STORE-MSF75508-DOTW2/1",
1113
}
1214

1315
SearchUrls = map[string]string{
1416
"it": "it-it/search/",
1517
"en": "en-US/search/",
18+
"at": "de-at/search/",
19+
"de": "de-de/search/",
1620
}
1721

1822
AddonsUrls = map[string]string{
1923
"en": "en-US/grid/STORE-MSF77008-NEWPS4ADDONSCATE",
2024
"it": "it-it/grid/STORE-MSF75508-ADDONSSEEALL",
25+
"at": "de-at/grid/STORE-MSF75508-ADDONSSEEALL",
26+
"de": "de-de/grid/STORE-MSF75508-ADDONSSEEALL",
2127
}
2228

2329
AllGamesUrls = map[string]string{
2430
"en": "en-US/grid/STORE-MSF77008-PS4ALLGAMESCATEG",
2531
"it": "it-it/grid/STORE-MSF75508-PS4CAT",
32+
"at": "de-at/grid/STORE-MSF75508-PS4CAT",
33+
"de": "de-de/grid/STORE-MSF75508-PS4CAT",
2634
}
2735
)
2836

@@ -31,6 +39,7 @@ type Game struct {
3139
Cover string
3240
Details string
3341
Price string
42+
Url string
3443
}
3544

3645
func Visit(rootUrl string) chan Game {
@@ -50,9 +59,10 @@ func Visit(rootUrl string) chan Game {
5059
title := e.ChildText("div.grid-cell__body > a")
5160
details := e.ChildText("div.grid-cell__bottom > grid-cell__details-container grid-cell__left-detail--detail-2")
5261
price := e.ChildText("div.grid-cell__bottom > div.grid-cell__footer h3.price-display__price")
62+
url := e.ChildAttr("div.grid-cell__body a", "href")
5363

5464
if price != "" {
55-
ch <- Game{Title: title, Cover: cover, Details: details, Price: price}
65+
ch <- Game{Title: title, Cover: cover, Details: details, Price: price, Url: url}
5666
}
5767
})
5868

0 commit comments

Comments
 (0)