summaryrefslogtreecommitdiff
path: root/deviantion.go
blob: 98038554d08594cd84bcc391a97019771f2609b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package devianter

import (
	"encoding/json"
	"strconv"
	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
}

// самая главная структура для поста
type deviantion struct {
	Title, Url, License string
	PublishedTime       time

	NSFW bool `json:"isMature"`
	AI   bool `json:"isAiGenerated"`
	DD   bool `json:"isDailyDeviation"`

	Author struct {
		Username string
	}
	Stats struct {
		Favourites, Views, Downloads int
	}
	Media    media
	Extended struct {
		Tags []struct {
			Name string
		}
		DescriptionText text
		RelatedContent  []struct {
			Deviations []deviantion
		}
	}
	TextContent text
}

// её выпердыши
type media struct {
	BaseUri string
	Token   []string
	Types   []struct {
		T    string
		H, W int
	}
}

type text struct {
	Excerpt string
	Html    struct {
		Markup, Type string
	}
}

// структура поста
type Deviantion struct {
	Deviation deviantion
	Comments  struct {
		Total  int
		Cursor string
	}

	ParsedComments []struct {
		Author         string
		Posted         time
		Replies, Likes int
	}

	IMG, Desctiption string
}

// для работы функции нужно ID поста и имя пользователя.
func Deviation(id string, user string) Deviantion {
	var st Deviantion
	ujson(
		"dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true",
		&st,
	)

	// преобразование урла в правильный
	for _, t := range st.Deviation.Media.Types {
		if m := st.Deviation.Media; t.T == "fullview" {
			if len(m.Token) > 0 {
				st.IMG = m.BaseUri + "?token="
			} else {
				st.IMG = m.BaseUri + "/v1/fill/w_" + strconv.Itoa(t.W) + ",h_" + strconv.Itoa(t.H) + "/" + id + "_" + user + ".gif" + "?token="
			}
			st.IMG += m.Token[0]
		}
	}

	// базовая обработка описания
	txt := st.Deviation.TextContent.Html.Markup
	if len(txt) > 0 && txt[1] == 125 {
		var description struct {
			Blocks []struct {
				Text string
			}
		}

		json.Unmarshal([]byte(txt), &description)
		for _, a := range description.Blocks {
			txt = a.Text
		}
	}

	st.Desctiption = txt

	return st
}