go-mastodon/cmd/mstdn/cmd_xsearch.go

39 lines
802 B
Go
Raw Normal View History

2017-05-01 09:35:33 +02:00
package main
import (
"fmt"
2017-05-02 15:36:06 +02:00
"io"
2017-05-01 09:35:33 +02:00
"net/url"
"github.com/PuerkitoBio/goquery"
2022-11-27 05:24:42 +01:00
"github.com/urfave/cli/v2"
2017-05-01 09:35:33 +02:00
)
func cmdXSearch(c *cli.Context) error {
2017-05-02 19:29:15 +02:00
return xSearch(c.App.Metadata["xsearch_url"].(string), c.Args().First(), c.App.Writer)
2017-05-02 15:36:06 +02:00
}
2017-05-02 19:29:15 +02:00
func xSearch(xsearchRawurl, query string, w io.Writer) error {
2017-05-02 15:36:06 +02:00
u, err := url.Parse(xsearchRawurl)
2017-05-01 09:35:33 +02:00
if err != nil {
return err
}
params := url.Values{}
2017-05-02 15:36:06 +02:00
params.Set("q", query)
2017-05-01 09:35:33 +02:00
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()
2017-05-02 15:36:06 +02:00
fmt.Fprintf(w, "%s\n", href)
fmt.Fprintf(w, "%s\n\n", text)
2017-05-01 09:35:33 +02:00
})
return nil
}