|
|
|
@ -9,6 +9,7 @@ import (
|
|
|
|
|
"log"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
@ -19,7 +20,7 @@ import (
|
|
|
|
|
type Status struct {
|
|
|
|
|
NFTNum int
|
|
|
|
|
TotalUSDValue float64
|
|
|
|
|
NFTs []string
|
|
|
|
|
NFTs []downloadedOpenSeaAsset
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const NFTS_DIR = "./nfts"
|
|
|
|
@ -64,7 +65,7 @@ func getStatus() (Status, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(status.NFTs) <= 20 {
|
|
|
|
|
status.NFTs = append(status.NFTs, id)
|
|
|
|
|
status.NFTs = append(status.NFTs, asset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status.NFTNum += 1
|
|
|
|
@ -99,33 +100,34 @@ func getRandomOpenSeaAsset() (Asset, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type downloadedOpenSeaAsset struct {
|
|
|
|
|
Id string `json:"Id"`
|
|
|
|
|
Asset `json:"Asset"`
|
|
|
|
|
Transaction `json:"Transaction"`
|
|
|
|
|
FileName string `json:"FileName"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d downloadedOpenSeaAsset) GetUSDPrice() float64 {
|
|
|
|
|
return d.Transaction.Value * d.Asset.LastSale.TokenUSDPrice
|
|
|
|
|
}
|
|
|
|
|
func (d downloadedOpenSeaAsset) IsVideo() bool {
|
|
|
|
|
return path.Ext(d.FileName) == ".mp4"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func downloadOpenSeaAsset(asset Asset, transaction Transaction) (string, error) {
|
|
|
|
|
id := getOpenSeaId(asset.TokenId)
|
|
|
|
|
jsonFile, err := os.Create(filepath.Join(NFTS_DIR, id+".json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
err = json.NewEncoder(jsonFile).Encode(downloadedOpenSeaAsset{
|
|
|
|
|
Asset: asset,
|
|
|
|
|
Transaction: transaction,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(asset.ImageUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
ext := path.Ext(u.Path)
|
|
|
|
|
filename := id + ext
|
|
|
|
|
|
|
|
|
|
imageRes, err := http.DefaultClient.Get(asset.ImageUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
file, err := os.Create(filepath.Join(NFTS_DIR, id))
|
|
|
|
|
file, err := os.Create(filepath.Join(NFTS_DIR, filename))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
@ -134,6 +136,20 @@ func downloadOpenSeaAsset(asset Asset, transaction Transaction) (string, error)
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonFile, err := os.Create(filepath.Join(NFTS_DIR, id+".json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
err = json.NewEncoder(jsonFile).Encode(downloadedOpenSeaAsset{
|
|
|
|
|
Id: id,
|
|
|
|
|
Asset: asset,
|
|
|
|
|
Transaction: transaction,
|
|
|
|
|
FileName: filename,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -144,13 +160,13 @@ func must(err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
indexTmpl, err := template.ParseFiles("./index.tmpl")
|
|
|
|
|
indexTmpl, err := template.ParseFiles("./templates/base.tmpl", "./templates/index.tmpl")
|
|
|
|
|
must(err)
|
|
|
|
|
nftsTmpl, err := template.ParseFiles("./nft.tmpl")
|
|
|
|
|
nftsTmpl, err := template.ParseFiles("./templates/base.tmpl", "./templates/nft.tmpl")
|
|
|
|
|
must(err)
|
|
|
|
|
|
|
|
|
|
fs := http.FileServer(http.Dir(NFTS_DIR))
|
|
|
|
|
http.Handle("/static/nfts/", http.StripPrefix("/static/nfts/", fs))
|
|
|
|
|
http.Handle("/static/nfts/", http.StripPrefix("/static/nfts/", http.FileServer(http.Dir(NFTS_DIR))))
|
|
|
|
|
http.Handle("/static/assets/", http.StripPrefix("/static/assets/", http.FileServer(http.Dir("./assets"))))
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
status, err := getStatus()
|
|
|
|
@ -200,13 +216,7 @@ func main() {
|
|
|
|
|
log.Panicln("No pude conseguir NFT", err)
|
|
|
|
|
}
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
err = nftsTmpl.Execute(w, struct {
|
|
|
|
|
Id string
|
|
|
|
|
NFT downloadedOpenSeaAsset
|
|
|
|
|
}{
|
|
|
|
|
Id: id,
|
|
|
|
|
NFT: nft,
|
|
|
|
|
})
|
|
|
|
|
err = nftsTmpl.Execute(w, nft)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Panicln("No pude escribir nft", err)
|
|
|
|
|
}
|
|
|
|
|