devianter/comments.go

74 lines
1.5 KiB
Go
Raw Normal View History

2024-06-03 21:50:30 +00:00
package devianter
import (
"encoding/json"
2024-06-30 11:39:11 +00:00
"net/url"
2024-06-03 21:50:30 +00:00
"strconv"
)
2024-06-30 11:39:11 +00:00
type Thread struct {
Replies, Likes int
ID int `json:"commentId"`
Parent int `json:"parentId"`
Posted time
Author bool `json:"isAuthorHighlited"`
Desctiption string
Comment string
TextContent Text
User struct {
Username string
Banned bool `json:"isBanned"`
}
}
2024-06-13 21:05:21 +00:00
type Comments struct {
2024-06-03 21:50:30 +00:00
Cursor string
PrevOffset int
HasMore, HasLess bool
Total int
2024-06-30 11:39:11 +00:00
Thread []Thread
2024-06-03 21:50:30 +00:00
}
2024-06-30 11:39:11 +00:00
// 1 - комментарии поста; 4 - комментарии на стене группы или пользователя
func CommentsFunc(postid string, cursor string, page int, typ int) (cmmts Comments) {
2024-06-03 21:50:30 +00:00
for x := 0; x <= page; x++ {
ujson(
"dashared/comments/thread?typeid="+strconv.Itoa(typ)+
"&itemid="+postid+"&maxdepth=1000&order=newest"+
2024-06-30 11:39:11 +00:00
"&limit=50&cursor="+url.QueryEscape(cursor),
2024-06-03 21:50:30 +00:00
&cmmts,
)
cursor = cmmts.Cursor
// парсинг json внутри json
for i := 0; i < len(cmmts.Thread); i++ {
m, l := cmmts.Thread[i].TextContent.Html.Markup, len(cmmts.Thread[i].TextContent.Html.Markup)
cmmts.Thread[i].Comment = m
// если начало строки {, а конец }, то срабатывает этот иф
2024-06-13 21:05:21 +00:00
if m[0] == '{' && m[l-1] == '}' {
2024-06-03 21:50:30 +00:00
var content struct {
Blocks []struct {
Text string
}
}
e := json.Unmarshal([]byte(m), &content)
err(e)
for _, a := range content.Blocks {
cmmts.Thread[i].Comment = a.Text
}
}
}
}
return
}