From f09a20f5ba899225d37432a109906fc8056664db Mon Sep 17 00:00:00 2001
From: Aaron Fischer
Date: Thu, 8 Apr 2021 23:53:03 +0200
Subject: [PATCH] RSS Feed working
---
go.mod | 2 +
go.sum | 2 +
src/bdm.go | 74 ++++++++++++++++++++----
templates/{BOOK.mkd.html => book.html} | 4 +-
templates/book.xml | 21 +++++++
templates/{COMIC.mkd.html => comic.html} | 4 +-
templates/comic.xml | 22 +++++++
7 files changed, 113 insertions(+), 16 deletions(-)
create mode 100644 go.sum
rename templates/{BOOK.mkd.html => book.html} (84%)
create mode 100644 templates/book.xml
rename templates/{COMIC.mkd.html => comic.html} (83%)
create mode 100644 templates/comic.xml
diff --git a/go.mod b/go.mod
index 2d8832a..66f8a4b 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module okoyono.de/buchdesmonats
go 1.16
+
+require github.com/a-h/gemini v0.0.61
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..8d42340
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/a-h/gemini v0.0.61 h1:WSV9f5T6Vut9gIATUhqAh9KfsA4/kqcUIbTiDbsn1JY=
+github.com/a-h/gemini v0.0.61/go.mod h1:p9wvIRDc2s3Lnbkw7CgNzDNgJHPuXDwh3dOF7w0NT8s=
diff --git a/src/bdm.go b/src/bdm.go
index f097a80..9516743 100644
--- a/src/bdm.go
+++ b/src/bdm.go
@@ -2,18 +2,23 @@ package main
import (
"fmt"
- "html/template"
+ //"html/template"
+ xmltpl "text/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
+ "strings"
)
type Item struct {
ISBN string
+ Author string
+ Title string
Filename string
+ Date string
}
func (i Item) ImageURL() string {
@@ -67,27 +72,61 @@ func getItems(filename string) []Item {
log.Fatal("Can not download the file. Network problem?")
}
- re := regexp.MustCompile(`[0-9]{13}`)
- matches := re.FindAllString(string(content), -1)
+ currentYear := ""
+ currentMonth := 0
+ re := regexp.MustCompile(`^[^[]+ \[(?P[^"]+)"(?P[^"]+)"\]\([^=]+=(?P[0-9]+).*$`)
+ yearRe := regexp.MustCompile(`^## (?P20[0-9]{2})$`)
- for _, isbn := range matches {
- items = append(items, Item{ISBN: isbn, Filename: filename})
+ var yearBucket []Item
+ for _, line := range strings.Split(string(content), "\n") {
+ // Do we find a year?
+ yearMatches := yearRe.FindStringSubmatch(line)
+ if len(yearMatches) > 0 {
+ currentYear = yearMatches[1]
+ currentMonth = 0
+
+ // Add the bucket in reverse order
+ for i := len(yearBucket)-1; i >= 0; i-- {
+ items = append(items, yearBucket[i])
+ }
+ yearBucket = nil
+ }
+
+ matches := re.FindStringSubmatch(line)
+ if len(matches) == 4 {
+ currentMonth++
+
+ yearBucket = append(yearBucket, Item{
+ Author: strings.Trim(matches[1], " "),
+ Title: strings.Trim(matches[2], " "),
+ ISBN: matches[3],
+ Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
+ Filename: filename,
+ })
+ }
+ }
+
+ log.Printf("Output all items:")
+ for _, i := range items {
+ log.Printf("%v", i)
}
return items
}
-func getHTML(filename string, w http.ResponseWriter) {
+func getTemplate(sourceFile string, templateFilename string, w http.ResponseWriter) {
// Get all items from the git repo
- items := getItems(filename)
+ items := getItems(sourceFile)
// Generate the restulting HTML
- t, err := template.ParseFiles("templates/" + filename + ".html")
+ t, err := xmltpl.ParseFiles("templates/" + templateFilename)
if err != nil {
panic(err)
}
- err = t.Execute(w, items)
+ err = t.Execute(w, map[string]interface{}{
+ "Items": items,
+ })
if err != nil {
panic(err)
}
@@ -115,6 +154,7 @@ func main() {
err := item.downloadCover()
if err != nil {
+ fmt.Printf("%v", err)
fmt.Printf("ERROR: File %s not found\n", item.ImageURL())
}
}
@@ -122,15 +162,25 @@ func main() {
http.HandleFunc("/book", func(w http.ResponseWriter, r *http.Request) {
log.Print("/book")
- getHTML("BOOK.mkd", w)
+ getTemplate("BOOK.mkd", "book.html", w)
})
http.HandleFunc("/comic", func(w http.ResponseWriter, r *http.Request) {
log.Print("/comic")
- getHTML("COMIC.mkd", w)
+ getTemplate("COMIC.mkd", "comic.html", w)
+ })
+ http.HandleFunc("/book.xml", func(w http.ResponseWriter, r *http.Request) {
+ log.Print("/book.xml")
+ w.Header().Add("Content-Type", "Application/rss+xml")
+ getTemplate("BOOK.mkd", "book.xml", w)
+ })
+ http.HandleFunc("/comic.xml", func(w http.ResponseWriter, r *http.Request) {
+ log.Print("/comic.xml")
+ w.Header().Add("Content-Type", "Application/rss+xml")
+ getTemplate("COMIC.mkd", "comic.xml", w)
})
// Spawn the webserver (blocking)
- log.Print("Spawn webserver on port :9783 and waiting for requests ...")
+ log.Print("Spawn webserver on port :9783 and waiting for requests ... ...")
err := http.ListenAndServe(":9783", nil)
if err != nil {
panic(err)
diff --git a/templates/BOOK.mkd.html b/templates/book.html
similarity index 84%
rename from templates/BOOK.mkd.html
rename to templates/book.html
index db8b4eb..fbc0a0e 100644
--- a/templates/BOOK.mkd.html
+++ b/templates/book.html
@@ -21,9 +21,9 @@
Ein økoyono Projekt.
- {{ range . }}
+ {{ range .Items }}
-
+
{{ end }}
diff --git a/templates/book.xml b/templates/book.xml
new file mode 100644
index 0000000..e59e0c1
--- /dev/null
+++ b/templates/book.xml
@@ -0,0 +1,21 @@
+
+
+
+ okoyono.de -- Buch des Monats
+
+ {{ (index .Items 0).Date }}T00:00:00Z
+
+ Michael Reutter
+
+ urn:uuid:{{ (index .Items 0).ISBN }}
+
+ {{ range .Items }}
+
+ {{ .Author | html }} - "{{ .Title | html }}"
+
+ urn:uuid:{{ .ISBN }}
+ {{ .Date }}T00:00:00Z
+ {{ .Author | html }} - "{{ .Title | html }}"
+
+ {{ end }}
+
diff --git a/templates/COMIC.mkd.html b/templates/comic.html
similarity index 83%
rename from templates/COMIC.mkd.html
rename to templates/comic.html
index 73946b4..b4fe371 100644
--- a/templates/COMIC.mkd.html
+++ b/templates/comic.html
@@ -16,9 +16,9 @@
Code von Aaron Fischer. Ein økoyono Projekt. (Eine Seite mit Buchempfehlungen findet Ihr hier)
- {{ range . }}
+ {{ range .Items }}
-
+
{{ end }}
diff --git a/templates/comic.xml b/templates/comic.xml
new file mode 100644
index 0000000..3695b5d
--- /dev/null
+++ b/templates/comic.xml
@@ -0,0 +1,22 @@
+
+
+
+ okoyono.de -- Comic des Monats
+
+ {{ (index .Items 0).Date }}T00:00:00Z
+
+ Michael Reutter
+
+ urn:uuid:{{ (index .Items 0).ISBN }}
+
+ {{ range .Items }}
+
+ {{ .Author | html }} - "{{ .Title | html }}"
+
+ urn:uuid:{{ .ISBN }}
+ {{ .Date }}T00:00:00Z
+ {{ .Author | html }} - "{{ .Title | html }}"
+
+ {{ end }}
+
+