all repos — honk @ 854f66c8705c29127c89770dfdf23a7e46027cb5

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