From 7a56cec6b6122d1e7b80e4283f259346c7eb3547 Mon Sep 17 00:00:00 2001 From: Petter Rodhelind Date: Sun, 21 Feb 2021 22:09:42 +0100 Subject: Support images. --- README.md | 4 ++++ main.go | 12 ++++++++++++ parser.go | 39 +++++++++++++++++++++++++++++++-------- templates.go | 14 +++++++++++++- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 92091fc..124e096 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,7 @@ For HTML: `http://localhost:1212/nameOfGroup` For RSS: `http://localhost:1212/nameOfGroup.rss` `nameOfGroup` is the @-name in Facebook. + +## License + +MIT. See the LICENSE file. \ No newline at end of file diff --git a/main.go b/main.go index a491337..fc243fa 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,12 @@ type post struct { Time time.Time Link string Content string + Images []*image // list of urls +} + +type image struct { + Source string + Caption string } func (p *post) String() string { @@ -61,6 +67,12 @@ func (p *post) String() string { // time format: Mon Jan 2 15:04:05 -0700 MST 2006 s = strings.Replace(template, "{{time}}", p.Time.Format("Mon, 2 Jan 2006 15:04:05 MST"), 2) s = strings.Replace(s, "{{content}}", p.Content, 1) + var imgs string + for i := range p.Images { + imgs += `` + } + s = strings.Replace(s, "{{images}}", imgs, 1) + return s } diff --git a/parser.go b/parser.go index 58ce9bf..4d3720b 100644 --- a/parser.go +++ b/parser.go @@ -40,7 +40,6 @@ func parse(r io.Reader) (c *channel, err error) { if n.Type == html.ElementNode && n.Data == "div" { for _, a := range n.Attr { - //if a.Key == "class" && strings.Contains(a.Val, "text_exposed_root") { if a.Key == "class" && strings.Contains(a.Val, "userContentWrapper") { var p post parseNode(n, &p) @@ -62,8 +61,9 @@ func parse(r io.Reader) (c *channel, err error) { func parseNode(n *html.Node, p *post) { if n.Type == html.ElementNode { + switch n.Data { // fetch time stamp from header - if n.Data == "abbr" { + case "abbr": for _, attr := range n.Attr { if attr.Key == "data-utime" { unix, _ := strconv.ParseInt(attr.Val, 10, 64) @@ -71,13 +71,15 @@ func parseNode(n *html.Node, p *post) { return } } - } + // skip post header with name of group and check-in - if n.Data == "h5" { + case "h5": return - } + // skip "show more" and aria-hidden stuff - if n.Data == "span" { + case "span": + fallthrough + case "a": for _, attr := range n.Attr { if attr.Key == "class" && attr.Val == "text_exposed_hide" { return @@ -86,13 +88,17 @@ func parseNode(n *html.Node, p *post) { return } } - } // parse paragraphs - if n.Data == "p" { + case "p": parseParagraph(n, p) p.Content += "\n\n" return + + // get images + case "img": + parseImage(n, p) + return } } @@ -118,3 +124,20 @@ func parseParagraph(n *html.Node, p *post) { parseParagraph(c, p) } } + +func parseImage(n *html.Node, p *post) { + img := &image{} + for _, attr := range n.Attr { + switch attr.Key { + case "src": + img.Source = attr.Val + case "alt": + fallthrough + case "title": + fallthrough + case "aria-label": + img.Caption = attr.Val + } + } + p.Images = append(p.Images, img) +} diff --git a/templates.go b/templates.go index 0537f7c..e768c60 100644 --- a/templates.go +++ b/templates.go @@ -49,6 +49,17 @@ const htmlRoot = ` word-wrap: break-word; white-space: pre-line; } + div#images { + padding-left: 1em; + } + img { + max-height: 200px; + max-width: 200px; + height: auto; + width: auto; + border-radius: 12px; + padding: 0.2em; + } @@ -63,7 +74,7 @@ const htmlRoot = `
@@ -74,5 +85,6 @@ const htmlItem = `
{{time}}
{{content}}
+
{{images}}
` -- cgit v1.2.3