all repos — honk @ 82641e81b2b76dbbf46fc9d2173b1999ab2cff7a

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