2021-03-12 00:01:27 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-08 23:53:03 +02:00
|
|
|
//"html/template"
|
|
|
|
xmltpl "text/template"
|
2021-03-12 00:01:27 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2021-03-18 23:16:26 +01:00
|
|
|
"log"
|
2021-03-12 00:01:27 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2021-04-08 23:53:03 +02:00
|
|
|
"strings"
|
2021-03-12 00:01:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Item struct {
|
2021-03-18 23:16:26 +01:00
|
|
|
ISBN string
|
2021-04-08 23:53:03 +02:00
|
|
|
Author string
|
|
|
|
Title string
|
2021-03-12 00:01:27 +01:00
|
|
|
Filename string
|
2021-04-08 23:53:03 +02:00
|
|
|
Date string
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 23:16:26 +01:00
|
|
|
func (i Item) ImageURL() string {
|
2021-07-01 22:18:39 +02:00
|
|
|
return "https://medien.umbreitkatalog.de/bildzentrale_original/" +
|
2021-03-12 00:01:27 +01:00
|
|
|
i.ISBN[0:3] + "/" +
|
|
|
|
i.ISBN[3:6] + "/" +
|
|
|
|
i.ISBN[6:9] + "/" +
|
|
|
|
i.ISBN[9:13] +
|
|
|
|
".jpg"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Item) targetFilename() string {
|
|
|
|
return "covers/" + i.ISBN + ".jpg"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i Item) downloadCover() error {
|
2021-03-18 23:16:26 +01:00
|
|
|
resp, err := http.Get(i.ImageURL())
|
2021-03-12 00:01:27 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(i.targetFilename())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getItems(filename string) []Item {
|
|
|
|
var items []Item
|
|
|
|
// Get all book URLS
|
|
|
|
url := "https://git.okoyono.de/mezzo/buch_des_monats/raw/branch/master/" + filename
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-19 20:33:55 +01:00
|
|
|
log.Fatal(filename + " is missing")
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2021-03-19 20:33:55 +01:00
|
|
|
log.Fatal("Can not download the file. Network problem?")
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
|
2021-04-08 23:53:03 +02:00
|
|
|
currentYear := ""
|
|
|
|
currentMonth := 0
|
|
|
|
re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"\]\([^=]+=(?P<isbn>[0-9]+).*$`)
|
|
|
|
yearRe := regexp.MustCompile(`^## (?P<year>20[0-9]{2})$`)
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 00:01:27 +01:00
|
|
|
|
2021-04-08 23:53:03 +02:00
|
|
|
log.Printf("Output all items:")
|
|
|
|
for _, i := range items {
|
|
|
|
log.Printf("%v", i)
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:53:03 +02:00
|
|
|
func getTemplate(sourceFile string, templateFilename string, w http.ResponseWriter) {
|
2021-03-12 00:01:27 +01:00
|
|
|
// Get all items from the git repo
|
2021-04-08 23:53:03 +02:00
|
|
|
items := getItems(sourceFile)
|
2021-03-12 00:01:27 +01:00
|
|
|
|
2021-03-18 23:16:26 +01:00
|
|
|
// Generate the restulting HTML
|
2021-04-08 23:53:03 +02:00
|
|
|
t, err := xmltpl.ParseFiles("templates/" + templateFilename)
|
2021-03-18 23:16:26 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:53:03 +02:00
|
|
|
err = t.Execute(w, map[string]interface{}{
|
|
|
|
"Items": items,
|
|
|
|
})
|
2021-03-18 23:16:26 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// All static files (CSS, JS)
|
|
|
|
fileServer := http.FileServer(http.Dir("./static"))
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static", fileServer))
|
|
|
|
|
2021-03-19 20:33:55 +01:00
|
|
|
// Cover images
|
2021-03-18 23:16:26 +01:00
|
|
|
imageServer := http.FileServer(http.Dir("./covers/"))
|
|
|
|
http.Handle("/covers/", http.StripPrefix("/covers", imageServer))
|
|
|
|
|
|
|
|
// Update "Hook" /update?filename=COMIC.mkd
|
|
|
|
http.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
filename := r.URL.Query().Get("filename")
|
2021-03-19 20:33:55 +01:00
|
|
|
log.Printf("Update hook triggered for %v", filename)
|
2021-03-18 23:16:26 +01:00
|
|
|
|
|
|
|
// Get all items from the git repo
|
|
|
|
items := getItems(filename)
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
fmt.Printf("Downloading %v ...\n", item.ImageURL())
|
2021-03-12 00:01:27 +01:00
|
|
|
err := item.downloadCover()
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-08 23:53:03 +02:00
|
|
|
fmt.Printf("%v", err)
|
2021-03-18 23:16:26 +01:00
|
|
|
fmt.Printf("ERROR: File %s not found\n", item.ImageURL())
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 23:16:26 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/book", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Print("/book")
|
2021-04-08 23:53:03 +02:00
|
|
|
getTemplate("BOOK.mkd", "book.html", w)
|
2021-03-18 23:16:26 +01:00
|
|
|
})
|
|
|
|
http.HandleFunc("/comic", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Print("/comic")
|
2021-04-08 23:53:03 +02:00
|
|
|
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)
|
2021-03-18 23:16:26 +01:00
|
|
|
})
|
|
|
|
|
2021-03-19 20:33:55 +01:00
|
|
|
// Spawn the webserver (blocking)
|
2021-04-08 23:53:03 +02:00
|
|
|
log.Print("Spawn webserver on port :9783 and waiting for requests ... ...")
|
2021-03-18 23:35:08 +01:00
|
|
|
err := http.ListenAndServe(":9783", nil)
|
2021-03-18 23:16:26 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-03-12 00:01:27 +01:00
|
|
|
}
|
|
|
|
}
|