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)
|
* 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)
|
* 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)
|
* 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)
|
* Oktober: [Christophe Arleston "Morea"](https://www.buchhandel.de/buch/9783939823902)
|
||||||
* November: [Neil Gaiman "American Gods"](https://www.buchhandel.de/buch/9783962196004)
|
* November: [Neil Gaiman "American Gods"](https://www.buchhandel.de/buch/9783962196004)
|
||||||
* Dezember: [Christophe Bec "Olympus Mons"](https://www.buchhandel.de/buch/9783962190200)
|
* Dezember: [Christophe Bec "Olympus Mons"](https://www.buchhandel.de/buch/9783962190200)
|
||||||
|
|
||||||
|
## 2017
|
||||||
|
|
72
src/bdm.go
72
src/bdm.go
|
@ -12,33 +12,47 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ISBN string
|
ISBN string
|
||||||
Author string
|
FileExtension string
|
||||||
Title string
|
Author string
|
||||||
Filename string
|
Title string
|
||||||
Date string
|
Filename string
|
||||||
|
Date string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) ImageURL() string {
|
func (i Item) ImageURL(extension string) string {
|
||||||
return "https://medien.umbreitkatalog.de/bildzentrale_original/" +
|
return "https://medien.umbreitkatalog.de/bildzentrale_original/" +
|
||||||
i.ISBN[0:3] + "/" +
|
i.ISBN[0:3] + "/" +
|
||||||
i.ISBN[3:6] + "/" +
|
i.ISBN[3:6] + "/" +
|
||||||
i.ISBN[6:9] + "/" +
|
i.ISBN[6:9] + "/" +
|
||||||
i.ISBN[9:13] +
|
i.ISBN[9:13] +
|
||||||
".jpg"
|
"." + extension
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) targetFilename() string {
|
func (i Item) targetFilename(extension string) string {
|
||||||
return "covers/" + i.ISBN + ".jpg"
|
return "covers/" + i.ISBN + "." + extension
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i Item) downloadCover() error {
|
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 {
|
if err != nil {
|
||||||
return err
|
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) {
|
defer func(Body io.ReadCloser) {
|
||||||
err := Body.Close()
|
err := Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,7 +64,7 @@ func (i Item) downloadCover() error {
|
||||||
return fmt.Errorf("HTTP status code is %d", resp.StatusCode)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -113,12 +127,18 @@ func getItems(filename string) []Item {
|
||||||
if len(matches) == 4 {
|
if len(matches) == 4 {
|
||||||
currentMonth++
|
currentMonth++
|
||||||
|
|
||||||
|
extension, err := getFileExtension(matches[3])
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
yearBucket = append(yearBucket, Item{
|
yearBucket = append(yearBucket, Item{
|
||||||
Author: strings.Trim(matches[1], " "),
|
ISBN: matches[3],
|
||||||
Title: strings.Trim(matches[2], " "),
|
FileExtension: extension,
|
||||||
ISBN: matches[3],
|
Author: strings.Trim(matches[1], " "),
|
||||||
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
|
Title: strings.Trim(matches[2], " "),
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
|
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,6 +151,23 @@ func getItems(filename string) []Item {
|
||||||
return items
|
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) {
|
func getTemplate(sourceFile string, templateFilename string, w http.ResponseWriter) {
|
||||||
// Get all items from the git repo
|
// Get all items from the git repo
|
||||||
items := getItems(sourceFile)
|
items := getItems(sourceFile)
|
||||||
|
@ -167,12 +204,11 @@ func main() {
|
||||||
items := getItems(filename)
|
items := getItems(filename)
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
fmt.Printf("Downloading %v ...\n", item.ImageURL())
|
|
||||||
err := item.downloadCover()
|
err := item.downloadCover()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%v", err)
|
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">
|
<div id="covers">
|
||||||
{{ range .Items }}
|
{{ range .Items }}
|
||||||
<div class="cover-item">
|
<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>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
<summary>{{ .Author | html }} - "{{ .Title | html }}</summary>
|
<summary>{{ .Author | html }} - "{{ .Title | html }}</summary>
|
||||||
<content type="xhtml">
|
<content type="xhtml">
|
||||||
<div xmlns="http://www.w3.org/1999/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>
|
</div>
|
||||||
</content>
|
</content>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
<div id="covers">
|
<div id="covers">
|
||||||
{{ range .Items }}
|
{{ range .Items }}
|
||||||
<div class="cover-item">
|
<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>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
<summary>{{ .Author | html }} - "{{ .Title | html }}"</summary>
|
<summary>{{ .Author | html }} - "{{ .Title | html }}"</summary>
|
||||||
<content type="xhtml">
|
<content type="xhtml">
|
||||||
<div xmlns="http://www.w3.org/1999/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>
|
</div>
|
||||||
</content>
|
</content>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
Loading…
Reference in a new issue