From 48667f7af813ef7a412d367149b3f74b8b862478 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 1 May 2017 16:35:33 +0900 Subject: [PATCH] add xsearch command --- cmd/mstdn/cmd_xsearch.go | 34 ++++++++++++++++++++++++++++++++++ cmd/mstdn/main.go | 5 +++++ 2 files changed, 39 insertions(+) create mode 100644 cmd/mstdn/cmd_xsearch.go diff --git a/cmd/mstdn/cmd_xsearch.go b/cmd/mstdn/cmd_xsearch.go new file mode 100644 index 0000000..1544d9f --- /dev/null +++ b/cmd/mstdn/cmd_xsearch.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "net/url" + + "github.com/PuerkitoBio/goquery" + "github.com/urfave/cli" +) + +func cmdXSearch(c *cli.Context) error { + u, err := url.Parse("http://mastodonsearch.jp/cross/") + if err != nil { + return err + } + params := url.Values{} + params.Set("q", c.Args().First()) + u.RawQuery = params.Encode() + doc, err := goquery.NewDocument(u.String()) + if err != nil { + return err + } + doc.Find(".post").Each(func(n int, elem *goquery.Selection) { + href, ok := elem.Find(".mst_content a").Attr("href") + if !ok { + return + } + text := elem.Find(".mst_content p").Text() + fmt.Println(href) + fmt.Println(text) + fmt.Println() + }) + return nil +} diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index 4822f02..a53c7f7 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -285,6 +285,11 @@ func makeApp() *cli.App { Usage: "search mikami", Action: cmdMikami, }, + { + Name: "xsearch", + Usage: "cross search", + Action: cmdXSearch, + }, } app.Setup() return app