hunter/hunter.go
Miguel de la Cruz 547643c2b6 Initial commit
2024-07-05 11:11:13 +02:00

88 lines
2 KiB
Go

package main
import (
"fmt"
"log/slog"
"os"
"strconv"
"github.com/alecthomas/kong"
"github.com/gocolly/colly/v2"
)
// ToDo: emoji in text support
// ToDo: attachment support?
// ToDo: polls support?
type Status struct {
Username string
DisplayName string
ProfileURL string
AvatarURL string
Text string
Published string
ReplyCount int
FavouriteCount int
BoostCount int
}
func main() {
ctx := kong.Parse(&cli)
if cli.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
if err := ctx.Run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
func getStatusesForURL(url string) []*Status {
statuses := []*Status{}
c := colly.NewCollector()
c.OnHTML("article.status", func(e *colly.HTMLElement) {
replyCount, _ := strconv.Atoi(e.ChildText("aside.status-info div[title=Replies] dd"))
favouriteCount, _ := strconv.Atoi(e.ChildText("aside.status-info div[title=Faves] dd"))
boostCount, _ := strconv.Atoi(e.ChildText("aside.status-info div[title=Boosts] dd"))
status := &Status{
Username: e.ChildText("address div.author-strap span.username"),
DisplayName: e.ChildText("address div.author-strap span.displayname"),
ProfileURL: e.ChildAttr("address a", "href"),
AvatarURL: e.ChildAttr("address img.avatar", "src"),
Text: e.ChildText("div.status-body div.content"),
Published: e.ChildAttr("aside.status-info div.published-at time", "datetime"),
ReplyCount: replyCount,
FavouriteCount: favouriteCount,
BoostCount: boostCount,
}
slog.Debug("Status", "status", status)
statuses = append(statuses, status)
})
c.Visit(url)
return statuses
}
func (u *statusesCmd) Run() error {
statuses := getStatusesForURL(u.URL)
for _, s := range statuses {
txt := s.Text
if len(txt) > 30 {
txt = txt[:27] + "..."
}
slog.Info("Status", "username", s.Username, "display name", s.DisplayName, "text", txt)
}
return nil
}
func (p *parseCmd) Run() error {
return nil
}