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"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cmdXSearch(c *cli.Context) error {
|
2017-05-02 15:36:06 +02:00
|
|
|
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)
|
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
|
|
|
|
}
|