devianter/deviantion.go

138 lines
2.6 KiB
Go
Raw Normal View History

2024-06-03 21:50:30 +00:00
package devianter
import (
"encoding/json"
"strconv"
2024-06-13 21:05:21 +00:00
"strings"
2024-06-03 21:50:30 +00:00
timelib "time"
)
// хрень для парсинга времени публикации
type time struct {
timelib.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 = timelib.Parse("2006-01-02T15:04:05-0700", string(b))
return
}
// самая главная структура для поста
2024-06-13 21:05:21 +00:00
type Deviation struct {
Title, Url, License string
PublishedTime time
NSFW bool `json:"isMature"`
AI bool `json:"isAiGenerated"`
DD bool `json:"isDailyDeviation"`
Author struct {
2024-06-03 21:50:30 +00:00
Username string
}
Stats struct {
Favourites, Views, Downloads int
}
2024-06-27 11:52:33 +00:00
Media Media
2024-06-03 21:50:30 +00:00
Extended struct {
Tags []struct {
Name string
}
2024-06-27 11:52:33 +00:00
DescriptionText Text
2024-06-03 21:50:30 +00:00
RelatedContent []struct {
2024-06-13 21:05:21 +00:00
Deviations []Deviation
2024-06-03 21:50:30 +00:00
}
}
2024-06-27 11:52:33 +00:00
TextContent Text
2024-06-03 21:50:30 +00:00
}
// её выпердыши
2024-06-27 11:52:33 +00:00
type Media struct {
2024-06-03 21:50:30 +00:00
BaseUri string
Token []string
Types []struct {
T string
H, W int
}
}
2024-06-27 11:52:33 +00:00
type Text struct {
Excerpt string
Html struct {
2024-06-03 21:50:30 +00:00
Markup, Type string
}
}
// структура поста
2024-06-13 21:05:21 +00:00
type Post struct {
Deviation Deviation
2024-06-03 21:50:30 +00:00
Comments struct {
Total int
Cursor string
}
ParsedComments []struct {
Author string
Posted time
Replies, Likes int
}
2024-06-13 21:05:21 +00:00
IMG, Description string
}
// преобразование урла в правильный
2024-06-27 11:52:33 +00:00
func UrlFromMedia(m Media) string {
2024-06-13 21:05:21 +00:00
var url strings.Builder
for _, t := range m.Types {
if t.T == "fullview" {
url.WriteString(m.BaseUri)
if m.BaseUri[len(m.BaseUri)-3:] != "gif" && t.W*t.H < 33177600 {
url.WriteString("/v1/fill/w_")
url.WriteString(strconv.Itoa(t.W))
url.WriteString(",h_")
url.WriteString(strconv.Itoa(t.H))
url.WriteString("/")
url.WriteString("image")
url.WriteString(".gif")
}
2024-06-14 17:05:21 +00:00
if len(m.Token) > 0 {
url.WriteString("?token=")
url.WriteString(m.Token[0])
}
2024-06-13 21:05:21 +00:00
}
}
return url.String()
2024-06-03 21:50:30 +00:00
}
// для работы функции нужно ID поста и имя пользователя.
2024-06-13 21:05:21 +00:00
func DeviationFunc(id string, user string) Post {
var st Post
2024-06-03 21:50:30 +00:00
ujson(
"dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true",
&st,
)
2024-06-13 21:05:21 +00:00
st.IMG = UrlFromMedia(st.Deviation.Media)
2024-06-03 21:50:30 +00:00
// базовая обработка описания
txt := st.Deviation.TextContent.Html.Markup
2024-06-13 21:05:21 +00:00
if len(txt) > 0 && txt[1] == '{' {
2024-06-03 21:50:30 +00:00
var description struct {
Blocks []struct {
Text string
}
}
json.Unmarshal([]byte(txt), &description)
for _, a := range description.Blocks {
txt = a.Text
}
}
2024-06-13 21:05:21 +00:00
st.Description = txt
2024-06-03 21:50:30 +00:00
return st
}