From e5d11f63aa22ead16e777ab3200b1bebda30c55f Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 2 May 2017 22:36:06 +0900 Subject: [PATCH] Add xsearch --- cmd/mstdn/cmd_mikami.go | 19 +------------------ cmd/mstdn/cmd_xsearch.go | 14 +++++++++----- cmd/mstdn/main.go | 5 +++-- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/cmd/mstdn/cmd_mikami.go b/cmd/mstdn/cmd_mikami.go index ec3449e..b46cc1f 100644 --- a/cmd/mstdn/cmd_mikami.go +++ b/cmd/mstdn/cmd_mikami.go @@ -1,26 +1,9 @@ package main import ( - "fmt" - - "github.com/PuerkitoBio/goquery" "github.com/urfave/cli" ) func cmdMikami(c *cli.Context) error { - doc, err := goquery.NewDocument("http://mastodonsearch.jp/cross/?q=三上") - 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 + return xsearch(c.App.Metadata["xsearch_url"].(string), "三上", c.App.Writer) } diff --git a/cmd/mstdn/cmd_xsearch.go b/cmd/mstdn/cmd_xsearch.go index 1544d9f..92cef85 100644 --- a/cmd/mstdn/cmd_xsearch.go +++ b/cmd/mstdn/cmd_xsearch.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "net/url" "github.com/PuerkitoBio/goquery" @@ -9,12 +10,16 @@ import ( ) func cmdXSearch(c *cli.Context) error { - u, err := url.Parse("http://mastodonsearch.jp/cross/") + return xsearch(c.App.Metadata["xsearch_url"].(string), c.Args().First(), c.App.Writer) +} + +func xsearch(xsearchRawurl, query string, w io.Writer) error { + u, err := url.Parse(xsearchRawurl) if err != nil { return err } params := url.Values{} - params.Set("q", c.Args().First()) + params.Set("q", query) u.RawQuery = params.Encode() doc, err := goquery.NewDocument(u.String()) if err != nil { @@ -26,9 +31,8 @@ func cmdXSearch(c *cli.Context) error { return } text := elem.Find(".mst_content p").Text() - fmt.Println(href) - fmt.Println(text) - fmt.Println() + fmt.Fprintf(w, "%s\n", href) + fmt.Fprintf(w, "%s\n\n", text) }) return nil } diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index a53c7f7..392f748 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -361,8 +361,9 @@ func run() int { client := mastodon.NewClient(config) app.Metadata = map[string]interface{}{ - "client": client, - "config": config, + "client": client, + "config": config, + "xsearch_url": "http://mastodonsearch.jp/cross/", } if config.AccessToken == "" { return authenticate(client, config, file)