diff --git a/getNewPosts.go b/getNewPosts.go index 955fa55..5f6a6a8 100644 --- a/getNewPosts.go +++ b/getNewPosts.go @@ -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 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 }