honk/hoot.go

113 lines
3.1 KiB
Go
Raw Normal View History

//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2019-06-05 09:52:56 +02:00
package main
import (
"fmt"
2019-07-18 05:11:48 +02:00
"io"
2019-06-05 09:52:56 +02:00
"log"
"net/http"
2019-07-18 05:11:48 +02:00
"os"
2019-06-05 09:52:56 +02:00
"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/([^/]+)")
2019-07-18 05:11:48 +02:00
func hootfetcher(hoot string) string {
2019-06-05 09:52:56 +02:00
url := hoot[5:]
if url[0] == ' ' {
url = url[1:]
}
2019-06-05 09:55:27 +02:00
url = strings.Replace(url, "mobile.twitter.com", "twitter.com", -1)
2019-06-05 09:52:56 +02:00
log.Printf("hooterizing %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("error: %s", err)
return hoot
}
2019-07-18 05:11:48 +02:00
req.Header.Set("User-Agent", "OpenBSD ftp")
2019-06-05 09:52:56 +02:00
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
}
2019-07-18 05:11:48 +02:00
ld, _ := os.Create("lasthoot.html")
r := io.TeeReader(resp.Body, ld)
return hootfixer(r, url)
}
2019-06-05 09:52:56 +02:00
2019-07-18 05:11:48 +02:00
func hootfixer(r io.Reader, url string) string {
2019-09-03 07:19:55 +02:00
root, err := html.Parse(r)
if err != nil {
log.Printf("error parsing hoot: %s", err)
return url
}
2019-06-05 09:52:56 +02:00
divs := tweetsel.MatchAll(root)
2019-07-18 05:11:48 +02:00
wanted := ""
2019-06-05 09:52:56 +02:00
var buf strings.Builder
filt := htfilter.New()
2019-09-03 07:19:55 +02:00
fmt.Fprintf(&buf, "%s\n", url)
2019-06-05 09:52:56 +02:00
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")
2019-07-18 05:11:48 +02:00
authormatch := authorregex.FindStringSubmatch(link)
2019-06-05 09:52:56 +02:00
if len(authormatch) < 2 {
log.Printf("no author?")
continue
}
author := authormatch[1]
2019-07-18 05:11:48 +02:00
if wanted == "" {
wanted = author
}
2019-06-05 09:52:56 +02:00
if author != wanted {
continue
}
text := filt.TextOnly(div)
2019-06-05 09:55:27 +02:00
text = strings.Replace(text, "\n", " ", -1)
text = strings.Replace(text, "pic.twitter.com", "https://pic.twitter.com", -1)
2019-06-05 09:52:56 +02:00
fmt.Fprintf(&buf, "> @%s: %s\n", author, text)
}
return buf.String()
}
var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
func hooterize(noise string) string {
2019-07-18 05:11:48 +02:00
return re_hoots.ReplaceAllStringFunc(noise, hootfetcher)
2019-06-05 09:52:56 +02:00
}