Support png file extension
This commit is contained in:
parent
8cb57383ad
commit
121081ea9c
7 changed files with 64 additions and 24 deletions
2
BOOK.mkd
2
BOOK.mkd
|
@ -202,3 +202,5 @@
|
|||
* 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)
|
||||
|
||||
## 2009
|
|
@ -89,3 +89,5 @@
|
|||
* 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)
|
||||
|
||||
## 2017
|
||||
|
|
56
src/bdm.go
56
src/bdm.go
|
@ -13,32 +13,46 @@ import (
|
|||
|
||||
type Item struct {
|
||||
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{
|
||||
ISBN: matches[3],
|
||||
FileExtension: extension,
|
||||
Author: strings.Trim(matches[1], " "),
|
||||
Title: strings.Trim(matches[2], " "),
|
||||
ISBN: matches[3],
|
||||
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
|
||||
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"))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
<div id="covers">
|
||||
{{ range .Items }}
|
||||
<div class="cover-item">
|
||||
<a target="_blank" href="https://www.buchhandel.de/buch/{{ .ISBN }}"><img src="/covers/{{ .ISBN }}.jpg" alt="{{ .Author | html }} - {{ .Title | html }}" title="zu buchhandel.de" /></a>
|
||||
<a target="_blank" href="https://www.buchhandel.de/buch/{{ .ISBN }}"><img src="/covers/{{ .ISBN }}.{{ .FileExtension }}" alt="{{ .Author | html }} - {{ .Title | html }}" title="zu buchhandel.de" /></a>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<summary>{{ .Author | html }} - "{{ .Title | html }}</summary>
|
||||
<content type="xhtml">
|
||||
<div xmlns="http://www.w3.org/1999/xhtml">
|
||||
<img src="https://buchdesmonats.okoyono.de/covers/{{ .ISBN }}.jpg"/>
|
||||
<img src="https://buchdesmonats.okoyono.de/covers/{{ .ISBN }}.{{ .FileExtension }}"/>
|
||||
</div>
|
||||
</content>
|
||||
</entry>
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
<div id="covers">
|
||||
{{ range .Items }}
|
||||
<div class="cover-item">
|
||||
<a target="_blank" href="https://www.buchhandel.de/buch/{{ .ISBN }}"><img src="/covers/{{ .ISBN }}.jpg" alt="{{ .Author | html }} - {{ .Title | html }}" title="zu buchhandel.de" /></a>
|
||||
<a target="_blank" href="https://www.buchhandel.de/buch/{{ .ISBN }}"><img src="/covers/{{ .ISBN }}.{{ .FileExtension }}" alt="{{ .Author | html }} - {{ .Title | html }}" title="zu buchhandel.de" /></a>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
<summary>{{ .Author | html }} - "{{ .Title | html }}"</summary>
|
||||
<content type="xhtml">
|
||||
<div xmlns="http://www.w3.org/1999/xhtml">
|
||||
<img src="https://comicdesmonats.okoyono.de/covers/{{ .ISBN }}.jpg"/>
|
||||
<img src="https://comicdesmonats.okoyono.de/covers/{{ .ISBN }}.{{ .FileExtension }}"/>
|
||||
</div>
|
||||
</content>
|
||||
</entry>
|
||||
|
|
Loading…
Reference in a new issue