make the hoot code amenable to testing

This commit is contained in:
Ted Unangst 2019-10-31 00:00:31 -04:00
parent 4e225e16f3
commit 48513d3613
2 changed files with 69 additions and 49 deletions

21
hoot.go
View File

@ -35,10 +35,7 @@ var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`) var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
func hooterize(noise string) string { func hootextractor(r io.Reader, url string, seen map[string]bool) string {
seen := make(map[string]bool)
hootfixer := func(r io.Reader, url string) string {
root, err := html.Parse(r) root, err := html.Parse(r)
if err != nil { if err != nil {
log.Printf("error parsing hoot: %s", err) log.Printf("error parsing hoot: %s", err)
@ -46,15 +43,15 @@ func hooterize(noise string) string {
} }
divs := tweetsel.MatchAll(root) divs := tweetsel.MatchAll(root)
var wanted string
wantmatch := authorregex.FindStringSubmatch(url) wantmatch := authorregex.FindStringSubmatch(url)
if len(wantmatch) < 2 { if len(wantmatch) == 2 {
log.Printf("no wanted author?") wanted = wantmatch[1]
} }
wanted := wantmatch[1]
var buf strings.Builder var buf strings.Builder
var htf htfilter.Filter
fmt.Fprintf(&buf, "%s\n", url) fmt.Fprintf(&buf, "%s\n", url)
var htf htfilter.Filter
for _, div := range divs { for _, div := range divs {
twp := div.Parent.Parent.Parent twp := div.Parent.Parent.Parent
alink := linksel.MatchFirst(twp) alink := linksel.MatchFirst(twp)
@ -69,6 +66,9 @@ func hooterize(noise string) string {
continue continue
} }
author := authormatch[1] author := authormatch[1]
if wanted == "" {
wanted = author
}
if author != wanted { if author != wanted {
continue continue
} }
@ -86,6 +86,9 @@ func hooterize(noise string) string {
return buf.String() return buf.String()
} }
func hooterize(noise string) string {
seen := make(map[string]bool)
hootfetcher := func(hoot string) string { hootfetcher := func(hoot string) string {
url := hoot[5:] url := hoot[5:]
if url[0] == ' ' { if url[0] == ' ' {
@ -113,7 +116,7 @@ func hooterize(noise string) string {
} }
ld, _ := os.Create("lasthoot.html") ld, _ := os.Create("lasthoot.html")
r := io.TeeReader(resp.Body, ld) r := io.TeeReader(resp.Body, ld)
return hootfixer(r, url) return hootextractor(r, url, seen)
} }
return re_hoots.ReplaceAllStringFunc(noise, hootfetcher) return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)

17
hoot_test.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"fmt"
"os"
"testing"
)
func TestHooterize(t *testing.T) {
fd, err := os.Open("lasthoot.html")
if err != nil {
return
}
seen := make(map[string]bool)
hoots := hootextractor(fd, "lasthoot.html", seen)
fmt.Printf("hoots: %s\n", hoots)
}