aboutsummaryrefslogtreecommitdiff
path: root/comments.go
blob: 7b3bb3d9439ba86c74beaf0f657bc8d82c42b047 (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
package devianter

import (
	"encoding/json"
	"strconv"
	"strings"
)

type comments struct {
	Cursor           string
	PrevOffset       int
	HasMore, HasLess bool

	Total  int
	Thread []struct {
		Replies, Likes int
		ID             int `json:"commentId"`
		Parent         int `json:"ParrentId"`

		Posted time
		Author bool `json:"isAuthorHighlited"`

		Desctiption string
		Comment     string

		TextContent text

		User struct {
			Username string
			Banned   bool `json:"isBanned"`
		}
	}
}

// функция для обработки комментариев поста, пользователя, группы и многого другого
func Comments(
	postid string,
	cursor string,
	page int,
	typ int, // 1 - комментарии поста; 4 - комментарии на стене группы или пользователя
) (cmmts comments) {
	for x := 0; x <= page; x++ {
		ujson(
			"dashared/comments/thread?typeid="+strconv.Itoa(typ)+
				"&itemid="+postid+"&maxdepth=1000&order=newest"+
				"&limit=50&cursor="+strings.ReplaceAll(cursor, "+", `%2B`),
			&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

			// если начало строки {, а конец }, то срабатывает этот иф
			if m[0] == 123 && m[l-1] == 125 {
				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
}