157 lines
3.9 KiB
Go
157 lines
3.9 KiB
Go
package roblox
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Item struct containing data on retrieved items
|
|
type Item struct {
|
|
ProductID int
|
|
ItemID int
|
|
AssetID int
|
|
UserID int
|
|
UserName string
|
|
Type string
|
|
Name string
|
|
CopyLocked bool
|
|
Owned bool
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// AddItem add item by id to inventory
|
|
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))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == 403 {
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("https://economy.roblox.com/v1/purchases/products/%d", product.ProductID), bytes.NewBuffer(data))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0])
|
|
|
|
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
|
|
func (s *Session) RemoveItem(id int) error {
|
|
v := url.Values{}
|
|
v.Set("assetId", strconv.Itoa(id))
|
|
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()))
|
|
req.Header.Set("x-csrf-token", strings.Trim(resp.Header["X-Csrf-Token"][0], " "))
|
|
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
|
|
func (s *Session) GetModels(user int) ([]Item, error) {
|
|
var Data []Item
|
|
|
|
resp, err := s.client.Get("https://www.roblox.com/users/inventory/list-json?assetTypeId=10&userId=" + strconv.Itoa(user))
|
|
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{})
|
|
Data = append(Data, Item{0, int(iInfo["AssetId"].(float64)), 0, int(iCreator["Id"].(float64)), iCreator["Name"].(string), "Model", iInfo["Name"].(string), false, true})
|
|
}
|
|
|
|
return Data, nil
|
|
}
|
|
return Data, fmt.Errorf("Could not get models. Status: %d", resp.StatusCode)
|
|
}
|