go-roblox/item.go

157 lines
3.9 KiB
Go
Raw Normal View History

2017-01-07 05:20:45 +00:00
package roblox
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
2017-01-08 07:34:03 +00:00
"strconv"
2020-04-06 20:41:55 +00:00
"strings"
2017-01-07 05:20:45 +00:00
)
// Item struct containing data on retrieved items
type Item struct {
2017-09-28 23:27:53 +00:00
ProductID int
ItemID int
AssetID int
UserID int
2017-01-07 05:20:45 +00:00
UserName string
Type string
Name string
CopyLocked bool
Owned bool
}
2017-09-29 01:17:47 +00:00
// HasItem will return if you own the item
func (s *Session) HasItem(id int) (bool, error) {
resp, err := s.client.Get(fmt.Sprintf("http://api.roblox.com/Ownership/HasAsset?userId=%d&assetId=%d", s.ID, id))
if err != nil {
return false, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return string(data) == "true", nil
}
2017-01-07 05:20:45 +00:00
// AddItem add item by id to inventory
2020-04-06 20:41:55 +00:00
func (s *Session) AddItem(id int) error {
product, err := s.GetProduct(id)
if err != nil {
return err
}
payload := struct {
ExpectedCurrency int `json:"expectedCurrency"`
ExpectedPrice int `json:"expectedPrice"`
ExpectedSellerID int `json:"expectedSellerId"`
}{
ExpectedCurrency: 1,
ExpectedPrice: 0,
ExpectedSellerID: product.Creator.ID,
}
// Gen json body
data, err := json.Marshal(&payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://economy.roblox.com/v1/purchases/products/%d", product.ProductID), bytes.NewBuffer(data))
2017-01-07 05:20:45 +00:00
if err != nil {
return err
}
2020-04-06 20:41:55 +00:00
req.Header.Set("Content-Type", "application/json")
2017-01-07 05:20:45 +00:00
2020-04-06 20:41:55 +00:00
resp, err := s.client.Do(req)
if err != nil {
return err
}
2017-01-07 05:20:45 +00:00
defer resp.Body.Close()
if resp.StatusCode == 403 {
2020-04-06 20:41:55 +00:00
req, err := http.NewRequest("POST", fmt.Sprintf("https://economy.roblox.com/v1/purchases/products/%d", product.ProductID), bytes.NewBuffer(data))
2017-01-07 05:20:45 +00:00
req.Header.Set("Content-Type", "application/json")
2020-04-06 20:41:55 +00:00
req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0])
2017-01-07 05:20:45 +00:00
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Failed to add item. Status %d", resp.StatusCode)
}
}
return nil
}
// RemoveItem will remove item from inventory
2017-09-28 23:27:53 +00:00
func (s *Session) RemoveItem(id int) error {
2017-01-07 05:20:45 +00:00
v := url.Values{}
2017-09-28 23:27:53 +00:00
v.Set("assetId", strconv.Itoa(id))
2017-01-07 05:20:45 +00:00
resp, err := s.client.Post("https://www.roblox.com/asset/delete-from-inventory", "application/x-www-form-urlencoded", bytes.NewBufferString(v.Encode()))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 403 {
req, err := http.NewRequest("POST", "https://www.roblox.com/asset/delete-from-inventory", bytes.NewBufferString(v.Encode()))
2020-04-06 20:41:55 +00:00
req.Header.Set("x-csrf-token", strings.Trim(resp.Header["X-Csrf-Token"][0], " "))
2017-01-07 05:20:45 +00:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Failed to remove item. Status %d", resp.StatusCode)
}
}
return nil
}
// GetModels will list models in inventory
2017-09-28 23:27:53 +00:00
func (s *Session) GetModels(user int) ([]Item, error) {
2017-01-07 05:20:45 +00:00
var Data []Item
2017-09-28 23:27:53 +00:00
resp, err := s.client.Get("https://www.roblox.com/users/inventory/list-json?assetTypeId=10&userId=" + strconv.Itoa(user))
2017-01-07 05:20:45 +00:00
if err != nil {
return Data, fmt.Errorf("Could not get list: %s", err)
}
if resp.StatusCode == 200 {
var dat map[string]interface{}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err := json.Unmarshal(body, &dat); err != nil {
panic(err)
}
set := dat["Data"].(map[string]interface{})
ilist := set["Items"].([]interface{})
for _, obj := range ilist {
itm := obj.(map[string]interface{})
iInfo := itm["Item"].(map[string]interface{})
iCreator := itm["Creator"].(map[string]interface{})
2017-09-28 23:27:53 +00:00
Data = append(Data, Item{0, int(iInfo["AssetId"].(float64)), 0, int(iCreator["Id"].(float64)), iCreator["Name"].(string), "Model", iInfo["Name"].(string), false, true})
2017-01-07 05:20:45 +00:00
}
return Data, nil
}
return Data, fmt.Errorf("Could not get models. Status: %d", resp.StatusCode)
}