Update to ALMOST support 0.35 with new metadata

This commit is contained in:
Niko Storni 2019-04-03 00:44:30 +02:00
parent 41b4a3684a
commit 6f0d34f863
No known key found for this signature in database
GPG key ID: F37FE63398800368
4 changed files with 209 additions and 164 deletions

View file

@ -3,7 +3,7 @@ package claim
import ( import (
"bytes" "bytes"
types "github.com/lbryio/types/v1/go" types "github.com/lbryio/types/v2/go"
"github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"

View file

@ -10,9 +10,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/fatih/structs"
"github.com/lbryio/lbry.go/extras/errors" "github.com/lbryio/lbry.go/extras/errors"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -180,55 +181,137 @@ func (d *Client) ChannelList(account *string, page uint64, pageSize uint64) (*Ch
}) })
} }
type Metadata struct { type streamType string
Fee *Fee `json:"fee,omitempty"`
Title string `json:"title"` var (
Description string `json:"description"` StreamTypeVideo = streamType("video")
Author string `json:"author"` StreamTypeAudio = streamType("audio")
Language string `json:"language"` StreamTypeImage = streamType("image")
License string `json:"license"` )
LicenseURL *string `json:"license_url,omitempty"`
Thumbnail *string `json:"thumbnail,omitempty"` type Locations struct {
Preview *string `json:"preview,omitempty"` Country *string `json:"country,omitempty"`
NSFW bool `json:"nsfw"` State *string `json:"state,omitempty"`
City *string `json:"city,omitempty"`
PostalCode *string `json:"code,omitempty"`
Latitude *string `json:"latitude,omitempty"`
Longitude *string `json:"longitude,omitempty"`
} }
type PublishOptions struct { type ClaimCreateOptions struct {
*Metadata `json:"metadata"` Title string `json:"title"`
ChannelName *string `json:"channel_name,omitempty"` Description string `json:"description"`
ChannelID *string `json:"channel_id,omitempty"` Tags []string `json:"tags,omitempty"`
ChannelAccountID *string `json:"channel_account_id,omitempty"` Languages []string `json:"language"`
AccountID *string `json:"account_id,omitempty"` Locations *Locations `json:"locations,omitempty"`
ClaimAddress *string `json:"claim_address,omitempty"` ThumbnailURL *string `json:"thumbnail_url,omitempty"`
ChangeAddress *string `json:"change_address,omitempty"` AccountID *string `json:"account_id,omitempty"`
ClaimAddress *string `json:"claim_address,omitempty"`
ChangeAddress *string `json:"change_address,omitempty"`
Preview *bool `json:"preview,omitempty"`
} }
func (d *Client) Publish(name, filePath string, bid float64, options PublishOptions) (*PublishResponse, error) { type ChannelCreateOptions struct {
*ClaimCreateOptions `json:",flatten"`
ContactEmail *string `json:"contact_email,omitempty"`
HomepageURL *string `json:"homepage_url,omitempty"`
CoverURL *string `json:"cover_url,omitempty"`
}
func (d *Client) ChannelCreate(name string, bid float64, options *ChannelCreateOptions) (*PublishResponse, error) {
response := new(PublishResponse) response := new(PublishResponse)
args := struct { args := struct {
Name string `json:"name"` Name string `json:"name"`
FilePath string `json:"file_path,omitempty"` Bid string `json:"bid"`
Bid string `json:"bid"` FilePath string `json:"file_path,omitempty"`
*PublishOptions `json:",flatten"` *ChannelCreateOptions `json:",flatten"`
}{ }{
Name: name, Name: name,
FilePath: filePath, Bid: fmt.Sprintf("%.6f", bid),
Bid: fmt.Sprintf("%.6f", bid), ChannelCreateOptions: options,
PublishOptions: &options,
} }
structs.DefaultTagName = "json" structs.DefaultTagName = "json"
return response, d.call(response, "publish", structs.Map(args)) return response, d.call(response, "channel_create", structs.Map(args))
} }
func (d *Client) ChannelNew(name string, amount float64, accountID *string) (*ChannelNewResponse, error) { type StreamCreateOptions struct {
response := new(ChannelNewResponse) *ClaimCreateOptions `json:",flatten"`
return response, d.call(response, "channel_new", map[string]interface{}{ Fee *Fee `json:"fee,omitempty"`
"channel_name": name, Author *string `json:"author"`
"amount": fmt.Sprintf("%.6f", amount), License *string `json:"license"`
"account_id": accountID, LicenseURL *string `json:"license_url,omitempty"`
StreamType *streamType `json:"stream_type,omitempty"`
ReleaseTime *int `json:"release_time,omitempty"`
Duration *int `json:"duration,omitempty"`
ImageWidth *int `json:"image_width,omitempty"`
ImageHeigth *int `json:"image_heigth,omitempty"`
VideoWidth *int `json:"video_width,omitempty"`
VideoHeight *int `json:"video_height,omitempty"`
Preview *string `json:"preview,omitempty"`
AllowDuplicateName *bool `json:"allow_duplicate_name,omitempty"`
ChannelName *string `json:"channel_name,omitempty"`
ChannelID *string `json:"channel_id,omitempty"`
ChannelAccountID *string `json:"channel_account_id,omitempty"`
}
func (d *Client) StreamCreate(name, filePath string, bid float64, options StreamCreateOptions) (*PublishResponse, error) {
response := new(PublishResponse)
args := struct {
Name string `json:"name"`
Bid string `json:"bid"`
FilePath string `json:"file_path,omitempty"`
*StreamCreateOptions `json:",flatten"`
}{
Name: name,
FilePath: filePath,
Bid: fmt.Sprintf("%.6f", bid),
StreamCreateOptions: &options,
}
structs.DefaultTagName = "json"
return response, d.call(response, "stream_create", structs.Map(args))
}
func (d *Client) StreamAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) {
response := new(ClaimAbandonResponse)
err := d.call(response, "claim_abandon", map[string]interface{}{
"txid": txID,
"nout": nOut,
"account_id": accountID,
}) })
if err != nil {
return nil, err
} else if response == nil {
return nil, errors.Err("no response")
}
return response, nil
} }
func (d *Client) ClaimAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) { type StreamUpdateOptions struct {
ClearTags *bool `json:"clear_tags,omitempty"`
ClearLanguages *bool `json:"clear_languages,omitempty"`
ClearLocations *bool `json:"clear_locations,omitempty"`
Name *string `json:"name"`
FilePath *string `json:"file_path,omitempty"`
Bid *string `json:"bid"`
*StreamCreateOptions `json:",flatten"`
}
func (d *Client) StreamUpdate(claimID string, options StreamUpdateOptions) (*PublishResponse, error) {
response := new(PublishResponse)
args := struct {
ClaimID string `json:"claim_id"`
FilePath string `json:"file_path,omitempty"`
Bid string `json:"bid"`
*StreamUpdateOptions `json:",flatten"`
}{
ClaimID: claimID,
StreamUpdateOptions: &options,
}
structs.DefaultTagName = "json"
return response, d.call(response, "stream_create", structs.Map(args))
}
func (d *Client) ChannelAbandon(txID string, nOut uint64, accountID *string, blocking bool) (*ClaimAbandonResponse, error) {
response := new(ClaimAbandonResponse) response := new(ClaimAbandonResponse)
err := d.call(response, "claim_abandon", map[string]interface{}{ err := d.call(response, "claim_abandon", map[string]interface{}{
"txid": txID, "txid": txID,
@ -251,19 +334,12 @@ func (d *Client) AddressList(account *string) (*AddressListResponse, error) {
}) })
} }
func (d *Client) ClaimList(name string) (*ClaimListResponse, error) { func (d *Client) ClaimList(account *string, page uint64, pageSize uint64) (*ClaimListMineResponse, error) {
response := new(ClaimListResponse)
return response, d.call(response, "claim_list", map[string]interface{}{
"name": name,
})
}
func (d *Client) ClaimListMine(account *string, page uint64, pageSize uint64) (*ClaimListMineResponse, error) {
if page == 0 { if page == 0 {
return nil, errors.Err("pages start from 1") return nil, errors.Err("pages start from 1")
} }
response := new(ClaimListMineResponse) response := new(ClaimListMineResponse)
err := d.call(response, "claim_list_mine", map[string]interface{}{ err := d.call(response, "claim_list", map[string]interface{}{
"account_id": account, "account_id": account,
"page": page, "page": page,
"page_size": pageSize, "page_size": pageSize,
@ -301,10 +377,12 @@ func (d *Client) Resolve(urls string) (*ResolveResponse, error) {
}) })
} }
func (d *Client) NumClaimsInChannel(uri string) (uint64, error) { /*
// use resolve?
func (d *Client) NumClaimsInChannel(channelClaimID string) (uint64, error) {
response := new(NumClaimsInChannelResponse) response := new(NumClaimsInChannelResponse)
err := d.call(response, "claim_list_by_channel", map[string]interface{}{ err := d.call(response, "claim_search", map[string]interface{}{
"uri": uri, "channel_id": channelClaimID,
}) })
if err != nil { if err != nil {
return 0, err return 0, err
@ -324,12 +402,13 @@ func (d *Client) NumClaimsInChannel(uri string) (uint64, error) {
} }
return *channel.ClaimsInChannel, nil return *channel.ClaimsInChannel, nil
} }
*/
func (d *Client) ClaimShow(claimID *string, txid *string, nout *uint) (*ClaimShowResponse, error) { func (d *Client) ClaimSearch(claimName, claimID, txid *string, nout *uint) (*ClaimSearchResponse, error) {
response := new(ClaimShowResponse) response := new(ClaimSearchResponse)
return response, d.call(response, "claim_show", map[string]interface{}{ return response, d.call(response, "claim_search", map[string]interface{}{
"claim_id": claimID, "claim_id": claimID,
"txid": txid, "txid": txid,
"nout": nout, "nout": nout,
"name": claimName,
}) })
} }

View file

@ -7,8 +7,9 @@ import (
"testing" "testing"
"time" "time"
"github.com/lbryio/lbry.go/extras/util"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
"github.com/lbryio/lbry.go/extras/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -55,29 +56,46 @@ func TestClient_Publish(t *testing.T) {
t.Error(err) t.Error(err)
} }
address := string(*addressResponse) address := string(*addressResponse)
got, err := d.Publish("test", "/home/niko/work/allClaims.txt", 14.37, PublishOptions{ got, err := d.StreamCreate("test"+string(time.Now().Unix()), "/home/niko/work/allClaims.txt", 14.37, StreamCreateOptions{
Metadata: &Metadata{ ClaimCreateOptions: &ClaimCreateOptions{
Fee: &Fee{ Title: "This is a Test Title" + time.Now().String(),
Currency: "LBC",
Amount: decimal.NewFromFloat(1.0),
Address: &address,
},
Title: "This is a Test Title",
Description: "My Special Description", Description: "My Special Description",
Author: "Niko", Tags: []string{"nsfw", "test"},
Language: "en", Languages: []string{"en-US", "fr-CH"},
License: "FREEEEE", Locations: &Locations{
LicenseURL: nil, Country: util.PtrToString("CH"),
Thumbnail: util.PtrToString("https://scrn.storni.info/2019-01-18_16-37-39-098537783.png"), State: util.PtrToString("Ticino"),
Preview: nil, City: util.PtrToString("Lugano"),
NSFW: false, PostalCode: util.PtrToString("6900"),
Latitude: nil,
Longitude: nil,
},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-01-18_16-37-39-098537783.png"),
AccountID: nil,
ClaimAddress: &address,
ChangeAddress: &address,
Preview: nil,
}, },
ChannelName: nil, Fee: &Fee{
ChannelID: util.PtrToString("bda0520bff61e4a70c966d7298e6b89107cf8bed"), Currency: "LBC",
ChannelAccountID: nil, Amount: decimal.NewFromFloat(1.0),
AccountID: nil, Address: &address,
ClaimAddress: &address, },
ChangeAddress: &address, Author: util.PtrToString("Niko"),
License: util.PtrToString("FREE"),
LicenseURL: nil,
StreamType: &StreamTypeImage,
ReleaseTime: nil,
Duration: nil,
ImageWidth: nil,
ImageHeigth: nil,
VideoWidth: nil,
VideoHeight: nil,
Preview: nil,
AllowDuplicateName: nil,
ChannelName: nil,
ChannelID: util.PtrToString("bda0520bff61e4a70c966d7298e6b89107cf8bed"),
ChannelAccountID: nil,
}) })
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -85,25 +103,25 @@ func TestClient_Publish(t *testing.T) {
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ChannelNew(t *testing.T) { func TestClient_ChannelCreate(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ChannelNew("@Test", 13.37, nil) got, err := d.ChannelCreate("@Test", 13.37, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ClaimAbandon(t *testing.T) { func TestClient_ChannelAbandon(t *testing.T) {
d := NewClient("") d := NewClient("")
channelResponse, err := d.ChannelNew("@TestToDelete", 13.37, nil) channelResponse, err := d.ChannelCreate("@TestToDelete", 13.37, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
txID := channelResponse.Output.Txid txID := channelResponse.Output.Txid
nout := channelResponse.Output.Nout nout := channelResponse.Output.Nout
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
got, err := d.ClaimAbandon(txID, nout, nil, false) got, err := d.ChannelAbandon(txID, nout, nil, false)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -121,16 +139,16 @@ func TestClient_AddressList(t *testing.T) {
func TestClient_ClaimList(t *testing.T) { func TestClient_ClaimList(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ClaimList("test") got, err := d.ClaimList(nil, 1, 10)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
log.Infof("%+v", *got) log.Infof("%+v", *got)
} }
func TestClient_ClaimListMine(t *testing.T) { func TestClient_ClaimSearch(t *testing.T) {
d := NewClient("") d := NewClient("")
got, err := d.ClaimListMine(nil, 1, 50) got, err := d.ClaimSearch(nil, util.PtrToString("4742f25e6d51b4b0483d5b8cd82e3ea121dacde9"), nil, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -177,24 +195,6 @@ func TestClient_Resolve(t *testing.T) {
log.Infof("%s", b) log.Infof("%s", b)
} }
func TestClient_NumClaimsInChannel(t *testing.T) {
d := NewClient("")
got, err := d.NumClaimsInChannel("@Test#bda0520bff61e4a70c966d7298e6b89107cf8bed")
if err != nil {
t.Error(err)
}
log.Infof("%d", got)
}
func TestClient_ClaimShow(t *testing.T) {
d := NewClient("")
got, err := d.ClaimShow(util.PtrToString("4742f25e6d51b4b0483d5b8cd82e3ea121dacde9"), nil, nil)
if err != nil {
t.Error(err)
}
log.Infof("%+v", *got)
}
func TestClient_AccountFund(t *testing.T) { func TestClient_AccountFund(t *testing.T) {
d := NewClient("") d := NewClient("")
accounts, err := d.AccountList() accounts, err := d.AccountList()

View file

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"github.com/lbryio/lbry.go/extras/errors" "github.com/lbryio/lbry.go/extras/errors"
lbryschema "github.com/lbryio/types/v1/go" lbryschema "github.com/lbryio/types/v2/go"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
@ -93,49 +93,13 @@ func fixDecodeProto(src, dest reflect.Type, data interface{}) (interface{}, erro
return d, nil return d, nil
} }
case reflect.TypeOf(lbryschema.Metadata_Version(0)):
val, err := getEnumVal(lbryschema.Metadata_Version_value, data)
return lbryschema.Metadata_Version(val), err
case reflect.TypeOf(lbryschema.Metadata_Language(0)):
val, err := getEnumVal(lbryschema.Metadata_Language_value, data)
return lbryschema.Metadata_Language(val), err
case reflect.TypeOf(lbryschema.Stream_Version(0)):
val, err := getEnumVal(lbryschema.Stream_Version_value, data)
return lbryschema.Stream_Version(val), err
case reflect.TypeOf(lbryschema.Claim_Version(0)):
val, err := getEnumVal(lbryschema.Claim_Version_value, data)
return lbryschema.Claim_Version(val), err
case reflect.TypeOf(lbryschema.Claim_ClaimType(0)):
val, err := getEnumVal(lbryschema.Claim_ClaimType_value, data)
return lbryschema.Claim_ClaimType(val), err
case reflect.TypeOf(lbryschema.Fee_Version(0)):
val, err := getEnumVal(lbryschema.Fee_Version_value, data)
return lbryschema.Fee_Version(val), err
case reflect.TypeOf(lbryschema.Fee_Currency(0)): case reflect.TypeOf(lbryschema.Fee_Currency(0)):
val, err := getEnumVal(lbryschema.Fee_Currency_value, data) val, err := getEnumVal(lbryschema.Fee_Currency_value, data)
return lbryschema.Fee_Currency(val), err return lbryschema.Fee_Currency(val), err
case reflect.TypeOf(lbryschema.Source_Version(0)): case reflect.TypeOf(lbryschema.Claim_Type_name):
val, err := getEnumVal(lbryschema.Source_Version_value, data) val, err := getEnumVal(lbryschema.Claim_Type_value, data)
return lbryschema.Source_Version(val), err return lbryschema.Claim_Type_name[val], err
case reflect.TypeOf(lbryschema.Source_SourceTypes(0)):
val, err := getEnumVal(lbryschema.Source_SourceTypes_value, data)
return lbryschema.Source_SourceTypes(val), err
case reflect.TypeOf(lbryschema.KeyType(0)):
val, err := getEnumVal(lbryschema.KeyType_value, data)
return lbryschema.KeyType(val), err
case reflect.TypeOf(lbryschema.Signature_Version(0)):
val, err := getEnumVal(lbryschema.Signature_Version_value, data)
return lbryschema.Signature_Version(val), err
case reflect.TypeOf(lbryschema.Certificate_Version(0)):
val, err := getEnumVal(lbryschema.Certificate_Version_value, data)
return lbryschema.Certificate_Version(val), err
} }
return data, nil return data, nil
@ -296,33 +260,34 @@ type Support struct {
} }
type Claim struct { type Claim struct {
Address string `json:"address"` Address string `json:"address"`
Amount string `json:"amount"` Amount string `json:"amount"`
ChannelName *string `json:"channel_name,omitempty"` ChannelName *string `json:"channel_name,omitempty"`
ClaimID string `json:"claim_id"` ClaimID string `json:"claim_id"`
ClaimSequence int64 `json:"claim_sequence"` ClaimSequence int64 `json:"claim_sequence"`
DecodedClaim bool `json:"decoded_claim"` DecodedClaim bool `json:"decoded_claim"`
Depth int64 `json:"depth"` Depth int64 `json:"depth"`
EffectiveAmount string `json:"effective_amount"` EffectiveAmount string `json:"effective_amount"`
HasSignature *bool `json:"has_signature,omitempty"` HasSignature *bool `json:"has_signature,omitempty"`
Height int `json:"height"` Height int `json:"height"`
Hex string `json:"hex"` Hex string `json:"hex"`
Name string `json:"name"` Name string `json:"name"`
Nout uint64 `json:"nout"` NormalizedName string `json:"normalized_name"`
PermanentUrl string `json:"permanent_url"` Nout uint64 `json:"nout"`
SignatureIsValid *bool `json:"signature_is_valid,omitempty"` PermanentUrl string `json:"permanent_url"`
Supports []Support `json:"supports"` SignatureIsValid *bool `json:"signature_is_valid,omitempty"`
Txid string `json:"txid"` Supports []Support `json:"supports"`
Type string `json:"type"` Txid string `json:"txid"`
ValidAtHeight int `json:"valid_at_height"` //Type string `json:"type"`
Value lbryschema.Claim `json:"value"` ValidAtHeight int `json:"valid_at_height"`
Value lbryschema.Claim `json:"value"`
} }
type ClaimListResponse struct { type ClaimListResponse []Claim /* {
Claims []Claim `json:"claims"` Claims []Claim `json:"claims"`
LastTakeoverHeight int `json:"last_takeover_height"` LastTakeoverHeight int `json:"last_takeover_height"`
SupportsWithoutClaims []Support `json:"supports_without_claims"` SupportsWithoutClaims []Support `json:"supports_without_claims"`
} }*/
type ClaimListMineResponse struct { type ClaimListMineResponse struct {
Claims []Claim `json:"items"` Claims []Claim `json:"items"`
@ -330,6 +295,7 @@ type ClaimListMineResponse struct {
PageSize uint64 `json:"page_size"` PageSize uint64 `json:"page_size"`
TotalPages uint64 `json:"total_pages"` TotalPages uint64 `json:"total_pages"`
} }
type ClaimSearchResponse ClaimListMineResponse
type StatusResponse struct { type StatusResponse struct {
BlobManager struct { BlobManager struct {