add xsearch command

This commit is contained in:
Yasuhiro Matsumoto 2017-05-01 16:35:33 +09:00
parent 28598eb3ab
commit 48667f7af8
2 changed files with 39 additions and 0 deletions

34
cmd/mstdn/cmd_xsearch.go Normal file
View File

@ -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
}

View File

@ -285,6 +285,11 @@ func makeApp() *cli.App {
Usage: "search mikami",
Action: cmdMikami,
},
{
Name: "xsearch",
Usage: "cross search",
Action: cmdXSearch,
},
}
app.Setup()
return app