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 "humungus.tedunangst.com/r/webs/templates"
30)
31
32var tweetsel = cascadia.MustCompile("div[data-testid=tweetText]")
33var linksel = cascadia.MustCompile("a time")
34var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor")
35var imgsel = cascadia.MustCompile("div[data-testid=tweetPhoto] img")
36var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
37
38var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
39var re_removepics = regexp.MustCompile(`pic\.twitter\.com/[[:alnum:]]+`)
40
41func hootextractor(r io.Reader, url string, seen map[string]bool) string {
42 root, err := html.Parse(r)
43 if err != nil {
44 elog.Printf("error parsing hoot: %s", err)
45 return url
46 }
47
48 url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
49 wantmatch := authorregex.FindStringSubmatch(url)
50 var wanted string
51 if len(wantmatch) == 2 {
52 wanted = wantmatch[1]
53 }
54
55 var htf htfilter.Filter
56 htf.Imager = func(node *html.Node) string {
57 alt := htfilter.GetAttr(node, "alt")
58 if htfilter.HasClass(node, "Emoji") && alt != "" {
59 return alt
60 }
61 return string(templates.Sprintf(" <img src='%s' alt='%s'>", htfilter.GetAttr(node, "src"), alt))
62 }
63
64 var buf strings.Builder
65 fmt.Fprintf(&buf, "%s\n", url)
66
67 divs := tweetsel.MatchAll(root)
68 for i, div := range divs {
69 {
70 twp := div.Parent.Parent.Parent.Parent.Parent
71 link := url
72 alink := linksel.MatchFirst(twp)
73 if alink == nil {
74 if i != 0 {
75 dlog.Printf("missing link")
76 continue
77 }
78 } else {
79 alink = alink.Parent
80 link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
81 }
82 authormatch := authorregex.FindStringSubmatch(link)
83 if len(authormatch) < 2 {
84 dlog.Printf("no author?: %s", link)
85 continue
86 }
87 author := authormatch[1]
88 if wanted == "" {
89 wanted = author
90 }
91 if author != wanted {
92 continue
93 }
94 for _, img := range imgsel.MatchAll(twp) {
95 img.Parent.RemoveChild(img)
96 div.AppendChild(img)
97 }
98 text := htf.NodeText(div)
99 text = strings.Replace(text, "\n", " ", -1)
100 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
101 continue
102 }
103
104 twp := div.Parent.Parent.Parent.Parent.Parent
105 link := url
106 alink := linksel.MatchFirst(twp)
107 if alink == nil {
108 if i != 0 {
109 dlog.Printf("missing link")
110 continue
111 }
112 } else {
113 link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
114 }
115 replto := replyingto.MatchFirst(twp)
116 if replto != nil {
117 continue
118 }
119 authormatch := authorregex.FindStringSubmatch(link)
120 if len(authormatch) < 2 {
121 dlog.Printf("no author?: %s", link)
122 continue
123 }
124 author := authormatch[1]
125 if wanted == "" {
126 wanted = author
127 }
128 if author != wanted {
129 continue
130 }
131 for _, img := range imgsel.MatchAll(twp) {
132 img.Parent.RemoveChild(img)
133 div.AppendChild(img)
134 }
135 text := htf.NodeText(div)
136 text = strings.Replace(text, "\n", " ", -1)
137 text = re_removepics.ReplaceAllString(text, "")
138
139 if seen[text] {
140 continue
141 }
142
143 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
144 seen[text] = true
145 }
146 return buf.String()
147}
148
149func hooterize(noise string) string {
150 seen := make(map[string]bool)
151
152 hootfetcher := func(hoot string) string {
153 url := hoot[5:]
154 if url[0] == ' ' {
155 url = url[1:]
156 }
157 url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
158 dlog.Printf("hooterizing %s", url)
159 req, err := http.NewRequest("GET", url, nil)
160 if err != nil {
161 ilog.Printf("error: %s", err)
162 return hoot
163 }
164 req.Header.Set("User-Agent", "Bot")
165 req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
166 req.Header.Set("Accept-Language", "en-US,en;q=0.9")
167 resp, err := http.DefaultClient.Do(req)
168 if err != nil {
169 ilog.Printf("error: %s", err)
170 return hoot
171 }
172 defer resp.Body.Close()
173 if resp.StatusCode != 200 {
174 ilog.Printf("error getting %s: %d", url, resp.StatusCode)
175 return hoot
176 }
177 ld, _ := os.Create("lasthoot.html")
178 r := io.TeeReader(resp.Body, ld)
179 return hootextractor(r, url, seen)
180 }
181
182 return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
183}