|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/gocolly/colly" |
| 11 | + "github.com/lucasepe/go-ps4/utils" |
| 12 | +) |
| 13 | + |
| 14 | +type Game struct { |
| 15 | + Title string |
| 16 | + Cover string |
| 17 | + Details string |
| 18 | + Price string |
| 19 | +} |
| 20 | + |
| 21 | +var ( |
| 22 | + version string |
| 23 | + date string |
| 24 | +) |
| 25 | + |
| 26 | +func visit(rootUrl string) chan Game { |
| 27 | + ch := make(chan Game) |
| 28 | + |
| 29 | + go func() { |
| 30 | + // [\p{Sc}] |
| 31 | + |
| 32 | + collector := colly.NewCollector( |
| 33 | + colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0"), |
| 34 | + colly.AllowedDomains("store.playstation.com"), |
| 35 | + //colly.Async(true), |
| 36 | + //colly.CacheDir("./playstation-store-cache/"), |
| 37 | + ) |
| 38 | + |
| 39 | + collector.OnHTML("div[class*='grid-cell--game']", func(e *colly.HTMLElement) { |
| 40 | + cover := e.ChildAttr("div.grid-cell__thumbnail img[src^='https://store.playstation.com/store/api/chihiro']", "src") |
| 41 | + |
| 42 | + title := e.ChildText("div.grid-cell__body > a") |
| 43 | + details := e.ChildText("div.grid-cell__bottom > grid-cell__details-container grid-cell__left-detail--detail-2") |
| 44 | + price := e.ChildText("div.grid-cell__bottom > div.grid-cell__footer h3.price-display__price") |
| 45 | + |
| 46 | + if price != "" { |
| 47 | + ch <- Game{Title: title, Cover: cover, Details: details, Price: price} |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + collector.OnHTML("div.grid-footer-controls a.paginator-control__next", func(e *colly.HTMLElement) { |
| 52 | + link := e.Request.AbsoluteURL(e.Attr("href")) |
| 53 | + e.Request.Visit(link) |
| 54 | + }) |
| 55 | + |
| 56 | + collector.OnRequest(func(r *colly.Request) { |
| 57 | + //log.Println("visiting", r.URL.String()) |
| 58 | + }) |
| 59 | + |
| 60 | + collector.Visit(rootUrl) |
| 61 | + |
| 62 | + collector.Wait() |
| 63 | + |
| 64 | + close(ch) |
| 65 | + }() |
| 66 | + |
| 67 | + return ch |
| 68 | +} |
| 69 | + |
| 70 | +func showVersion() { |
| 71 | + fmt.Printf("PS4 Games CLI (build:%s@%s)\n", version, date) |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * NIX: go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%" |
| 76 | + * |
| 77 | + * WIN: go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~-4%-%date:~3,2%-%date:~6,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%" |
| 78 | + */ |
| 79 | +func main() { |
| 80 | + |
| 81 | + weeklyDealsUrls := map[string]string{ |
| 82 | + "it": "it-IT/grid/STORE-MSF75508-DOTW1", |
| 83 | + "en": "en-US/grid/STORE-MSF77008-WEEKLYDEALS", |
| 84 | + } |
| 85 | + |
| 86 | + searchUrls := map[string]string{ |
| 87 | + "it": "it-it/search/", |
| 88 | + "en": "en-US/search/", |
| 89 | + } |
| 90 | + |
| 91 | + addonsUrls := map[string]string{ |
| 92 | + "en": "en-US/grid/STORE-MSF77008-NEWPS4ADDONSCATE", |
| 93 | + "it": "it-it/grid/STORE-MSF75508-ADDONSSEEALL", |
| 94 | + } |
| 95 | + |
| 96 | + allGamesUrls := map[string]string{ |
| 97 | + "en": "en-US/grid/STORE-MSF77008-PS4ALLGAMESCATEG", |
| 98 | + "it": "it-it/grid/STORE-MSF75508-PS4CAT", |
| 99 | + } |
| 100 | + |
| 101 | + optVersion := flag.Bool("version", false, "show app version") |
| 102 | + optLang := flag.String("lang", "it", "language (it, en)") |
| 103 | + optAddons := flag.Bool("addons", false, "show also extra contents") |
| 104 | + optFree := flag.Bool("free", false, "show only free titles") |
| 105 | + optWeeklyDeals := flag.Bool("weekly-deals", false, "show only weekly deals titles") |
| 106 | + optSearch := flag.String("search", "", "search for specified title") |
| 107 | + |
| 108 | + flag.Parse() |
| 109 | + |
| 110 | + if *optVersion { |
| 111 | + showVersion() |
| 112 | + os.Exit(0) |
| 113 | + } |
| 114 | + |
| 115 | + var uriPath string |
| 116 | + if *optAddons { |
| 117 | + uriPath = addonsUrls[*optLang] |
| 118 | + } else if *optSearch != "" { |
| 119 | + uriPath = searchUrls[*optLang] |
| 120 | + } else if *optWeeklyDeals { |
| 121 | + uriPath = weeklyDealsUrls[*optLang] |
| 122 | + } else { |
| 123 | + uriPath = allGamesUrls[*optLang] |
| 124 | + } |
| 125 | + |
| 126 | + u, err := url.Parse(fmt.Sprintf("https://store.playstation.com/%s", uriPath)) |
| 127 | + if err != nil { |
| 128 | + log.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + if *optSearch == "" { |
| 132 | + q := u.Query() |
| 133 | + q.Add("direction", "asc") |
| 134 | + q.Add("sort", "price") |
| 135 | + q.Add("platform", "ps4") |
| 136 | + |
| 137 | + if !*optAddons && !*optWeeklyDeals { |
| 138 | + q.Add("gameContentType", "games,bundles") |
| 139 | + } |
| 140 | + |
| 141 | + if !*optWeeklyDeals { |
| 142 | + if *optFree { |
| 143 | + q.Add("price", "0-0") |
| 144 | + } else if *optLang == "it" { |
| 145 | + q.Add("price", "1000-1999,2000-2999,3000-3999") |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + u.RawQuery = q.Encode() |
| 150 | + |
| 151 | + } else { |
| 152 | + u, err = url.Parse(fmt.Sprintf("https://store.playstation.com/%s/%s", uriPath, *optSearch)) |
| 153 | + if err != nil { |
| 154 | + log.Fatal(err) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + for g := range visit(u.String()) { |
| 159 | + fmt.Printf("%s %s\n", utils.RightPad(g.Title, 68, "."), utils.RightPad(g.Price, 7, " ")) |
| 160 | + } |
| 161 | +} |
0 commit comments