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

PTT keyword search #18

Merged
merged 2 commits into from
Feb 15, 2023
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
12 changes: 10 additions & 2 deletions cmd/ptt_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func printPageResult(p *PTT, count int) {
likeCount := p.GetPostStarByIndex(i)
fmt.Printf("%d:[%d★]%s\n", i, likeCount, title)
}
fmt.Printf("(o: open file in fider, s: top page, n:next, p:prev, quit: quit program)\n")
fmt.Printf("(o: open file in fider, s: search keyword, t: top page, n:next, p:prev, quit: quit program)\n")
}

type NullWriter int
Expand Down Expand Up @@ -75,12 +75,20 @@ func main() {
}
pagePostCount = ptt.ParsePttPageByIndex(page, true)
printPageResult(ptt, pagePostCount)
case "s":
case "t":
page = 0
pagePostCount = ptt.ParsePttPageByIndex(page, true)
printPageResult(ptt, pagePostCount)
case "o":
open.Run(filepath.FromSlash(ptt.BaseDir))
case "s":
if len(args) == 0 {
fmt.Println("You don't input any article index. Input as 's keyword'")
continue
}

pagePostCount = ptt.ParseSearchByKeyword(args[0])
printPageResult(ptt, pagePostCount)
case "d":
if len(args) == 0 {
fmt.Println("You don't input any article index. Input as 'd 1'")
Expand Down
38 changes: 35 additions & 3 deletions ptt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ type PTT struct {
baseCrawler

//Handle base folder address to store images
BaseDir string
BaseDir string
SearchAddress string
}

func NewPTT() *PTT {

p := new(PTT)
p.baseAddress = "https://www.ptt.cc"
p.entryAddress = "https://www.ptt.cc/bbs/Beauty/index.html"
p.SearchAddress = "https://www.ptt.cc/bbs/Beauty/search?q="
return p
}

Expand Down Expand Up @@ -234,7 +236,7 @@ func (p *PTT) GetPostStarByIndex(postIndex int) int {
return p.storedPost[postIndex].Likeint
}

//Set Ptt board psot number, fetch assigned (at least) number of posts. Return real number.
// Set Ptt board psot number, fetch assigned (at least) number of posts. Return real number.
func (p *PTT) ParsePttByNumber(num int, page int) int {
count := p.ParsePttPageByIndex(page, true)
if count > num {
Expand All @@ -249,7 +251,7 @@ func (p *PTT) ParsePttByNumber(num int, page int) int {
return count
}

//Set Ptt board page index, fetch all post and return article count back
// Set Ptt board page index, fetch all post and return article count back
func (p *PTT) ParsePttPageByIndex(page int, replace bool) int {
// Get https response with setting cookie over18=1
resp := getResponseWithCookie(p.entryAddress)
Expand Down Expand Up @@ -351,6 +353,36 @@ func (p *PTT) GetPostLikeDis(target string) (int, int) {
return likeCount, disLikeCount
}

// Search with specific keyword, fetch all post and return article count back
func (p *PTT) ParseSearchByKeyword(keyword string) int {
// Get https response with setting cookie over18=1
resp := getResponseWithCookie(p.SearchAddress + keyword)
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
log.Fatal(err)
}

posts := make([]PostDoc, 0)
doc.Find(".r-ent").Each(func(i int, s *goquery.Selection) {
title := strings.TrimSpace(s.Find(".title").Text())
if CheckTitleWithBeauty(title) {
likeCount, _ := strconv.Atoi(s.Find(".nrec span").Text())
href, _ := s.Find(".title a").Attr("href")
link := p.baseAddress + href
newPost := PostDoc{
ArticleID: "",
ArticleTitle: title,
URL: link,
Likeint: likeCount,
}

posts = append(posts, newPost)
}
})
p.storedPost = posts
return len(p.storedPost)
}

// CheckTitleWithBeauty: check if title contains "美女" or "美女圖" or "美女圖片" or "美女圖片"
func CheckTitleWithBeauty(title string) bool {
d, _ := regexp.MatchString("^\\[正妹\\].*", title)
Expand Down