Skip to content

Commit

Permalink
Merge pull request #4 from PoCInnovation/getHighestId
Browse files Browse the repository at this point in the history
add: get boardMaxid / boards post handling
  • Loading branch information
Doozers authored Jun 5, 2022
2 parents b6a38fa + 70b74f2 commit c6bfa38
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
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
}

0 comments on commit c6bfa38

Please sign in to comment.