krz/devianter

A DeviantArt guest API library for Go.

clone: git clone https://gitbay.org/krz/devianter.git

cec788554afe20b8c67357ede087b3c5c543be89

unsigned

author: lost+skunk <skunky@macaw.me> · 2024-06-14T17:05:21Z

Daily Deviations
 comments.go   |  2 +-
 deviantion.go |  6 ++++--
 misc.go       | 64 +++++++++++++++++++++++++++++++++--------------------------
 3 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/comments.go b/comments.go
index a78ab1a..e8b58e2 100644
--- a/comments.go
+++ b/comments.go
@@ -15,7 +15,7 @@ type Comments struct {
 	Thread []struct {
 		Replies, Likes int
 		ID             int `json:"commentId"`
-		Parent         int `json:"ParrentId"`
+		Parent         int `json:"parentId"`
 
 		Posted time
 		Author bool `json:"isAuthorHighlited"`
diff --git a/deviantion.go b/deviantion.go
index 9e17da2..19405a4 100644
--- a/deviantion.go
+++ b/deviantion.go
@@ -97,8 +97,10 @@ func UrlFromMedia(m media) string {
 				url.WriteString("image")
 				url.WriteString(".gif")
 			}
-			url.WriteString("?token=")
-			url.WriteString(m.Token[0])
+			if len(m.Token) > 0 {
+				url.WriteString("?token=")
+				url.WriteString(m.Token[0])
+			}
 		}
 	}
 	return url.String()
diff --git a/misc.go b/misc.go
index 5976e80..35251f3 100644
--- a/misc.go
+++ b/misc.go
@@ -115,10 +115,27 @@ func AEmedia(name string, t rune) (string, error) {
 	return "", errors.New("User not exists")
 }
 
+/* DAILY DEVIATIONS */
+type DailyDeviations struct {
+	HasMore bool
+	Strips  []struct {
+		Codename, Title string
+		TitleType       string
+		Deviations      []Deviation
+	}
+	Deviations []Deviation
+}
+
+func DailyDeviationsFunc(page int) (dd DailyDeviations) {
+	ujson("dabrowse/networkbar/rfy/deviations?page="+strconv.Itoa(page), &dd)
+	return
+}
+
 /* SEARCH */
 type Search struct {
-	Total   int         `json:"estTotal"`
-	Pages   int         // only for 'a' and 'g' scope.
+	Total   int `json:"estTotal"`
+	Pages   int // only for 'a' and 'g' scope.
+	HasMore bool
 	Results []Deviation `json:"deviations,results"`
 }
 
@@ -170,38 +187,29 @@ func SearchFunc(query string, page int, scope rune, user ...string) (ss Search,
 }
 
 /* PUPPY aka DeviantArt API */
-func puppy(data string) (string, error) {
-	// получение или обновление токена
-	update := func() (string, string, error) {
-		var cookie string
-		if cookie == "" {
-			req := request("https://www.deviantart.com/_puppy")
-
-			for _, content := range req.Cookies {
-				cookie = content.Raw
-			}
-		}
+// получение или обновление токена
+var cookie string
+var token string
 
-		req := request("https://www.deviantart.com", cookie)
-		if req.Status != 200 {
-			return "", "", errors.New(req.Body)
-		}
+func UpdateCSRF() error {
+	if cookie == "" {
+		req := request("https://www.deviantart.com/_puppy")
 
-		return cookie, req.Body[strings.Index(req.Body, "window.__CSRF_TOKEN__ = '")+25 : strings.Index(req.Body, "window.__XHR_LOCAL__")-3], nil
+		for _, content := range req.Cookies {
+			cookie = content.Raw
+		}
 	}
 
-	// использование токена
-	var (
-		cookie, token string
-	)
-	if cookie == "" || token == "" {
-		var e error
-		cookie, token, e = update()
-		if e != nil {
-			return "", e
-		}
+	req := request("https://www.deviantart.com", cookie)
+	if req.Status != 200 {
+		return errors.New(req.Body)
 	}
+	token = req.Body[strings.Index(req.Body, "window.__CSRF_TOKEN__ = '")+25 : strings.Index(req.Body, "window.__XHR_LOCAL__")-3]
 
+	return nil
+}
+
+func puppy(data string) (string, error) {
 	var url strings.Builder
 	url.WriteString("https://www.deviantart.com/_puppy/")
 	url.WriteString(data)