1 package main 2 3 import ( 4 "net/http" 5 "strings" 6 7 "vimagination.zapto.org/errors" 8 xmlpath "gopkg.in/xmlpath.v2" 9 ) 10 11 type Prefixed struct { 12 Grabber 13 Prefix string 14 Length int 15 } 16 17 func (p Prefixed) Grab(r http.Request) string { 18 source := p.Grabber.Grab(r) 19 index := strings.Index(source, p.Prefix) 20 if index < 0 || len(source) < index+len(p.Prefix)+p.Length { 21 panic(errors.Error("failed to get prefixed data")) 22 } 23 return source[index+len(p.Prefix) : index+len(p.Prefix)+p.Length] 24 } 25 26 type XPath struct { 27 Grabber 28 Path string 29 } 30 31 func (x XPath) Grab(r http.Request) string { 32 p := xmlpath.MustCompile(x.Path) 33 source := x.Grabber.Grab(r) 34 h, err := xmlpath.ParseHTML(strings.NewReader(source)) 35 e(err) 36 str, ok := p.String(h) 37 if !ok { 38 panic(errors.Error("failed to get xpath data")) 39 } 40 return str 41 }