172 lines
4.3 KiB
Go
172 lines
4.3 KiB
Go
package task
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"mt"
|
|
"fmt"
|
|
"jccommon"
|
|
"main/constant"
|
|
//"math/rand"
|
|
"strings"
|
|
"net/http"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type nftInfo struct {
|
|
netId int32
|
|
contractAddress string
|
|
tokenId string
|
|
apiMeta *apiNftMeta
|
|
nftMeta *nftMeta
|
|
}
|
|
|
|
type apiNftMeta struct {
|
|
TokenId string `json:"token_id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Image string `json:"image"`
|
|
//ExternalUrl string `json:"external_url"`
|
|
//AnimationUrl string `json:"animation_url"`
|
|
//YoutubeUrl string `json:"youtube_url"`
|
|
Attributes []struct {
|
|
TraitType string `json:"trait_type"`
|
|
Value interface{} `json:"value"`
|
|
} `json:"attributes"`
|
|
}
|
|
|
|
type nftMeta struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Image string `json:"image"`
|
|
ExternalUrl string `json:"external_url"`
|
|
AnimationUrl string `json:"animation_url"`
|
|
YoutubeUrl string `json:"youtube_url"`
|
|
TokenId string `json:"token_id"`
|
|
Attributes []struct {
|
|
TraitType string `json:"trait_type"`
|
|
Value interface{} `json:"value"`
|
|
} `json:"attributes"`
|
|
}
|
|
|
|
type refreshMeta struct {
|
|
batchNfts []*nftInfo
|
|
}
|
|
|
|
func (this* refreshMeta) init() {
|
|
this.batchNfts = []*nftInfo{}
|
|
go this.process()
|
|
}
|
|
|
|
func (this* refreshMeta) unInit() {
|
|
}
|
|
|
|
func (this* refreshMeta) process() {
|
|
f5.GetGoStyleDb().LoopLoad(
|
|
constant.BCNFT_DB,
|
|
"refreshMeta",
|
|
"t_nft",
|
|
func () int64 {
|
|
return 30
|
|
},
|
|
func (lastIdx int64) string {
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_nft WHERE idx > %d AND last_refresh_meta_time = 0 LIMIT 200`,
|
|
lastIdx,
|
|
)
|
|
return sql
|
|
},
|
|
[]string{
|
|
},
|
|
func () int64 {
|
|
this.doRefresh()
|
|
this.batchNfts = []*nftInfo{}
|
|
return 3
|
|
},
|
|
func () int64 {
|
|
this.doRefresh()
|
|
this.batchNfts = []*nftInfo{}
|
|
return 60 + 10
|
|
},
|
|
this.repairHeroMeta)
|
|
}
|
|
|
|
func (this* refreshMeta) repairHeroMeta(ds *f5.DataSet) bool {
|
|
f5.GetSysLog().Info("refresh meta %s %s %s",
|
|
ds.GetByName("net_id"),
|
|
ds.GetByName("contract_address"),
|
|
ds.GetByName("token_id"))
|
|
nft := new(nftInfo)
|
|
nft.netId = q5.ToInt32(ds.GetByName("net_id"))
|
|
nft.contractAddress = ds.GetByName("contract_address")
|
|
nft.tokenId = ds.GetByName("token_id")
|
|
if nft.tokenId == "6240619010000001" {
|
|
q5.AppendSlice(&this.batchNfts, nft)
|
|
}
|
|
return true
|
|
}
|
|
|
|
//https://api.sandbox.immutable.com/v1/chains/{chain_name}/collections/{contract_address}/nfts/refresh-metadata
|
|
func (this* refreshMeta) doRefresh() {
|
|
nftMetas := []*nftMeta{}
|
|
for _, v := range this.batchNfts {
|
|
f5.GetHttpCliMgr().SendGoStyleRequest(
|
|
mt.Table.Config.GetById(0).GetGameapiUrl() + "/webapp/index.php",
|
|
map[string]string{
|
|
"c": "OutAppNft",
|
|
"a": "nftMetaView",
|
|
"net_id": q5.ToString(v.netId),
|
|
"token_type": q5.ToString(jccommon.NFT_TYPE_CFHERO),
|
|
"token_id": v.tokenId,
|
|
},
|
|
func (rsp f5.HttpCliResponse) {
|
|
if rsp.GetErr() != nil {
|
|
f5.GetSysLog().Info("%s", rsp.GetErr())
|
|
return
|
|
}
|
|
f5.GetSysLog().Info("token_id: %s %s", v.tokenId, rsp.GetRawData())
|
|
var apiNft apiNftMeta
|
|
if q5.DecodeJson(rsp.GetRawData(), &apiNft) == nil {
|
|
if (apiNft.TokenId == v.tokenId) {
|
|
v.apiMeta = &apiNft
|
|
v.nftMeta = new(nftMeta)
|
|
v.nftMeta.Name = apiNft.Name
|
|
v.nftMeta.Description = apiNft.Description
|
|
v.nftMeta.Image = apiNft.Image
|
|
v.nftMeta.TokenId = apiNft.TokenId
|
|
v.nftMeta.Attributes = apiNft.Attributes
|
|
q5.AppendSlice(&nftMetas, v.nftMeta)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
if len(nftMetas) > 0 {
|
|
data := struct {
|
|
NftMetaData []*nftMeta `json:"nft_metadata"`
|
|
}{}
|
|
url := fmt.Sprintf("%s/v1/chains/%s/collections/%s/nfts/refresh-metadata",
|
|
mt.Table.Config.GetById(0).GetImmutableBaseUrl(),
|
|
mt.Table.Config.GetById(0).GetChainName(),
|
|
mt.Table.Config.GetById(0).GetHeroContractAddress())
|
|
data.NftMetaData = nftMetas
|
|
req, _ := http.NewRequest("POST",
|
|
url,
|
|
strings.NewReader(q5.EncodeJson(data)))
|
|
req.Header.Set("x-immutable-api-key", mt.Table.Config.GetById(0).GetImmutableApiKey())
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
resp, err := (&http.Client{}).Do(req)
|
|
if err != nil {
|
|
fmt.Printf("http error %s", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
|
|
fmt.Printf("http ok status_code:%d %s",resp.StatusCode, string(bytes))
|
|
if resp.StatusCode == 202 {
|
|
fmt.Printf("1111111111111111111111111111111111")
|
|
}
|
|
} else {
|
|
fmt.Printf("http error read %s", err)
|
|
}
|
|
}
|
|
}
|