all repos — honk @ 5c82837bd8b1c23b532110c65ba16544d5ff61cd

my fork of honk

hoot.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"fmt"
 20	"io"
 21	"net/http"
 22	"os"
 23	"regexp"
 24	"strings"
 25
 26	"github.com/andybalholm/cascadia"
 27	"golang.org/x/net/html"
 28	"humungus.tedunangst.com/r/webs/htfilter"
 29)
 30
 31var tweetsel = cascadia.MustCompile("div[itemProp=articleBody]")
 32var linksel = cascadia.MustCompile("a time")
 33var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor")
 34var imgsel = cascadia.MustCompile("div.js-adaptive-photo img")
 35var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
 36
 37var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
 38var re_removepics = regexp.MustCompile(`pic\.twitter\.com/[[:alnum:]]+`)
 39
 40func hootextractor(r io.Reader, url string, seen map[string]bool) string {
 41	root, err := html.Parse(r)
 42	if err != nil {
 43		elog.Printf("error parsing hoot: %s", err)
 44		return url
 45	}
 46
 47	url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
 48	wantmatch := authorregex.FindStringSubmatch(url)
 49	var wanted string
 50	if len(wantmatch) == 2 {
 51		wanted = wantmatch[1]
 52	}
 53
 54	var htf htfilter.Filter
 55	htf.Imager = func(node *html.Node) string {
 56		alt := htfilter.GetAttr(node, "alt")
 57		if htfilter.HasClass(node, "Emoji") && alt != "" {
 58			return alt
 59		}
 60		return fmt.Sprintf(" <img src='%s'>", htfilter.GetAttr(node, "src"))
 61	}
 62
 63	var buf strings.Builder
 64	fmt.Fprintf(&buf, "%s\n", url)
 65
 66	divs := tweetsel.MatchAll(root)
 67	for i, div := range divs {
 68		{
 69			twp := div.Parent.Parent.Parent
 70			link := url
 71			alink := linksel.MatchFirst(twp)
 72			if alink == nil {
 73				if i != 0 {
 74					dlog.Printf("missing link")
 75					continue
 76				}
 77			} else {
 78				alink = alink.Parent
 79				link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
 80			}
 81			authormatch := authorregex.FindStringSubmatch(link)
 82			if len(authormatch) < 2 {
 83				dlog.Printf("no author?: %s", link)
 84				continue
 85			}
 86			author := authormatch[1]
 87			if author != wanted {
 88				continue
 89			}
 90			text := htf.NodeText(div)
 91			text = strings.Replace(text, "\n", " ", -1)
 92			fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
 93			continue
 94		}
 95
 96		twp := div.Parent.Parent.Parent
 97		link := url
 98		alink := linksel.MatchFirst(twp)
 99		if alink == nil {
100			if i != 0 {
101				dlog.Printf("missing link")
102				continue
103			}
104		} else {
105			link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
106		}
107		replto := replyingto.MatchFirst(twp)
108		if replto != nil {
109			continue
110		}
111		authormatch := authorregex.FindStringSubmatch(link)
112		if len(authormatch) < 2 {
113			dlog.Printf("no author?: %s", link)
114			continue
115		}
116		author := authormatch[1]
117		if wanted == "" {
118			wanted = author
119		}
120		if author != wanted {
121			continue
122		}
123		for _, img := range imgsel.MatchAll(twp) {
124			img.Parent.RemoveChild(img)
125			div.AppendChild(img)
126		}
127		text := htf.NodeText(div)
128		text = strings.Replace(text, "\n", " ", -1)
129		text = re_removepics.ReplaceAllString(text, "")
130
131		if seen[text] {
132			continue
133		}
134
135		fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
136		seen[text] = true
137	}
138	return buf.String()
139}
140
141func hooterize(noise string) string {
142	seen := make(map[string]bool)
143
144	hootfetcher := func(hoot string) string {
145		url := hoot[5:]
146		if url[0] == ' ' {
147			url = url[1:]
148		}
149		url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
150		dlog.Printf("hooterizing %s", url)
151		req, err := http.NewRequest("GET", url, nil)
152		if err != nil {
153			ilog.Printf("error: %s", err)
154			return hoot
155		}
156		req.Header.Set("User-Agent", "Bot")
157		req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
158		req.Header.Set("Accept-Language", "en-US,en;q=0.9")
159		resp, err := http.DefaultClient.Do(req)
160		if err != nil {
161			ilog.Printf("error: %s", err)
162			return hoot
163		}
164		defer resp.Body.Close()
165		if resp.StatusCode != 200 {
166			ilog.Printf("error getting %s: %d", url, resp.StatusCode)
167			return hoot
168		}
169		ld, _ := os.Create("lasthoot.html")
170		r := io.TeeReader(resp.Body, ld)
171		return hootextractor(r, url, seen)
172	}
173
174	return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
175}