all repos — honk @ 8f557f8374725f7df73656083cd92d45d907896a

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