generated from PoCInnovation/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getNewReplies.go
74 lines (64 loc) · 1.66 KB
/
getNewReplies.go
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
package main
import (
"fmt"
"regexp"
"strings"
)
type Reply struct {
Content string
Author string
Id int
}
func getRepliesInfos(replies string, postId int) Reply {
regAuthor := regexp.MustCompile(`\\- \[([a-zA-Z0-9_.-@]+)\]`)
regContent := regexp.MustCompile(`> ([^>\[\]]+)\n>`)
matchAuthor := regAuthor.FindStringSubmatch(replies)
matchContent := regContent.FindStringSubmatch(replies)
fmt.Println(replies)
p := Reply{
Content: matchContent[1],
Author: matchAuthor[1],
Id: postId,
}
fmt.Println(p)
return p
}
func parsePostsReplies(postReplies string) []string {
postReplies = strings.Split(postReplies, "\n\\- [@")[1]
a := strings.Split(postReplies, "\n\n")[1:]
var replies []string
for _, c := range a {
replies = append(replies, strings.Split(c, "> \n")...)
}
return replies
}
func parseNewReplies(postReplies string, post string, postTitle string, board string) []Embed {
var replies []Reply
newMaxId := maxId[post]
a := parsePostsReplies(postReplies)
for _, c := range a {
nb, _ := getID(c)
if nb > maxId[post] {
replies = append(replies, getRepliesInfos(c, nb))
if nb > newMaxId {
newMaxId = nb
}
}
}
maxId[post] = newMaxId
return embedReplies(replies, post, postTitle, board)
}
func getNewReplies(post string, board string, postTitle string) ([]Embed, error) {
// this return the reply from the post
PostReplies, err := getBoardsContents(post)
if err != nil {
return nil, err
}
ids := getMessagesIds(PostReplies, fmt.Sprintf("\\br\\/boards:%s\\/([0-9]+)", post))
for _, i := range ids {
if i > maxId[post] {
return parseNewReplies(PostReplies, post, postTitle, board), nil
}
}
return nil, nil
}