Compare commits

...

5 Commits

Author SHA1 Message Date
d84bfb7eba Roblox now just returns the asset id 2021-12-31 17:36:54 -05:00
itzaname
3574e3d70d Update 'go.mod' 2021-08-10 23:54:37 +00:00
a09b6e34a3 csrf token 2021-03-17 08:46:02 -04:00
41119b5966 Maybe fix this 2021-03-17 08:41:51 -04:00
9652a2630e Update to allow overwriting 2020-09-22 12:30:25 -04:00
2 changed files with 27 additions and 13 deletions

View File

@ -1,16 +1,18 @@
package roblox package roblox
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings"
) )
type AssetUploadOptions struct { type AssetUploadOptions struct {
Name string Name string
AssetID int
Description string Description string
Public bool Public bool
Comments bool Comments bool
@ -22,17 +24,16 @@ type AssetUploadResponse struct {
AssetVersionID int64 `json:"AssetVersionId"` AssetVersionID int64 `json:"AssetVersionId"`
} }
func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (AssetUploadResponse, error) { func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (int, error) {
var aresp AssetUploadResponse endpoint, err := url.Parse("https://data.roblox.com/Data/Upload.ashx?json=1&type=Model&genreTypeId=1")
endpoint, err := url.Parse("https://data.roblox.com/Data/Upload.ashx?json=1&assetid=0&type=Model&genreTypeId=1")
if err != nil { if err != nil {
return aresp, err return -1, err
} }
query := endpoint.Query() query := endpoint.Query()
query.Set("name", options.Name) query.Set("name", options.Name)
query.Set("description", options.Description) query.Set("description", options.Description)
query.Set("assetid", strconv.Itoa(options.AssetID))
// Comments // Comments
if options.Comments { if options.Comments {
@ -55,25 +56,38 @@ func (s *Session) CreateAsset(options *AssetUploadOptions, f io.Reader) (AssetUp
endpoint.RawQuery = query.Encode() endpoint.RawQuery = query.Encode()
req, err := http.NewRequest("POST", endpoint.String(), f) req, err := http.NewRequest("POST", endpoint.String(), nil)
req.Header.Set("user-agent", "Roblox") req.Header.Set("user-agent", "Roblox")
// Perform request // Perform request
resp, err := s.client.Do(req) resp, err := s.client.Do(req)
if err != nil { if err != nil {
return aresp, err return -1, err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == 403 && resp.Header.Get("X-Csrf-Token") != "" {
req, err := http.NewRequest("POST", endpoint.String(), f)
req.Header.Set("user-agent", "Roblox")
req.Header.Set("x-csrf-token", strings.Trim(resp.Header["X-Csrf-Token"][0], " "))
// Perform request
resp, err = s.client.Do(req)
if err != nil {
return -1, err
}
defer resp.Body.Close()
}
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return aresp, fmt.Errorf(resp.Status) return -1, fmt.Errorf(resp.Status)
} }
if err := json.NewDecoder(resp.Body).Decode(&aresp); err != nil { body, err := ioutil.ReadAll(resp.Body)
return aresp, err if err != nil {
return -1, err
} }
return aresp, nil return strconv.Atoi(string(body))
} }
func (s *Session) Download(id int) (io.Reader, error) { func (s *Session) Download(id int) (io.Reader, error) {

2
go.mod
View File

@ -1,3 +1,3 @@
module ci.itzana.me/itzaname/go-roblox module git.itzana.me/itzaname/go-roblox
go 1.15 go 1.15