devianter/deviantion.go

159 lines
3.1 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"
"time"
2024-06-03 21:50:30 +00:00
)
// хрень для парсинга времени публикации
type timeStamp struct {
time.Time
2024-06-03 21:50:30 +00:00
}
func (t *timeStamp) UnmarshalJSON(b []byte) (err error) {
2024-06-03 21:50:30 +00:00
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))
2024-06-03 21:50:30 +00:00
return
}
// самая главная структура для поста
2024-06-13 21:05:21 +00:00
type Deviation struct {
Title, Url, License string
PublishedTime timeStamp
2024-07-13 18:40:17 +00:00
ID int `json:"deviationId"`
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-07-13 18:40:17 +00:00
OriginalFile struct {
Type string
Width int
Height int
Filesize int
}
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 timeStamp
2024-06-03 21:50:30 +00:00
Replies, Likes int
}
2024-06-13 21:05:21 +00:00
IMG, Description string
}
// преобразование урла в правильный
func UrlFromMedia(m Media, thumb ...int) string {
2024-06-13 21:05:21 +00:00
var url strings.Builder
subtractWidthHeight := func(to int, target ...*int) {
for i, l := 0, len(target); i < l; i++ {
for x := *target[i]; x > to; x -= to {
*target[i] = x
}
}
}
2024-06-13 21:05:21 +00:00
for _, t := range m.Types {
if t.T == "fullview" {
url.WriteString(m.BaseUri)
2024-08-14 16:18:40 +00:00
if l := len(m.BaseUri); l != 0 && (m.BaseUri[l-3:] != "gif" && t.W*t.H < 33177600) {
if len(thumb) != 0 {
subtractWidthHeight(thumb[0], &t.W, &t.H)
}
2024-07-13 18:40:17 +00:00
url.WriteString("/v1/fit/w_")
2024-06-13 21:05:21 +00:00
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
}
}
2024-06-13 21:05:21 +00:00
return url.String()
2024-06-03 21:50:30 +00:00
}
// для работы функции нужно ID поста и имя пользователя.
func GetDeviation(id string, user string) Post {
2024-06-13 21:05:21 +00:00
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
}