Minor improvements

This commit is contained in:
Alex Yatskov 2016-01-08 20:03:40 +09:00
parent 9e87770acc
commit 51d16713f3

View File

@ -24,7 +24,9 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"flag" "flag"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -43,15 +45,26 @@ func scrape(url, css, attr string) (string, error) {
} }
sel := doc.Find(css) sel := doc.Find(css)
if sel.Length() == 0 {
return "", fmt.Errorf("no selection for '%s'", css)
}
var res string var res string
if len(attr) == 0 { if len(attr) == 0 {
res = sel.Text() res = sel.Text()
} else { } else {
res, _ = sel.Attr(attr) var exists bool
if res, exists = sel.Attr(attr); !exists {
return "", fmt.Errorf("attribute '%s' not found", attr)
}
} }
return strings.TrimSpace(res), nil res = strings.TrimSpace(res)
if len(res) == 0 {
return "", errors.New("extracted empty string")
}
return res, nil
} }
func download(url string, w io.Writer) error { func download(url string, w io.Writer) error {
@ -77,28 +90,40 @@ func export(path string, r io.Reader) error {
} }
func main() { func main() {
var (
attr = flag.String("attr", "", "attribute to query")
verbose = flag.Bool("verbose", false, "verbose output")
)
flag.Parse()
if flag.NArg() < 2 { if flag.NArg() < 2 {
flag.Usage() flag.Usage()
os.Exit(2) os.Exit(2)
} }
var ( var (
attr = flag.String("attr", "", "attrerty to query") baseRaw = flag.Arg(0)
rawBase = flag.Arg(1) css = flag.Arg(1)
css = flag.Arg(2)
) )
base, err := url.Parse(rawBase) base, err := url.Parse(baseRaw)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
resRaw, err := scrape(rawBase, css, *attr) if *verbose {
log.Printf("scraping page '%s'", baseRaw)
}
resRaw, err := scrape(baseRaw, css, *attr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Print(resRaw) if *verbose {
log.Printf("extracted string '%s'", resRaw)
}
res, err := url.Parse(resRaw) res, err := url.Parse(resRaw)
if err != nil { if err != nil {
@ -109,6 +134,10 @@ func main() {
res = res.ResolveReference(base) res = res.ResolveReference(base)
} }
if *verbose {
log.Printf("downloading file '%s'", res.String())
}
var buff bytes.Buffer var buff bytes.Buffer
if err := download(res.String(), &buff); err != nil { if err := download(res.String(), &buff); err != nil {
log.Fatal(err) log.Fatal(err)
@ -116,11 +145,15 @@ func main() {
var path string var path string
if flag.NArg() > 2 { if flag.NArg() > 2 {
path = flag.Arg(3) path = flag.Arg(2)
} else { } else {
path = filepath.Base(res.Path) path = filepath.Base(res.Path)
} }
if *verbose {
log.Printf("writing file '%s'", path)
}
if err := export(path, &buff); err != nil { if err := export(path, &buff); err != nil {
log.Fatal(err) log.Fatal(err)
} }