2024-08-19 21:54:33 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-26 22:49:39 -04:00
|
|
|
"bufio"
|
2024-08-19 21:54:33 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-08-26 22:49:39 -04:00
|
|
|
"sort"
|
2024-08-19 22:27:34 -04:00
|
|
|
"strings"
|
2024-08-26 22:49:39 -04:00
|
|
|
"time"
|
|
|
|
|
2024-08-19 21:54:33 -04:00
|
|
|
"github.com/mmcdole/gofeed"
|
2024-09-15 22:48:02 -04:00
|
|
|
"golang.org/x/net/html"
|
2024-08-19 21:54:33 -04:00
|
|
|
)
|
|
|
|
|
2024-08-26 22:49:39 -04:00
|
|
|
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() {
|
2024-08-19 21:54:33 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
fmt.Println("<!DOCTYPE html>")
|
|
|
|
fmt.Println("<html>")
|
|
|
|
fmt.Println("<body>")
|
|
|
|
fmt.Println("<ul>")
|
|
|
|
|
2024-08-26 22:49:39 -04:00
|
|
|
items := []gofeed.Item{}
|
|
|
|
|
2024-08-19 21:54:33 -04:00
|
|
|
s := bufio.NewScanner(os.Stdin)
|
|
|
|
for s.Scan() {
|
|
|
|
fp := gofeed.NewParser()
|
2024-08-19 22:27:34 -04:00
|
|
|
feed, err := fp.ParseURLWithContext(strings.TrimSpace(s.Text()), ctx)
|
2024-08-19 21:54:33 -04:00
|
|
|
if err != nil {
|
2024-08-19 22:27:34 -04:00
|
|
|
log.Println(err)
|
2024-08-19 21:54:33 -04:00
|
|
|
} else {
|
|
|
|
for _, item := range feed.Items {
|
2024-08-26 22:49:39 -04:00
|
|
|
items = append(items, *item)
|
2024-08-19 21:54:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-26 22:49:39 -04:00
|
|
|
|
|
|
|
info := RSSInfo{
|
|
|
|
Items: items,
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(info)
|
|
|
|
|
|
|
|
for _, item := range info.Items {
|
2024-09-15 22:48:02 -04:00
|
|
|
description := fixHtml(item.Description)
|
|
|
|
content := fixHtml(item.Content)
|
|
|
|
|
2024-09-17 19:56:39 -04:00
|
|
|
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>")
|
2024-08-26 22:49:39 -04:00
|
|
|
}
|
|
|
|
|
2024-08-19 21:54:33 -04:00
|
|
|
fmt.Println("</ul>")
|
|
|
|
fmt.Println("</body>")
|
|
|
|
fmt.Println("</html>")
|
|
|
|
}
|
2024-09-15 22:48:02 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|