SkunkyArt/util/dtype.go

116 lines
2.3 KiB
Go

package util
import (
"strconv"
"strings"
"time"
)
type Deviation struct {
Title, Url, License string
PublishedTime Time
IsMature, IsAiGenerated, IsDailyDeviation bool
Author struct {
Username string
}
Stats struct {
Favourites, Views, Downloads int
}
Media Media
Extended struct {
Tags []struct {
Name string
}
DescriptionText struct {
Html HTML
}
RelatedContent []struct {
Deviations []Deviation
}
}
TextContent struct {
Html HTML
}
}
type Media struct {
BaseUri string
Token []string
Types []struct {
T string
H, W int
}
}
type HTML struct {
Markup, Type string
}
type Time struct {
time.Time
}
func (t *Time) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
t.Time, err = time.Parse("2006-01-02T15:04:05-0700", string(b))
return
}
// обработка изображения(ий)
func Images(deviation Deviation, media Media, nfmt bool) string {
var uri string
if len(media.BaseUri) > 0 {
uri = strings.Replace(media.BaseUri[21:], ".wixmp.com", "", 1)
}
var image string
for _, a := range media.Types {
if a.T == "fullview" {
image = uri
if len(media.Token) > 0 {
if media.BaseUri[len(media.BaseUri)-3:] == "gif" {
image = uri + "?t=" + media.Token[0]
} else {
image = uri + "/v1/fill/w_" + strconv.Itoa(a.W) + ",h_" + strconv.Itoa(a.H) + "/" + ".gif" + "?t=" + media.Token[0]
}
}
}
}
if nfmt {
return "/image/" + image
}
var link string
if deviation.Url != "" {
Url := deviation.Url[27:]
link = strings.ReplaceAll(Url, Url[0:strings.Index(Url, "/")], "")
}
if deviation.Author.Username != "" {
deviation.Author.Username = deviation.Author.Username + " — "
}
if image != "" {
image = "<img src=\"/image/" + image + "\" width=\"15%\">"
} else {
image = "<h1>[ TEXT ]</h1>"
}
switch {
case deviation.IsAiGenerated:
deviation.Title += " [🤖]"
case deviation.IsDailyDeviation:
deviation.Title += " [<span class=\"dd\">DD</span>]"
case deviation.IsMature:
if !Conf.Nsfw {
return ""
}
deviation.Title += " [<span class=\"nsfw\">NSFW</span>]"
}
return "<div class=\"block\">" + image + "<br><a href=\"/post" + link + "\">" + deviation.Author.Username + deviation.Title + "</a></div>"
}