diff --git a/cmd/ptt_cli/main.go b/cmd/ptt_cli/main.go index 176ec3f..b3b234f 100644 --- a/cmd/ptt_cli/main.go +++ b/cmd/ptt_cli/main.go @@ -44,7 +44,7 @@ func main() { Run: func(cmd *cobra.Command, args []string) { page := 0 pagePostCount := 0 - pagePostCount = ptt.ParsePttPageByIndex(page) + pagePostCount = ptt.ParsePttPageByIndex(page, true) printPageResult(ptt, pagePostCount) scanner := bufio.NewScanner(os.Stdin) @@ -67,17 +67,17 @@ func main() { quit = true case "n": page = page + 1 - pagePostCount = ptt.ParsePttPageByIndex(page) + pagePostCount = ptt.ParsePttPageByIndex(page, true) printPageResult(ptt, pagePostCount) case "p": if page > 0 { page = page - 1 } - pagePostCount = ptt.ParsePttPageByIndex(page) + pagePostCount = ptt.ParsePttPageByIndex(page, true) printPageResult(ptt, pagePostCount) case "s": page = 0 - pagePostCount = ptt.ParsePttPageByIndex(page) + pagePostCount = ptt.ParsePttPageByIndex(page, true) printPageResult(ptt, pagePostCount) case "o": open.Run(filepath.FromSlash(ptt.BaseDir)) diff --git a/ptt.go b/ptt.go index 2bce349..b978a64 100644 --- a/ptt.go +++ b/ptt.go @@ -179,8 +179,14 @@ func (p *PTT) GetPostStarByIndex(postIndex int) int { return p.storedPost[postIndex].Likeint } +//Set Ptt board psot number, fetch assigned number of posts. +func (p *PTT) ParsePttByNumber(num int) { + + return +} + //Set Ptt board page index, fetch all post and return article count back -func (p *PTT) ParsePttPageByIndex(page int) int { +func (p *PTT) ParsePttPageByIndex(page int, replace bool) int { // Get https response with setting cookie over18=1 resp := getResponseWithCookie(p.entryAddress) doc, err := goquery.NewDocumentFromResponse(resp) @@ -234,6 +240,11 @@ func (p *PTT) ParsePttPageByIndex(page int) int { posts = append(posts, newPost) } }) + if replace { + p.storedPost = posts + } else { + p.storedPost = append(p.storedPost, posts...) + } return len(p.storedPost) } diff --git a/ptt_test.go b/ptt_test.go index 4b868cc..a0d1c11 100644 --- a/ptt_test.go +++ b/ptt_test.go @@ -2,6 +2,14 @@ package photomgr import "testing" +func TestGetPage(t *testing.T) { + ptt := NewPTT() + count := ptt.ParsePttPageByIndex(0, true) + if count == 0 { + t.Errorf("ParsePttPageByIndex: no content") + } +} + func TestURLPhoto(t *testing.T) { ptt := NewPTT() title := ptt.GetPostTitleByIndex(5) @@ -36,7 +44,7 @@ func TestURLLike(t *testing.T) { func TestUAllGirls(t *testing.T) { ptt := NewPTT() - count := ptt.ParsePttPageByIndex(0) + count := ptt.ParsePttPageByIndex(0, true) for i := 0; i < count; i++ { title := ptt.GetPostTitleByIndex(i) if CheckTitleWithBeauty(title) {