Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Enable remote mode, add auto checkout again yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmarmour committed Sep 24, 2020
1 parent 51e1023 commit 10f6380
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ var regionalConfig = map[string]RegionalConfig{
},
"USA": {
Models: map[string]Model{
"2060": {
SKU: strPtr("5379432500"),
},
"3070": {},
"3080": {
SKU: strPtr("5438481700"),
Expand Down
93 changes: 84 additions & 9 deletions internal/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package rest

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -98,32 +100,105 @@ type InventoryProduct struct {
AvailableQuantity int `json:"availableQuantity"`
}

func GetSkuInfo(sku string, locale string, currency string, client *http.Client) (*ProductsResponse, error) {
endpoint := fmt.Sprintf("https://api-prod.nvidia.com/direct-sales-shop/DR/products/%s/%s/%s", locale, currency, sku)
type SessionToken struct {
Value string `json:"session_token"`
}

type AddToCartResponse struct {
URL string `json:"location"`
}

r, err := client.Get(endpoint)
func GetSessionToken(client *http.Client) (*SessionToken, error) {
url := "https://store.nvidia.com/store/nvidia/SessionToken?format=json"

resBody, err := getBody(url, client)
if err != nil {
message := fmt.Sprintf("Error attempting to access URL: %s", endpoint)
log.Println(message)
log.Println(err)
return nil, err
}

if r.Body != nil {
defer r.Body.Close()
session := SessionToken{}
jsonErr := json.Unmarshal(resBody, &session)
if jsonErr != nil {
log.Println(jsonErr)
return nil, jsonErr
}

body, readErr := ioutil.ReadAll(r.Body)
return &session, nil
}

func AddToCheckout(sku string, token string, locale string) (*AddToCartResponse, error) {
url := "https://api-prod.nvidia.com/direct-sales-shop/DR/add-to-cart"

body := []byte(fmt.Sprintf(`{"products": [{"productId":%s,"quantity": 1}]}`, sku))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}

req.Header.Set("locale", locale)
req.Header.Set("content-type", "application/json")
req.Header.Set("nvidia_shop_id", token)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode > 400 {
return nil, errors.New("Invalid response")
}
defer res.Body.Close()

body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Println(readErr)
return nil, readErr
}

cart := AddToCartResponse{}

jsonErr := json.Unmarshal(body, &cart)
if jsonErr != nil {
return nil, jsonErr
}

return &cart, nil
}

func GetSkuInfo(sku string, locale string, currency string, client *http.Client) (*ProductsResponse, error) {
url := fmt.Sprintf("https://api-prod.nvidia.com/direct-sales-shop/DR/products/%s/%s/%s", locale, currency, sku)

resBody, err := getBody(url, client)
if err != nil {
log.Println(err)
return nil, err
}

products := ProductsResponse{}
jsonErr := json.Unmarshal(body, &products)
jsonErr := json.Unmarshal(resBody, &products)
if jsonErr != nil {
log.Println(jsonErr)
return nil, jsonErr
}

return &products, nil
}

func getBody(url string, client *http.Client) ([]byte, error) {
r, err := client.Get(url)
if err != nil {
message := fmt.Sprintf("Error attempting to access URL: %s", url)
log.Println(message)
return nil, err
}
defer r.Body.Close()

body, readErr := ioutil.ReadAll(r.Body)
if readErr != nil {
log.Println(readErr)
return nil, readErr
}

return body, nil
}
27 changes: 16 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {
twilio := flag.Bool("sms", false, "Enable SMS notifications for whenever SKU is in stock.")
discord := flag.Bool("discord", false, "Enable Discord webhook notifications for whenever SKU is in stock.")
telegram := flag.Bool("telegram", false, "Enable Telegram webhook notifications for whenever SKU is in stock.")
test := flag.Bool("test", false, "Enable testing mode")
remote := flag.Bool("remote", false, "Enable notification only mode.")
test := flag.Bool("test", false, "Enable remote mode for when you're away from computer.")
flag.Parse()

config, configErr := config.Get(region, model, delay, *twilio, *discord, *twitter, *telegram)
Expand All @@ -41,6 +42,10 @@ func main() {
}

client := &http.Client{Timeout: 10 * time.Second}
token, err := rest.GetSessionToken(client)
if err != nil {
log.Fatal("Error getting session token from NVIDIA store.")
}

for {
sleep(delay)
Expand All @@ -62,17 +67,22 @@ func main() {
log.Println("Product Status: " + info.Products.Product[0].InventoryStatus.Status + "\n")

if info.Products.Product[0].InventoryStatus.Status == "PRODUCT_INVENTORY_IN_STOCK" || *test == true {
url := productUrl(config.NvidiaLocale, model)
cart, err := rest.AddToCheckout(*config.SKU, token.Value, config.Locale)
if err != nil {
log.Fatal("Error adding card to checkout.")
}
id := info.Products.Product[0].Name

err := notify(id, url, config, client)
err = notify(id, cart.URL, config, client)
if err != nil {
log.Fatal("Error attempting to send notification.", err)
}

err = openbrowser(url)
if err != nil {
log.Fatal("Error attempting to open browser.", err)
if *remote != true {
err = openbrowser(cart.URL)
if err != nil {
log.Fatal("Error attempting to open browser.", err)
}
}

break
Expand Down Expand Up @@ -116,11 +126,6 @@ func notify(id string, url string, config *config.Config, client *http.Client) e
return nil
}

func productUrl(locale string, model string) string {
url := fmt.Sprintf("https://www.nvidia.com/%s/geforce/graphics-cards/30-series/rtx-%s/", locale, model)
return url
}

func openbrowser(url string) error {
var err error

Expand Down

0 comments on commit 10f6380

Please sign in to comment.