summaryrefslogtreecommitdiff
path: root/deviantion.go
diff options
context:
space:
mode:
Diffstat (limited to 'deviantion.go')
-rw-r--r--deviantion.go70
1 files changed, 44 insertions, 26 deletions
diff --git a/deviantion.go b/deviantion.go
index 12c7a14..5b6df39 100644
--- a/deviantion.go
+++ b/deviantion.go
@@ -1,13 +1,14 @@
package devianter
import (
- "encoding/json"
"strconv"
"strings"
"time"
)
-// хрень для парсинга времени публикации
+// timeStamp is a time.Time that parses DeviantArt's publication timestamps,
+// which are ISO 8601 with no colon in the zone offset and so are rejected by
+// encoding/json's default time handling.
type timeStamp struct {
time.Time
}
@@ -20,7 +21,14 @@ func (t *timeStamp) UnmarshalJSON(b []byte) (err error) {
return
}
-// самая главная структура для поста
+// Deviation is a single artwork and its metadata: the central type of this
+// package. Most endpoints return these, either alone or in slices.
+//
+// How much of it is populated depends on the endpoint. Search results and
+// gallery listings return a shallow Deviation — enough for a thumbnail and a
+// title — while [GetDeviation] fills in Extended, with the tags, original file
+// details, and description. A zero-valued field usually means the endpoint did
+// not send it rather than that the artwork lacks it.
type Deviation struct {
Title, Url, License string
PublishedTime timeStamp
@@ -55,17 +63,25 @@ type Deviation struct {
TextContent Text
}
-// её выпердыши
+// Media locates a deviation's image files. It is not a usable URL on its own:
+// the pieces have to be assembled, and the result signed with a token. Pass it
+// to [UrlFromMedia] rather than building the URL by hand.
type Media struct {
BaseUri string
Name string `json:"prettyName"`
Token []string
- Types []struct {
+ // Types are the renditions available (thumbnails, preview, "fullview"), each
+ // with its own dimensions.
+ Types []struct {
T string
H, W int
}
}
+// Text is a block of user-written text — a description, a comment, a group's
+// about page. Markup holds either HTML or a JSON-encoded Draft.js document,
+// distinguished by Type; the functions that return a Text generally extract the
+// plain text into a neighbouring field, which is easier to use.
type Text struct {
Excerpt string
Html struct {
@@ -73,7 +89,13 @@ type Text struct {
}
}
-// структура поста
+// Post is a deviation together with its comment metadata, as returned by
+// [GetDeviation]. IMG and Description are conveniences that GetDeviation derives
+// from the Deviation, so callers need not assemble a URL or decode Draft.js
+// markup themselves.
+//
+// Comments holds only a total and a cursor. To retrieve the comments, pass them
+// to [GetComments] with type 1.
type Post struct {
Deviation Deviation
Comments struct {
@@ -90,7 +112,14 @@ type Post struct {
IMG, Description string
}
-// преобразование урла в правильный
+// UrlFromMedia assembles a usable, token-signed image URL from a [Media], along
+// with the filename DeviantArt would serve it under. It selects the "fullview"
+// rendition and returns empty strings if the media has none.
+//
+// An optional thumb argument scales the request down towards that many pixels
+// per side, for fetching a smaller copy than the original. GIFs and very large
+// images (beyond roughly 33 megapixels) are returned at their original URL
+// without resizing, as DeviantArt's resizer refuses them.
func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename string) {
var url strings.Builder
@@ -131,7 +160,13 @@ func UrlFromMedia(m Media, thumb ...int) (urlParsed, wellFormattedFilename strin
return
}
-// для работы функции нужно ID поста и имя пользователя.
+// GetDeviation retrieves a single deviation by its numeric ID and its author's
+// username. Both are required: the endpoint will not resolve an ID alone. They
+// appear in a deviation's page URL, which ends in a slug of the form
+// title-by-author-123456789.
+//
+// The returned Post has its IMG and Description already derived, and its
+// Deviation is fully populated, including Extended.
func GetDeviation(id string, user string) (st Post, err Error) {
err = ujson(
"dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true",
@@ -140,24 +175,7 @@ func GetDeviation(id string, user string) (st Post, err Error) {
st.IMG, _ = UrlFromMedia(st.Deviation.Media)
- // базовая обработка описания
- txt := st.Deviation.TextContent.Html.Markup
- if len(txt) > 1 && txt[1] == '{' {
- var description struct {
- Blocks []struct {
- Text string
- }
- }
-
- if err := json.Unmarshal([]byte(txt), &description); err != nil {
- // Handle error appropriately
- try(err) // or log/return the error
- }
- for _, a := range description.Blocks {
- txt = a.Text
- }
- }
- st.Description = txt
+ st.Description = flattenComment(st.Deviation.TextContent.Html.Markup)
return
}