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(".time a.tweet-timestamp")
34var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
35
36var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
37
38func hootextractor(r io.Reader, url string, seen map[string]bool) string {
39 root, err := html.Parse(r)
40 if err != nil {
41 log.Printf("error parsing hoot: %s", err)
42 return url
43 }
44 divs := tweetsel.MatchAll(root)
45
46 var wanted string
47 wantmatch := authorregex.FindStringSubmatch(url)
48 if len(wantmatch) == 2 {
49 wanted = wantmatch[1]
50 }
51 var buf strings.Builder
52
53 fmt.Fprintf(&buf, "%s\n", url)
54 var htf htfilter.Filter
55 for _, div := range divs {
56 twp := div.Parent.Parent.Parent
57 alink := linksel.MatchFirst(twp)
58 if alink == nil {
59 log.Printf("missing link")
60 continue
61 }
62 link := "https://twitter.com" + htfilter.GetAttr(alink, "href")
63 authormatch := authorregex.FindStringSubmatch(link)
64 if len(authormatch) < 2 {
65 log.Printf("no author?")
66 continue
67 }
68 author := authormatch[1]
69 if wanted == "" {
70 wanted = author
71 }
72 if author != wanted {
73 continue
74 }
75 text := htf.TextOnly(div)
76 text = strings.Replace(text, "\n", " ", -1)
77 text = strings.Replace(text, "pic.twitter.com", "https://pic.twitter.com", -1)
78
79 if seen[text] {
80 continue
81 }
82
83 fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
84 seen[text] = true
85 }
86 return buf.String()
87}
88
89func hooterize(noise string) string {
90 seen := make(map[string]bool)
91
92 hootfetcher := func(hoot string) string {
93 url := hoot[5:]
94 if url[0] == ' ' {
95 url = url[1:]
96 }
97 url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
98 log.Printf("hooterizing %s", url)
99 req, err := http.NewRequest("GET", url, nil)
100 if err != nil {
101 log.Printf("error: %s", err)
102 return hoot
103 }
104 req.Header.Set("User-Agent", "OpenBSD ftp")
105 req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
106 req.Header.Set("Accept-Language", "en-US,en;q=0.9")
107 resp, err := http.DefaultClient.Do(req)
108 if err != nil {
109 log.Printf("error: %s", err)
110 return hoot
111 }
112 defer resp.Body.Close()
113 if resp.StatusCode != 200 {
114 log.Printf("error getting %s: %d", url, resp.StatusCode)
115 return hoot
116 }
117 ld, _ := os.Create("lasthoot.html")
118 r := io.TeeReader(resp.Body, ld)
119 return hootextractor(r, url, seen)
120 }
121
122 return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
123}