include images in the hooter

This commit is contained in:
Ted Unangst 2022-01-03 02:44:38 -05:00
parent f547153ecc
commit 60bc158774
2 changed files with 10 additions and 3 deletions

View File

@ -2,6 +2,8 @@ changelog
=== next === next
+ Images in the hooter.
+ Unread count for chatter. + Unread count for chatter.
+ More flexible hashtag characters. + More flexible hashtag characters.

11
hoot.go
View File

@ -32,9 +32,11 @@ import (
var tweetsel = cascadia.MustCompile("p.tweet-text") var tweetsel = cascadia.MustCompile("p.tweet-text")
var linksel = cascadia.MustCompile("a.tweet-timestamp") var linksel = cascadia.MustCompile("a.tweet-timestamp")
var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor") var replyingto = cascadia.MustCompile(".ReplyingToContextBelowAuthor")
var imgsel = cascadia.MustCompile("div.js-adaptive-photo img")
var authorregex = regexp.MustCompile("twitter.com/([^/]+)") var authorregex = regexp.MustCompile("twitter.com/([^/]+)")
var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`) var re_hoots = regexp.MustCompile(`hoot: ?https://\S+`)
var re_removepics = regexp.MustCompile(`pic\.twitter\.com/[[:alnum:]]+`)
func hootextractor(r io.Reader, url string, seen map[string]bool) string { func hootextractor(r io.Reader, url string, seen map[string]bool) string {
root, err := html.Parse(r) root, err := html.Parse(r)
@ -52,11 +54,10 @@ func hootextractor(r io.Reader, url string, seen map[string]bool) string {
wanted = wantmatch[1] wanted = wantmatch[1]
} }
var buf strings.Builder var buf strings.Builder
fmt.Fprintf(&buf, "%s\n", url) fmt.Fprintf(&buf, "%s\n", url)
var htf htfilter.Filter var htf htfilter.Filter
htf.Imager = func(node *html.Node) string { htf.Imager = func(node *html.Node) string {
return "" return fmt.Sprintf(" <img src='%s'>", htfilter.GetAttr(node, "src"))
} }
for i, div := range divs { for i, div := range divs {
twp := div.Parent.Parent.Parent twp := div.Parent.Parent.Parent
@ -86,9 +87,13 @@ func hootextractor(r io.Reader, url string, seen map[string]bool) string {
if author != wanted { if author != wanted {
continue continue
} }
if img := imgsel.MatchFirst(twp); img != nil {
img.Parent.RemoveChild(img)
div.AppendChild(img)
}
text := htf.NodeText(div) text := htf.NodeText(div)
text = strings.Replace(text, "\n", " ", -1) text = strings.Replace(text, "\n", " ", -1)
text = strings.Replace(text, "pic.twitter.com", "https://pic.twitter.com", -1) text = re_removepics.ReplaceAllString(text, "")
if seen[text] { if seen[text] {
continue continue