feat: add collectible animation info to data fetched from Opensea

This commit is contained in:
Dario Gabriel Lipicar 2023-03-23 14:58:57 -03:00 committed by dlipicar
parent e4f70e89b7
commit 5bad40c8be
1 changed files with 41 additions and 14 deletions

View File

@ -106,20 +106,22 @@ type SellOrder struct {
}
type Asset struct {
ID int `json:"id"`
TokenID *bigint.BigInt `json:"token_id"`
Name string `json:"name"`
Description string `json:"description"`
Permalink string `json:"permalink"`
ImageThumbnailURL string `json:"image_thumbnail_url"`
ImageURL string `json:"image_url"`
Contract Contract `json:"asset_contract"`
Collection Collection `json:"collection"`
Traits []Trait `json:"traits"`
LastSale LastSale `json:"last_sale"`
SellOrders []SellOrder `json:"sell_orders"`
BackgroundColor string `json:"background_color"`
TokenURI string `json:"token_metadata"`
ID int `json:"id"`
TokenID *bigint.BigInt `json:"token_id"`
Name string `json:"name"`
Description string `json:"description"`
Permalink string `json:"permalink"`
ImageThumbnailURL string `json:"image_thumbnail_url"`
ImageURL string `json:"image_url"`
AnimationURL string `json:"animation_url"`
AnimationMediaType string `json:"animation_media_type"`
Contract Contract `json:"asset_contract"`
Collection Collection `json:"collection"`
Traits []Trait `json:"traits"`
LastSale LastSale `json:"last_sale"`
SellOrders []SellOrder `json:"sell_orders"`
BackgroundColor string `json:"background_color"`
TokenURI string `json:"token_metadata"`
}
type CollectionTrait struct {
@ -215,6 +217,25 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) {
return nil, fmt.Errorf("unsuccessful request: %d %s", statusCode, http.StatusText(statusCode))
}
func (o *HTTPClient) doContentTypeRequest(url string) (string, error) {
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return "", err
}
resp, err := o.client.Do(req)
if err != nil {
return "", err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Error("failed to close head request body", "err", err)
}
}()
return resp.Header.Get("Content-Type"), nil
}
type Client struct {
client *HTTPClient
url string
@ -402,6 +423,12 @@ func (o *Client) fetchAssets(queryParams url.Values, limit int) (*AssetContainer
asset.Traits[i].TraitType = strings.Replace(asset.Traits[i].TraitType, "_", " ", 1)
asset.Traits[i].Value = TraitValue(strings.Title(string(asset.Traits[i].Value)))
}
if len(asset.AnimationURL) > 0 {
asset.AnimationMediaType, err = o.client.doContentTypeRequest(asset.AnimationURL)
if err != nil {
asset.AnimationURL = ""
}
}
assets.Assets = append(assets.Assets, asset)
}
assets.NextCursor = container.NextCursor