Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: get boardMaxid / boards post handling #4

Merged
merged 2 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions getNewPosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,17 @@ func getNewPosts(board string) ([]*embed.Embed, error) {
if err != nil {
return nil, err
}
re := regexp.MustCompile("\\bpostid=([0-9]+)")
var newIdString = re.FindAllStringSubmatch(BoardPosts, -1)
// var newId []int

if maxId[board] != 0 {
return parseNewPosts(BoardPosts, board), nil
} else {
re := regexp.MustCompile("\\bpostid=([0-9]+)")
var newIdString = re.FindAllStringSubmatch(BoardPosts, -1)
var newId []int

var basicMaxId int
for _, i := range newIdString {
j, err := strconv.Atoi(i[1])
if err != nil {
panic(err)
}
if j > basicMaxId {
basicMaxId = j
}
for _, i := range newIdString {
j, err := strconv.Atoi(i[1])
if err != nil {
panic(err)
}
if len(newId) > 0 {
fmt.Println("first setup for this board:", board)
maxId[board] = basicMaxId
} else {
fmt.Println("Empty board:", board)
if j > maxId[board] {
return parseNewPosts(BoardPosts, board), nil
}
}
return nil, nil
Expand Down
18 changes: 18 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ func init() {
flag.Parse()
}

func getHighestId(board string) int {
post, err := getBoardsPosts(board)
if err != nil {
panic(err)
}
re := regexp.MustCompile("\\bpostid=([0-9]+)")
match := re.FindAllStringSubmatch(post, -1)
var highestId int
for _, postId := range match {
id, _ := strconv.Atoi(postId[1])
if highestId < id {
highestId = id
}
}
return highestId
}

func setup(Boards []string) error {
if Seconde == 0 {
Seconde = 5
Expand All @@ -68,6 +85,7 @@ func setup(Boards []string) error {
if match != nil {
return fmt.Errorf("%s", string(res.Data))
}
maxId[board] = getHighestId(board)
}
return nil
}