rss2html/main.go

119 lines
2.5 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"sort"
"strings"
"time"
"github.com/mmcdole/gofeed"
"golang.org/x/net/html"
)
type RSSInfo struct {
Items []gofeed.Item
}
// Len returns the length of Items.
func (f RSSInfo) Len() int {
return len(f.Items)
}
// Less compares PublishedParsed of Items[i], Items[k]
// and returns true if Items[i] is less than Items[k].
func (f RSSInfo) Less(i, k int) bool {
return f.Items[k].PublishedParsed.Before(
*f.Items[i].PublishedParsed,
)
}
// Swap swaps Items[i] and Items[k].
func (f RSSInfo) Swap(i, k int) {
f.Items[i], f.Items[k] = f.Items[k], f.Items[i]
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
fmt.Println("<!DOCTYPE html>")
fmt.Println("<html>")
fmt.Println("<body>")
fmt.Println("<ul>")
items := []gofeed.Item{}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
fp := gofeed.NewParser()
feed, err := fp.ParseURLWithContext(strings.TrimSpace(s.Text()), ctx)
if err != nil {
log.Println(err)
} else {
for _, item := range feed.Items {
items = append(items, *item)
}
}
}
info := RSSInfo{
Items: items,
}
sort.Sort(info)
for _, item := range info.Items {
description := fixHtml(item.Description)
content := fixHtml(item.Content)
fmt.Println("<li>")
fmt.Printf("<h3><a href=\"%s\">%s</a></h3>\n", item.Link, item.Title)
if description != "" {fmt.Printf("<h5>%s</h5>\n", description)}
if content != "" {fmt.Printf("<p>%s</p>\n", content)}
if item.Image != nil {fmt.Printf("<img src=\"%s\" height=\"400\">\n", item.Image.URL)}
fmt.Println("</li>")
}
fmt.Println("</ul>")
fmt.Println("</body>")
fmt.Println("</html>")
}
func fixHtml(rawHtml string) string {
content := ""
// Parse the HTML string
doc, err := html.Parse(strings.NewReader(rawHtml))
if err != nil {
content = rawHtml
} else {
// Modify img and video elements to have static height and width
var f func(*html.Node)
f = func(n *html.Node) {
switch n.Data {
case "img", "video", "iframe", "object", "embed", "canvas":
n.Attr = append(n.Attr, html.Attribute{Key: "height", Val: "400px"})
// n.Attr = append(n.Attr, html.Attribute{Key: "width", Val: "100px"})
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
// Convert the modified doc back to a string
buf := &strings.Builder{}
err = html.Render(buf, doc)
if err != nil {
log.Println(err)
} else {
content = buf.String()
}
}
return content
}