SkunkyArt/util/dtype.go

176 lines
3.6 KiB
Go

package util
import (
"encoding/json"
"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 tx
RelatedContent []struct {
Deviations []Deviation
}
}
TextContent tx
}
type tx 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 {
if image == "" {
return "no"
}
return Conf.Base_uri + "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=\"" + Conf.Base_uri + "image/" + image + "\" width=\"15%\">"
} else {
image = "<h1>[ TEXT ]</h1>"
}
switch {
case deviation.IsMature:
if !Conf.Nsfw {
return ""
}
deviation.Title += " [<span class=\"nsfw\">NSFW</span>]"
case deviation.IsAiGenerated:
deviation.Title += " [🤖]"
case deviation.IsDailyDeviation:
deviation.Title += " [<span class=\"dd\">DD</span>]"
}
return "<div class=\"block\">" + image + "<br><a href=\"" + Conf.Base_uri + "post" + link + "\">" + deviation.Author.Username + deviation.Title + "</a></div>"
}
func ParseDescription(tx tx) string {
var description string
ds := tx.Html.Markup
if len(ds) > 0 {
if ds[0:1] == "{" {
var j2 struct {
Blocks []struct {
Text string
}
}
json.Unmarshal([]byte(ds), &j2)
for _, a := range j2.Blocks {
if ds == "draft" {
ds += a.Text + "<br>"
} else {
ds = a.Text
}
}
}
description = Parse(ds)
if len(description) > 1000 {
tds := description[:1000]
description = "<details><summary>" + tds + "</summary>" + tds[1000:] + "</details>"
}
return description
}
return "No description."
}
func BaseList(template, args, uri string, results []Deviation, page int, folders ...string) string {
var tmp struct {
Results, Pages, Folders string
Total int
}
for _, a := range folders {
tmp.Folders = a
}
for _, a := range results {
tmp.Results += Images(a, a.Media, false)
}
if page > 0 {
tmp.Pages += "<a href=\"" + Conf.Base_uri + uri + "?p=" + strconv.Itoa(page-1) + args + "\"><-- Back</a> "
}
tmp.Pages += "<a href=\"" + Conf.Base_uri + uri + "?p=" + strconv.Itoa(page+1) + args + "\">Next --></a>"
return TmpExec(template, &tmp)
}