Add xsearch

This commit is contained in:
178inaba 2017-05-02 22:36:06 +09:00
parent 759ce4a331
commit e5d11f63aa
3 changed files with 13 additions and 25 deletions

View File

@ -1,26 +1,9 @@
package main package main
import ( import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
func cmdMikami(c *cli.Context) error { func cmdMikami(c *cli.Context) error {
doc, err := goquery.NewDocument("http://mastodonsearch.jp/cross/?q=三上") return xsearch(c.App.Metadata["xsearch_url"].(string), "三上", c.App.Writer)
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

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io"
"net/url" "net/url"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -9,12 +10,16 @@ import (
) )
func cmdXSearch(c *cli.Context) error { 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 { if err != nil {
return err return err
} }
params := url.Values{} params := url.Values{}
params.Set("q", c.Args().First()) params.Set("q", query)
u.RawQuery = params.Encode() u.RawQuery = params.Encode()
doc, err := goquery.NewDocument(u.String()) doc, err := goquery.NewDocument(u.String())
if err != nil { if err != nil {
@ -26,9 +31,8 @@ func cmdXSearch(c *cli.Context) error {
return return
} }
text := elem.Find(".mst_content p").Text() text := elem.Find(".mst_content p").Text()
fmt.Println(href) fmt.Fprintf(w, "%s\n", href)
fmt.Println(text) fmt.Fprintf(w, "%s\n\n", text)
fmt.Println()
}) })
return nil return nil
} }

View File

@ -361,8 +361,9 @@ func run() int {
client := mastodon.NewClient(config) client := mastodon.NewClient(config)
app.Metadata = map[string]interface{}{ app.Metadata = map[string]interface{}{
"client": client, "client": client,
"config": config, "config": config,
"xsearch_url": "http://mastodonsearch.jp/cross/",
} }
if config.AccessToken == "" { if config.AccessToken == "" {
return authenticate(client, config, file) return authenticate(client, config, file)