небольшие улучшения

This commit is contained in:
lost+skunk 2024-06-14 00:05:21 +03:00
parent fd031a255b
commit 8f645d7ccb
4 changed files with 57 additions and 37 deletions

View File

@ -6,7 +6,7 @@ import (
"strings" "strings"
) )
type comments struct { type Comments struct {
Cursor string Cursor string
PrevOffset int PrevOffset int
HasMore, HasLess bool HasMore, HasLess bool
@ -33,12 +33,12 @@ type comments struct {
} }
// функция для обработки комментариев поста, пользователя, группы и многого другого // функция для обработки комментариев поста, пользователя, группы и многого другого
func Comments( func CommentsFunc(
postid string, postid string,
cursor string, cursor string,
page int, page int,
typ int, // 1 - комментарии поста; 4 - комментарии на стене группы или пользователя typ int, // 1 - комментарии поста; 4 - комментарии на стене группы или пользователя
) (cmmts comments) { ) (cmmts Comments) {
for x := 0; x <= page; x++ { for x := 0; x <= page; x++ {
ujson( ujson(
"dashared/comments/thread?typeid="+strconv.Itoa(typ)+ "dashared/comments/thread?typeid="+strconv.Itoa(typ)+
@ -55,7 +55,7 @@ func Comments(
cmmts.Thread[i].Comment = m cmmts.Thread[i].Comment = m
// если начало строки {, а конец }, то срабатывает этот иф // если начало строки {, а конец }, то срабатывает этот иф
if m[0] == 123 && m[l-1] == 125 { if m[0] == '{' && m[l-1] == '}' {
var content struct { var content struct {
Blocks []struct { Blocks []struct {
Text string Text string

View File

@ -3,6 +3,7 @@ package devianter
import ( import (
"encoding/json" "encoding/json"
"strconv" "strconv"
"strings"
timelib "time" timelib "time"
) )
@ -20,7 +21,7 @@ func (t *time) UnmarshalJSON(b []byte) (err error) {
} }
// самая главная структура для поста // самая главная структура для поста
type deviantion struct { type Deviation struct {
Title, Url, License string Title, Url, License string
PublishedTime time PublishedTime time
@ -41,7 +42,7 @@ type deviantion struct {
} }
DescriptionText text DescriptionText text
RelatedContent []struct { RelatedContent []struct {
Deviations []deviantion Deviations []Deviation
} }
} }
TextContent text TextContent text
@ -65,8 +66,8 @@ type text struct {
} }
// структура поста // структура поста
type Deviantion struct { type Post struct {
Deviation deviantion Deviation Deviation
Comments struct { Comments struct {
Total int Total int
Cursor string Cursor string
@ -78,32 +79,44 @@ type Deviantion struct {
Replies, Likes int Replies, Likes int
} }
IMG, Desctiption string IMG, Description string
}
// преобразование урла в правильный
func UrlFromMedia(m media) string {
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")
}
url.WriteString("?token=")
url.WriteString(m.Token[0])
}
}
return url.String()
} }
// для работы функции нужно ID поста и имя пользователя. // для работы функции нужно ID поста и имя пользователя.
func Deviation(id string, user string) Deviantion { func DeviationFunc(id string, user string) Post {
var st Deviantion var st Post
ujson( ujson(
"dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true", "dadeviation/init?deviationid="+id+"&username="+user+"&type=art&include_session=false&expand=deviation.related&preload=true",
&st, &st,
) )
// преобразование урла в правильный st.IMG = UrlFromMedia(st.Deviation.Media)
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 txt := st.Deviation.TextContent.Html.Markup
if len(txt) > 0 && txt[1] == 125 { if len(txt) > 0 && txt[1] == '{' {
var description struct { var description struct {
Blocks []struct { Blocks []struct {
Text string Text string
@ -116,7 +129,7 @@ func Deviation(id string, user string) Deviantion {
} }
} }
st.Desctiption = txt st.Description = txt
return st return st
} }

13
misc.go
View File

@ -116,13 +116,13 @@ func AEmedia(name string, t rune) (string, error) {
} }
/* SEARCH */ /* SEARCH */
type search struct { type Search struct {
Total int `json:"estTotal"` Total int `json:"estTotal"`
Pages int // only for 'a' and 'g' scope. Pages int // only for 'a' and 'g' scope.
Results []deviantion `json:"deviations,results"` Results []Deviation `json:"deviations,results"`
} }
func Search(query string, page int, scope rune, user ...string) (ss search, e error) { func SearchFunc(query string, page int, scope rune, user ...string) (ss Search, e error) {
var url strings.Builder var url strings.Builder
e = nil e = nil
@ -159,7 +159,8 @@ func Search(query string, page int, scope rune, user ...string) (ss search, e er
ujson(url.String(), &ss) ujson(url.String(), &ss)
// расчёт, сколько всего страниц по запросу. без токена 417 страниц - максимум // расчёт, сколько всего страниц по запросу. без токена 417 страниц - максимум
for x := 0; x < int(math.Round(float64(ss.Total/25))); x++ { totalfloat := int(math.Round(float64(ss.Total / 25)))
for x := 0; x < totalfloat; x++ {
if x <= 417 { if x <= 417 {
ss.Pages = x ss.Pages = x
} }

View File

@ -6,7 +6,7 @@ import (
) )
// структура группы или пользователя // структура группы или пользователя
type Group struct { type GRuser struct {
ErrorDescription string ErrorDescription string
Owner struct { Owner struct {
Group bool `json:"isGroup"` Group bool `json:"isGroup"`
@ -30,7 +30,7 @@ type Group struct {
} }
} }
CoverDeviation struct { CoverDeviation struct {
Deviation deviantion `json:"coverDeviation"` Deviation Deviation `json:"coverDeviation"`
} }
// группы // группы
@ -56,7 +56,7 @@ type Group struct {
Folder struct { Folder struct {
Username string Username string
Pages int `json:"totalPageCount"` Pages int `json:"totalPageCount"`
Deviations []deviantion Deviations []Deviation
} `json:"folderDeviations"` } `json:"folderDeviations"`
} }
} }
@ -72,17 +72,23 @@ type Group struct {
} }
} }
func UGroup(name string) (g Group) { type Group struct {
ujson("dauserprofile/init/about?username="+name, &g) Name string // обязательно заполнить
Content GRuser
}
// подходит как группа, так и пользователь
func (s Group) GroupFunc() (g Group) {
ujson("dauserprofile/init/about?username="+s.Name, &g)
return return
} }
// гарелея пользователя или группы // гарелея пользователя или группы
func Gallery(name string, page int) (g Group) { func (s Group) Gallery(page int) (g Group) {
var url strings.Builder var url strings.Builder
url.WriteString("dauserprofile/init/gallery?username=") url.WriteString("dauserprofile/init/gallery?username=")
url.WriteString(name) url.WriteString(s.Name)
url.WriteString("&page=") url.WriteString("&page=")
url.WriteString(strconv.Itoa(page)) url.WriteString(strconv.Itoa(page))
url.WriteString("&deviations_limit=50&with_subfolders=false") url.WriteString("&deviations_limit=50&with_subfolders=false")