all repos — honk @ 46df8b5ced26c13c8e746f3812d05b7b516126a7

my fork of honk

hoot.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"log"
 6	"net/http"
 7	"regexp"
 8	"strings"
 9
10	"github.com/andybalholm/cascadia"
11	"golang.org/x/net/html"
12	"humungus.tedunangst.com/r/webs/htfilter"
13)
14
15var tweetsel = cascadia.MustCompile("p.tweet-text")
16var linksel = cascadia.MustCompile(".time a.tweet-timestamp")
17var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
18
19func hootfixer(hoot string) string {
20	url := hoot[5:]
21	if url[0] == ' ' {
22		url = url[1:]
23	}
24	url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
25	log.Printf("hooterizing %s", url)
26	req, err := http.NewRequest("GET", url, nil)
27	if err != nil {
28		log.Printf("error: %s", err)
29		return hoot
30	}
31	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")
32	req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
33	req.Header.Set("Accept-Language", "en-US,en;q=0.9")
34	resp, err := http.DefaultClient.Do(req)
35	if err != nil {
36		log.Printf("error: %s", err)
37		return hoot
38	}
39	defer resp.Body.Close()
40	if resp.StatusCode != 200 {
41		log.Printf("error getting %s: %d", url, resp.StatusCode)
42		return hoot
43	}
44
45	root, _ := html.Parse(resp.Body)
46	divs := tweetsel.MatchAll(root)
47
48	authormatch := authorregex.FindStringSubmatch(url)
49	if len(authormatch) < 2 {
50		log.Printf("no author")
51		return hoot
52	}
53	wanted := authormatch[1]
54	var buf strings.Builder
55
56	fmt.Fprintf(&buf, "hoot: %s\n", url)
57	for _, div := range divs {
58		twp := div.Parent.Parent.Parent
59		alink := linksel.MatchFirst(twp)
60		if alink == nil {
61			log.Printf("missing link")
62			continue
63		}
64		link := "https://twitter.com" + htfilter.GetAttr(alink, "href")
65		authormatch = authorregex.FindStringSubmatch(link)
66		if len(authormatch) < 2 {
67			log.Printf("no author?")
68			continue
69		}
70		author := authormatch[1]
71		if author != wanted {
72			continue
73		}
74		text := htfilter.TextOnly(div)
75		text = strings.Replace(text, "\n", " ", -1)
76		text = strings.Replace(text, "pic.twitter.com", "https://pic.twitter.com", -1)
77
78		fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
79	}
80	return buf.String()
81}
82
83var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
84
85func hooterize(noise string) string {
86	return re_hoots.ReplaceAllStringFunc(noise, hootfixer)
87}