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[data-testid=tweetText]")
32var linksel = cascadia.MustCompile("a time")
33var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor")
34var imgsel = cascadia.MustCompile("div[data-testid=tweetPhoto] 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.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 wanted == "" {
88 wanted = author
89 }
90 if author != wanted {
91 continue
92 }
93 for _, img := range imgsel.MatchAll(twp) {
94 img.Parent.RemoveChild(img)
95 div.AppendChild(img)
96 }
97 text := htf.NodeText(div)
98 text = strings.Replace(text, "\n", " ", -1)
99 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
100 continue
101 }
102
103 twp := div.Parent.Parent.Parent.Parent.Parent
104 link := url
105 alink := linksel.MatchFirst(twp)
106 if alink == nil {
107 if i != 0 {
108 dlog.Printf("missing link")
109 continue
110 }
111 } else {
112 link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
113 }
114 replto := replyingto.MatchFirst(twp)
115 if replto != nil {
116 continue
117 }
118 authormatch := authorregex.FindStringSubmatch(link)
119 if len(authormatch) < 2 {
120 dlog.Printf("no author?: %s", link)
121 continue
122 }
123 author := authormatch[1]
124 if wanted == "" {
125 wanted = author
126 }
127 if author != wanted {
128 continue
129 }
130 for _, img := range imgsel.MatchAll(twp) {
131 img.Parent.RemoveChild(img)
132 div.AppendChild(img)
133 }
134 text := htf.NodeText(div)
135 text = strings.Replace(text, "\n", " ", -1)
136 text = re_removepics.ReplaceAllString(text, "")
137
138 if seen[text] {
139 continue
140 }
141
142 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
143 seen[text] = true
144 }
145 return buf.String()
146}
147
148func hooterize(noise string) string {
149 seen := make(map[string]bool)
150
151 hootfetcher := func(hoot string) string {
152 url := hoot[5:]
153 if url[0] == ' ' {
154 url = url[1:]
155 }
156 url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
157 dlog.Printf("hooterizing %s", url)
158 req, err := http.NewRequest("GET", url, nil)
159 if err != nil {
160 ilog.Printf("error: %s", err)
161 return hoot
162 }
163 req.Header.Set("User-Agent", "Bot")
164 req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
165 req.Header.Set("Accept-Language", "en-US,en;q=0.9")
166 resp, err := http.DefaultClient.Do(req)
167 if err != nil {
168 ilog.Printf("error: %s", err)
169 return hoot
170 }
171 defer resp.Body.Close()
172 if resp.StatusCode != 200 {
173 ilog.Printf("error getting %s: %d", url, resp.StatusCode)
174 return hoot
175 }
176 ld, _ := os.Create("lasthoot.html")
177 r := io.TeeReader(resp.Body, ld)
178 return hootextractor(r, url, seen)
179 }
180
181 return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
182}