all repos — honk @ df3bfebfee550ce8f851fc03c53c72c0da3b2176

my fork of honk

hoot.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"regexp"
	"strings"

	"github.com/andybalholm/cascadia"
	"golang.org/x/net/html"
	"humungus.tedunangst.com/r/webs/htfilter"
)

var tweetsel = cascadia.MustCompile("p.tweet-text")
var linksel = cascadia.MustCompile(".time a.tweet-timestamp")
var authorregex = regexp.MustCompile("twitter.com/([^/]+)")

func hootfetcher(hoot string) string {
	url := hoot[5:]
	if url[0] == ' ' {
		url = url[1:]
	}
	url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
	log.Printf("hooterizing %s", url)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Printf("error: %s", err)
		return hoot
	}
	req.Header.Set("User-Agent", "OpenBSD ftp")
	req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
	req.Header.Set("Accept-Language", "en-US,en;q=0.9")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Printf("error: %s", err)
		return hoot
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		log.Printf("error getting %s: %d", url, resp.StatusCode)
		return hoot
	}
	ld, _ := os.Create("lasthoot.html")
	r := io.TeeReader(resp.Body, ld)
	return hootfixer(r, url)
}

func hootfixer(r io.Reader, url string) string {
	root, _ := html.Parse(r)
	divs := tweetsel.MatchAll(root)

	wanted := ""
	var buf strings.Builder

	fmt.Fprintf(&buf, "hoot: %s\n", url)
	for _, div := range divs {
		twp := div.Parent.Parent.Parent
		alink := linksel.MatchFirst(twp)
		if alink == nil {
			log.Printf("missing link")
			continue
		}
		link := "https://twitter.com" + htfilter.GetAttr(alink, "href")
		authormatch := authorregex.FindStringSubmatch(link)
		if len(authormatch) < 2 {
			log.Printf("no author?")
			continue
		}
		author := authormatch[1]
		if wanted == "" {
			wanted = author
		}
		if author != wanted {
			continue
		}
		text := htfilter.TextOnly(div)
		text = strings.Replace(text, "\n", " ", -1)
		text = strings.Replace(text, "pic.twitter.com", "https://pic.twitter.com", -1)

		fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
	}
	return buf.String()
}

var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)

func hooterize(noise string) string {
	return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
}