From 121081ea9cc53840af06fc65b82477d117475048 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Thu, 4 Apr 2024 23:00:04 +0200 Subject: [PATCH] Support png file extension --- BOOK.mkd | 4 ++- COMIC.mkd | 4 ++- src/bdm.go | 72 +++++++++++++++++++++++++++++++++----------- templates/book.html | 2 +- templates/book.xml | 2 +- templates/comic.html | 2 +- templates/comic.xml | 2 +- 7 files changed, 64 insertions(+), 24 deletions(-) diff --git a/BOOK.mkd b/BOOK.mkd index 29dcd68..c7635d6 100644 --- a/BOOK.mkd +++ b/BOOK.mkd @@ -201,4 +201,6 @@ * Oktober: [Charles Stross "Du bist tot"](https://www.buchhandel.de/buch/9783453526877) * November: [Daniel Suarez "Daemon: Die Welt ist nur ein Spiel"](https://www.buchhandel.de/buch/9783499256431) -* Dezember: [William Gibson "Quellcode"](https://www.buchhandel.de/buch/9783453526808) \ No newline at end of file +* Dezember: [William Gibson "Quellcode"](https://www.buchhandel.de/buch/9783453526808) + +## 2009 \ No newline at end of file diff --git a/COMIC.mkd b/COMIC.mkd index ed2bda2..e6ccda1 100644 --- a/COMIC.mkd +++ b/COMIC.mkd @@ -88,4 +88,6 @@ * September: [Denis Bajram "Death Experience"](https://www.buchhandel.de/buch/9783958390270) * Oktober: [Christophe Arleston "Morea"](https://www.buchhandel.de/buch/9783939823902) * November: [Neil Gaiman "American Gods"](https://www.buchhandel.de/buch/9783962196004) -* Dezember: [Christophe Bec "Olympus Mons"](https://www.buchhandel.de/buch/9783962190200) \ No newline at end of file +* Dezember: [Christophe Bec "Olympus Mons"](https://www.buchhandel.de/buch/9783962190200) + +## 2017 diff --git a/src/bdm.go b/src/bdm.go index 38ddb82..978fd7f 100644 --- a/src/bdm.go +++ b/src/bdm.go @@ -12,33 +12,47 @@ import ( ) type Item struct { - ISBN string - Author string - Title string - Filename string - Date string + ISBN string + FileExtension string + Author string + Title string + Filename string + Date string } -func (i Item) ImageURL() string { +func (i Item) ImageURL(extension string) string { return "https://medien.umbreitkatalog.de/bildzentrale_original/" + i.ISBN[0:3] + "/" + i.ISBN[3:6] + "/" + i.ISBN[6:9] + "/" + i.ISBN[9:13] + - ".jpg" + "." + extension } -func (i Item) targetFilename() string { - return "covers/" + i.ISBN + ".jpg" +func (i Item) targetFilename(extension string) string { + return "covers/" + i.ISBN + "." + extension } func (i Item) downloadCover() error { - resp, err := http.Get(i.ImageURL()) + extension := "jpg" + + fmt.Printf("Downloading %v ...\n", i.ImageURL(extension)) + resp, err := http.Get(i.ImageURL(extension)) if err != nil { return err } + // TODO: refactor later ... + if resp.StatusCode == 404 { + extension = "png" + fmt.Printf("Downloading %v ...\n", i.ImageURL(extension)) + resp, err = http.Get(i.ImageURL(extension)) + if err != nil { + return err + } + } + defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { @@ -50,7 +64,7 @@ func (i Item) downloadCover() error { return fmt.Errorf("HTTP status code is %d", resp.StatusCode) } - out, err := os.Create(i.targetFilename()) + out, err := os.Create(i.targetFilename(extension)) if err != nil { return err } @@ -113,12 +127,18 @@ func getItems(filename string) []Item { if len(matches) == 4 { currentMonth++ + extension, err := getFileExtension(matches[3]) + if err != nil { + log.Printf("%v", err) + } + 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, + ISBN: matches[3], + FileExtension: extension, + Author: strings.Trim(matches[1], " "), + Title: strings.Trim(matches[2], " "), + Filename: filename, + Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear), }) } } @@ -131,6 +151,23 @@ func getItems(filename string) []Item { return items } +func getFileExtension(isbn string) (string, error) { + // List all files in covers directory + // TODO: Cache this line + files, err := os.ReadDir("covers/") + if err != nil { + log.Fatal("Can not read the covers directory") + return "", err + } + for _, file := range files { + if strings.HasPrefix(file.Name(), isbn) { + return strings.Split(file.Name(), ".")[1], nil + } + } + + return "", fmt.Errorf("File not found for ISBN: %v", isbn) +} + func getTemplate(sourceFile string, templateFilename string, w http.ResponseWriter) { // Get all items from the git repo items := getItems(sourceFile) @@ -167,12 +204,11 @@ func main() { items := getItems(filename) for _, item := range items { - fmt.Printf("Downloading %v ...\n", item.ImageURL()) err := item.downloadCover() if err != nil { fmt.Printf("%v", err) - fmt.Printf("ERROR: File %s not found\n", item.ImageURL()) + fmt.Printf("ERROR: File %s not found\n", item.ImageURL("jpg")) } } }) diff --git a/templates/book.html b/templates/book.html index 845fc32..bba8fd0 100644 --- a/templates/book.html +++ b/templates/book.html @@ -24,7 +24,7 @@
{{ range .Items }}
- {{ .Author | html }} - {{ .Title | html }} + {{ .Author | html }} - {{ .Title | html }}
{{ end }}
diff --git a/templates/book.xml b/templates/book.xml index a9de86b..70bf3cb 100644 --- a/templates/book.xml +++ b/templates/book.xml @@ -18,7 +18,7 @@ {{ .Author | html }} - "{{ .Title | html }}
- +
diff --git a/templates/comic.html b/templates/comic.html index cb44097..5889315 100644 --- a/templates/comic.html +++ b/templates/comic.html @@ -19,7 +19,7 @@
{{ range .Items }}
- {{ .Author | html }} - {{ .Title | html }} + {{ .Author | html }} - {{ .Title | html }}
{{ end }}
diff --git a/templates/comic.xml b/templates/comic.xml index b774916..e748ed9 100644 --- a/templates/comic.xml +++ b/templates/comic.xml @@ -18,7 +18,7 @@ {{ .Author | html }} - "{{ .Title | html }}"
- +