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 "log"
22 "net/http"
23 "os"
24 "regexp"
25 "strings"
26
27 "github.com/andybalholm/cascadia"
28 "golang.org/x/net/html"
29 "humungus.tedunangst.com/r/webs/htfilter"
30)
31
32var tweetsel = cascadia.MustCompile("p.tweet-text")
33var linksel = cascadia.MustCompile("a.tweet-timestamp")
34var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor")
35var imgsel = cascadia.MustCompile("div.js-adaptive-photo 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 log.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 return fmt.Sprintf(" <img src='%s'>", htfilter.GetAttr(node, "src"))
58 }
59
60 var buf strings.Builder
61 fmt.Fprintf(&buf, "%s\n", url)
62
63 divs := tweetsel.MatchAll(root)
64 for i, div := range divs {
65 twp := div.Parent.Parent.Parent
66 link := url
67 alink := linksel.MatchFirst(twp)
68 if alink == nil {
69 if i != 0 {
70 log.Printf("missing link")
71 continue
72 }
73 } else {
74 link = "https://twitter.com" + htfilter.GetAttr(alink, "href")
75 }
76 replto := replyingto.MatchFirst(twp)
77 if replto != nil {
78 continue
79 }
80 authormatch := authorregex.FindStringSubmatch(link)
81 if len(authormatch) < 2 {
82 log.Printf("no author?: %s", link)
83 continue
84 }
85 author := authormatch[1]
86 if wanted == "" {
87 wanted = author
88 }
89 if author != wanted {
90 continue
91 }
92 for img := imgsel.MatchFirst(twp); img != nil; imgsel.MatchFirst(twp) {
93 img.Parent.RemoveChild(img)
94 div.AppendChild(img)
95 }
96 text := htf.NodeText(div)
97 text = strings.Replace(text, "\n", " ", -1)
98 text = re_removepics.ReplaceAllString(text, "")
99
100 if seen[text] {
101 continue
102 }
103
104 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
105 seen[text] = true
106 }
107 return buf.String()
108}
109
110func hooterize(noise string) string {
111 seen := make(map[string]bool)
112
113 hootfetcher := func(hoot string) string {
114 url := hoot[5:]
115 if url[0] == ' ' {
116 url = url[1:]
117 }
118 url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
119 log.Printf("hooterizing %s", url)
120 req, err := http.NewRequest("GET", url, nil)
121 if err != nil {
122 log.Printf("error: %s", err)
123 return hoot
124 }
125 req.Header.Set("User-Agent", "Bot")
126 req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
127 req.Header.Set("Accept-Language", "en-US,en;q=0.9")
128 resp, err := http.DefaultClient.Do(req)
129 if err != nil {
130 log.Printf("error: %s", err)
131 return hoot
132 }
133 defer resp.Body.Close()
134 if resp.StatusCode != 200 {
135 log.Printf("error getting %s: %d", url, resp.StatusCode)
136 return hoot
137 }
138 ld, _ := os.Create("lasthoot.html")
139 r := io.TeeReader(resp.Body, ld)
140 return hootextractor(r, url, seen)
141 }
142
143 return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
144}