DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

[Learning Notes] [Golang] Started Modifying LINE PTT Query Bot

title: [Learning Notes][Golang] Itchy Hands Start Modifying LINE PTT Query Bot
published: false
date: 2021-11-01 00:00:00 UTC
tags: 
canonical_url: http://www.evanlin.com/go-ptt-bot/
Enter fullscreen mode Exit fullscreen mode

screen1.jpg

Preface:

Found a LINE Bot that uses Go to search PTT:

https://github.com/mong0520/linebot-ptt-beauty

I thought it was very interesting at first, but then I found that the data was all using MongoDB. With itchy hands, I decided to fix it completely.

Modified Repo:

https://github.com/kkdai/linebot-ptt-beauty

Several features:

  • Real-time data retrieval from PTT
  • Real-time retrieval of the latest images from PTT
  • Can find Post Like and DisLike

Retrieving PTT Like/DisLike via GoQuery

Mainly through the function developed by https://github.com/kkdai/photomgr, the main idea is as follows:

  • Use .push-tag to find all "推" (Like) and "噓" (Dislike)
  • Use Text() to determine whether it's a like or a dislike.

The complete code is as follows:

func (p *PTT) GetPostLikeDis(target string) (int, int) {
    // Get https response with setting cookie over18=1
    resp := getResponseWithCookie(target)
    doc, err := goquery.NewDocumentFromResponse(resp)
    if err != nil {
        log.Println(err)
        return 0, 0
    }

    var likeCount int
    var disLikeCount int
    doc.Find(".push-tag").Each(func(i int, s *goquery.Selection) {
        if strings.Contains(s.Text(), "推") {
            likeCount++
        } else if strings.Contains(s.Text(), "噓") {
            disLikeCount++
        }
    })
    // fmt.Println("like:", likeCount, " dislike:", disLikeCount)
    return likeCount, disLikeCount
}

Enter fullscreen mode Exit fullscreen mode

Those interested can take a look at this PR https://github.com/kkdai/photomgr/pull/10

A little rambling:

Whenever there's a big project or a large-scale lecture, I always feel stressed. Writing code at this time is really a relaxing behavior.

Top comments (0)