We are now a webserver with a webhook :P
This commit is contained in:
parent
646659afb8
commit
22d36d5a89
11 changed files with 90 additions and 68 deletions
|
@ -1,10 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -15,7 +16,7 @@ type Item struct {
|
||||||
Filename string
|
Filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) imageURL() string {
|
func (i Item) ImageURL() string {
|
||||||
return "https://medien.ubitweb.de/bildzentrale_original/" +
|
return "https://medien.ubitweb.de/bildzentrale_original/" +
|
||||||
i.ISBN[0:3] + "/" +
|
i.ISBN[0:3] + "/" +
|
||||||
i.ISBN[3:6] + "/" +
|
i.ISBN[3:6] + "/" +
|
||||||
|
@ -29,7 +30,7 @@ func (i Item) targetFilename() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) downloadCover() error {
|
func (i Item) downloadCover() error {
|
||||||
resp, err := http.Get(i.imageURL())
|
resp, err := http.Get(i.ImageURL())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -49,9 +50,6 @@ func (i Item) downloadCover() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var filename string
|
|
||||||
var force bool
|
|
||||||
|
|
||||||
func getItems(filename string) []Item {
|
func getItems(filename string) []Item {
|
||||||
var items []Item
|
var items []Item
|
||||||
// Get all book URLS
|
// Get all book URLS
|
||||||
|
@ -79,27 +77,60 @@ func getItems(filename string) []Item {
|
||||||
return items
|
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() {
|
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
|
// Get all items from the git repo
|
||||||
items := getItems(filename)
|
items := getItems(filename)
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
_, err := os.Stat(item.targetFilename())
|
fmt.Printf("Downloading %v ...\n", item.ImageURL())
|
||||||
if os.IsNotExist(err) || force {
|
|
||||||
fmt.Printf("Downloading %v ...\n", item.imageURL())
|
|
||||||
err := item.downloadCover()
|
err := item.downloadCover()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR: File %s not found\n", item.imageURL())
|
fmt.Printf("ERROR: File %s not found\n", item.ImageURL())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// TODO: use template to generate the restulting HTML
|
// 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)
|
||||||
|
})
|
||||||
|
|
||||||
func init() {
|
err := http.ListenAndServe(":1337", nil)
|
||||||
flag.StringVar(&filename, "filename", "COMIC.mkd", "The filename to use")
|
if err != nil {
|
||||||
flag.BoolVar(&force, "force", false, "Ignore cache, download all covers")
|
panic(err)
|
||||||
flag.Parse()
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>okoyono.de -- Comic des Monats</title>
|
|
||||||
<link rel="stylesheet" href="comic.css" />
|
|
||||||
<script src="vendors/jquery-2.1.0.min.js"></script>
|
|
||||||
<script src="vendors/masonry-3.1.5.pkgd.min.js"></script>
|
|
||||||
<script src="vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
|
|
||||||
<script src="main.js"></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Comic des Monats</h1>
|
|
||||||
<p>Neues Projekt: Eine Sammlung mit Comics die <a href="https://social.okoyono.de/@mezzo" rel="author">Michael
|
|
||||||
Reutter</a> für Empfehlenswert hält:</p><p>
|
|
||||||
Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu <a href="http://mojoreads.de/">Mojoreads</a>, <a href="https://git.okoyono.de/mezzo/buch_des_monats">der
|
|
||||||
Code</a> von <a href="https://aaron-fischer.net/">Aaron Fischer</a>. Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
|
|
||||||
|
|
||||||
*Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)*
|
|
||||||
|
|
||||||
<div id="covers">
|
|
||||||
<div class="cover-item">
|
|
||||||
<a href="#"><img src="" alt="Mojoreads cover" title="zu Mojoreads" /></a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,14 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>okoyono.de -- Buch des Monats</title>
|
|
||||||
<link rel="stylesheet" href="book.css" />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Buch des Monats</h1>
|
|
||||||
|
|
||||||
<a href="comic.html">Comic des Monats</a>
|
|
||||||
<a href="book.html">Buch des Monats</a>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -3,11 +3,11 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>okoyono.de -- Buch des Monats</title>
|
<title>okoyono.de -- Buch des Monats</title>
|
||||||
<link rel="stylesheet" href="book.css" />
|
<link rel="stylesheet" href="/static/book.css" />
|
||||||
<script src="vendors/jquery-2.1.0.min.js"></script>
|
<script src="/static/vendors/jquery-2.1.0.min.js"></script>
|
||||||
<script src="vendors/masonry-3.1.5.pkgd.min.js"></script>
|
<script src="/static/vendors/masonry-3.1.5.pkgd.min.js"></script>
|
||||||
<script src="vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
|
<script src="/static/vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
|
||||||
<script src="main.js"></script>
|
<script src="/static/main.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Buch des Monats</h1>
|
<h1>Buch des Monats</h1>
|
||||||
|
@ -21,9 +21,11 @@
|
||||||
Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
|
Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
|
||||||
|
|
||||||
<div id="covers">
|
<div id="covers">
|
||||||
|
{{ range . }}
|
||||||
<div class="cover-item">
|
<div class="cover-item">
|
||||||
<a href="#"><img src="" alt="Mojoreads cover" title="zu Mojoreads" /></a>
|
<a href="https://mojoreads.de/book/?isbn={{ .ISBN }}&ref=mezzo&aff=mr"><img src="/covers/{{ .ISBN }}.jpg" alt="Mojoreads cover" title="zu Mojoreads" /></a>
|
||||||
</div>
|
</div>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
30
templates/COMIC.mkd.html
Normal file
30
templates/COMIC.mkd.html
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>okoyono.de -- Comic des Monats</title>
|
||||||
|
<link rel="stylesheet" href="/static/comic.css" />
|
||||||
|
<script src="/static/vendors/jquery-2.1.0.min.js"></script>
|
||||||
|
<script src="/static/vendors/masonry-3.1.5.pkgd.min.js"></script>
|
||||||
|
<script src="/static/vendors/imagesloaded-3.1.4.pkgd.min.js"></script>
|
||||||
|
<script src="/static/main.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Comic des Monats</h1>
|
||||||
|
<p>Neues Projekt: Eine Sammlung mit Comics die <a href="https://social.okoyono.de/@mezzo" rel="author">Michael
|
||||||
|
Reutter</a> für Empfehlenswert hält. Jeden Monat ein neuer Comic aus seiner Sammlung. Die Buchlinks gehen zu <a href="http://mojoreads.de/">Mojoreads</a>, <a href="https://git.okoyono.de/mezzo/buch_des_monats">der
|
||||||
|
Code</a> von <a href="https://aaron-fischer.net/">Aaron Fischer</a>. Ein <a href="https://okoyono.de/">økoyono</a> Projekt.</p>
|
||||||
|
|
||||||
|
*Eine Seite mit Buchempfehlungen findet Ihr [hier](https://buchdesmonats.okoyono.de)*
|
||||||
|
|
||||||
|
<div id="covers">
|
||||||
|
<div class="cover-item">
|
||||||
|
{{ range . }}
|
||||||
|
<div class="cover-item">
|
||||||
|
<a href="https://mojoreads.de/book/?isbn={{ .ISBN }}&ref=mezzo&aff=mr"><img src="/covers/{{ .ISBN }}.jpg" alt="Mojoreads cover" title="zu Mojoreads" /></a>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue