buch_des_monats/cmd/buchdesmonats/main.go

106 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"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
}
var filename string
var force bool
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 main() {
// Get all items from the git repo
items := getItems(filename)
for _, item := range items {
_, err := os.Stat(item.targetFilename())
if os.IsNotExist(err) || force {
fmt.Printf("Downloading %v ...\n", item.imageURL())
err := item.downloadCover()
if err != nil {
fmt.Printf("ERROR: File %s not found\n", item.imageURL())
}
}
}
// TODO: use template to generate the restulting HTML
}
func init() {
flag.StringVar(&filename, "filename", "COMIC.mkd", "The filename to use")
flag.BoolVar(&force, "force", false, "Ignore cache, download all covers")
flag.Parse()
}