136 lines
2.7 KiB
Go
136 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
type Item struct {
|
|
ISBN string
|
|
Filename string
|
|
}
|
|
|
|
func (i Item) ImageURL() string {
|
|
return "https://medien.ubitweb.de/bildzentrale_original/" +
|
|
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 {
|
|
resp, err := http.Get(i.ImageURL())
|
|
|
|
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 {
|
|
panic(filename + " is missing")
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
panic("Can not download the file. Network problem?")
|
|
}
|
|
|
|
re := regexp.MustCompile(`[0-9]{13}`)
|
|
matches := re.FindAllString(string(content), -1)
|
|
|
|
for _, isbn := range matches {
|
|
items = append(items, Item{ISBN: isbn, Filename: filename})
|
|
}
|
|
|
|
return items
|
|
}
|
|
|
|
func getHTML(filename string, w http.ResponseWriter) {
|
|
// Get all items from the git repo
|
|
items := getItems(filename)
|
|
|
|
// Generate the restulting HTML
|
|
t, err := template.ParseFiles("templates/" + filename + ".html")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = t.Execute(w, items)
|
|
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))
|
|
|
|
// Images
|
|
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")
|
|
|
|
// Get all items from the git repo
|
|
items := getItems(filename)
|
|
|
|
for _, item := range items {
|
|
fmt.Printf("Downloading %v ...\n", item.ImageURL())
|
|
err := item.downloadCover()
|
|
|
|
if err != nil {
|
|
fmt.Printf("ERROR: File %s not found\n", item.ImageURL())
|
|
}
|
|
}
|
|
})
|
|
|
|
// Spawn webserver /comic
|
|
http.HandleFunc("/book", func(w http.ResponseWriter, r *http.Request) {
|
|
log.Print("/book")
|
|
getHTML("BOOK.mkd", w)
|
|
})
|
|
http.HandleFunc("/comic", func(w http.ResponseWriter, r *http.Request) {
|
|
log.Print("/comic")
|
|
getHTML("COMIC.mkd", w)
|
|
})
|
|
|
|
err := http.ListenAndServe(":9783", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|