some diags to assist in hoot troubles

This commit is contained in:
Ted Unangst 2019-07-17 23:11:48 -04:00
parent 4232c63865
commit 99b4d9f521
1 changed files with 16 additions and 11 deletions

27
hoot.go
View File

@ -2,8 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strings" "strings"
@ -16,7 +18,7 @@ var tweetsel = cascadia.MustCompile("p.tweet-text")
var linksel = cascadia.MustCompile(".time a.tweet-timestamp") var linksel = cascadia.MustCompile(".time a.tweet-timestamp")
var authorregex = regexp.MustCompile("twitter.com/([^/]+)") var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
func hootfixer(hoot string) string { func hootfetcher(hoot string) string {
url := hoot[5:] url := hoot[5:]
if url[0] == ' ' { if url[0] == ' ' {
url = url[1:] url = url[1:]
@ -28,7 +30,7 @@ func hootfixer(hoot string) string {
log.Printf("error: %s", err) log.Printf("error: %s", err)
return hoot return hoot
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; CrOS x86_64 11021.56.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.76 Safari/537.36") req.Header.Set("User-Agent", "OpenBSD ftp")
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
req.Header.Set("Accept-Language", "en-US,en;q=0.9") req.Header.Set("Accept-Language", "en-US,en;q=0.9")
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
@ -41,16 +43,16 @@ func hootfixer(hoot string) string {
log.Printf("error getting %s: %d", url, resp.StatusCode) log.Printf("error getting %s: %d", url, resp.StatusCode)
return hoot return hoot
} }
ld, _ := os.Create("lasthoot.html")
r := io.TeeReader(resp.Body, ld)
return hootfixer(r, url)
}
root, _ := html.Parse(resp.Body) func hootfixer(r io.Reader, url string) string {
root, _ := html.Parse(r)
divs := tweetsel.MatchAll(root) divs := tweetsel.MatchAll(root)
authormatch := authorregex.FindStringSubmatch(url) wanted := ""
if len(authormatch) < 2 {
log.Printf("no author")
return hoot
}
wanted := authormatch[1]
var buf strings.Builder var buf strings.Builder
fmt.Fprintf(&buf, "hoot: %s\n", url) fmt.Fprintf(&buf, "hoot: %s\n", url)
@ -62,12 +64,15 @@ func hootfixer(hoot string) string {
continue continue
} }
link := "https://twitter.com" + htfilter.GetAttr(alink, "href") link := "https://twitter.com" + htfilter.GetAttr(alink, "href")
authormatch = authorregex.FindStringSubmatch(link) authormatch := authorregex.FindStringSubmatch(link)
if len(authormatch) < 2 { if len(authormatch) < 2 {
log.Printf("no author?") log.Printf("no author?")
continue continue
} }
author := authormatch[1] author := authormatch[1]
if wanted == "" {
wanted = author
}
if author != wanted { if author != wanted {
continue continue
} }
@ -83,5 +88,5 @@ func hootfixer(hoot string) string {
var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`) var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
func hooterize(noise string) string { func hooterize(noise string) string {
return re_hoots.ReplaceAllStringFunc(noise, hootfixer) return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
} }