From 6d46052ae17d41fc1e74b18afdfec5491abd485a Mon Sep 17 00:00:00 2001 From: Abdelkarim Bengrine Date: Sun, 5 Jun 2022 18:07:44 +0200 Subject: [PATCH] add: get boardMaxid / boards post handling --- getNewPosts.go | 15 ++------------- setup.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/getNewPosts.go b/getNewPosts.go index 64dc143..68784b1 100644 --- a/getNewPosts.go +++ b/getNewPosts.go @@ -113,29 +113,18 @@ 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 + // var newId []int for _, i := range newIdString { j, err := strconv.Atoi(i[1]) if err != nil { panic(err) } - newId = append(newId, j) - } - if maxId[board] != 0 { - if maxId[board] < newId[len(newId)-1] { + if j > maxId[board] { return parseNewPosts(BoardPosts, maxId[board], board), nil } - } else { - if len(newId) > 0 { - fmt.Println("first setup for this board:", board) - maxId[board] = newId[len(newId)-1] - } else { - fmt.Println("Empty board:", board) - } } return nil, nil } diff --git a/setup.go b/setup.go index f4c9978..00836c1 100644 --- a/setup.go +++ b/setup.go @@ -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 @@ -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 }