all repos — honk @ 6a23f35877106f3ed8fb743429b58bbc16c613c4

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