Compare commits

..

No commits in common. "8f9260b357463edcfcaaa6ce2bf811becec8d855" and "6110eddf68f384b758de5f14fcb07b6f869a7be9" have entirely different histories.

5 changed files with 21 additions and 37 deletions

7
.gitignore vendored
View file

@ -1,9 +1,2 @@
/data /data
/covers /covers
/bdm
/buch_des_monats.iml
/.idea/inspectionProfiles/Project_Default.xml
/.idea/.gitignore
/.idea/misc.xml
/.idea/modules.xml
/.idea/vcs.xml

4
go.mod
View file

@ -1,3 +1,5 @@
module okoyono.de/buchdesmonats module okoyono.de/buchdesmonats
go 1.20 go 1.16
require github.com/a-h/gemini v0.0.61

2
go.sum
View file

@ -0,0 +1,2 @@
github.com/a-h/gemini v0.0.61 h1:WSV9f5T6Vut9gIATUhqAh9KfsA4/kqcUIbTiDbsn1JY=
github.com/a-h/gemini v0.0.61/go.mod h1:p9wvIRDc2s3Lnbkw7CgNzDNgJHPuXDwh3dOF7w0NT8s=

View file

@ -2,13 +2,15 @@ package main
import ( import (
"fmt" "fmt"
//"html/template"
xmltpl "text/template"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"strings" "strings"
xmltpl "text/template"
) )
type Item struct { type Item struct {
@ -16,7 +18,7 @@ type Item struct {
Author string Author string
Title string Title string
Filename string Filename string
Date string Date string
} }
func (i Item) ImageURL() string { func (i Item) ImageURL() string {
@ -39,24 +41,14 @@ func (i Item) downloadCover() error {
return err return err
} }
defer func(Body io.ReadCloser) { defer resp.Body.Close()
err := Body.Close()
if err != nil {
log.Fatal("Can not close the response body")
}
}(resp.Body)
out, err := os.Create(i.targetFilename()) out, err := os.Create(i.targetFilename())
if err != nil { if err != nil {
return err return err
} }
defer func(out *os.File) { defer out.Close()
err := out.Close()
if err != nil {
log.Fatal("Can not close the file")
}
}(out)
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
@ -73,21 +65,16 @@ func getItems(filename string) []Item {
log.Fatal(filename + " is missing") log.Fatal(filename + " is missing")
} }
defer func(Body io.ReadCloser) { defer resp.Body.Close()
err := Body.Close()
if err != nil {
log.Fatal("Can not close the response body")
}
}(resp.Body)
content, err := io.ReadAll(resp.Body) content, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal("Can not download the file. Network problem?") log.Fatal("Can not download the file. Network problem?")
} }
currentYear := "" currentYear := ""
currentMonth := 0 currentMonth := 0
re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"]\([^=]+=(?P<isbn>[0-9]+).*$`) re := regexp.MustCompile(`^[^[]+ \[(?P<author>[^"]+)"(?P<title>[^"]+)"\]\([^=]+=(?P<isbn>[0-9]+).*$`)
yearRe := regexp.MustCompile(`^## (?P<year>20[0-9]{2})$`) yearRe := regexp.MustCompile(`^## (?P<year>20[0-9]{2})$`)
var yearBucket []Item var yearBucket []Item
@ -99,7 +86,7 @@ func getItems(filename string) []Item {
currentMonth = 0 currentMonth = 0
// Add the bucket in reverse order // Add the bucket in reverse order
for i := len(yearBucket) - 1; i >= 0; i-- { for i := len(yearBucket)-1; i >= 0; i-- {
items = append(items, yearBucket[i]) items = append(items, yearBucket[i])
} }
yearBucket = nil yearBucket = nil
@ -110,10 +97,10 @@ func getItems(filename string) []Item {
currentMonth++ currentMonth++
yearBucket = append(yearBucket, Item{ yearBucket = append(yearBucket, Item{
Author: strings.Trim(matches[1], " "), Author: strings.Trim(matches[1], " "),
Title: strings.Trim(matches[2], " "), Title: strings.Trim(matches[2], " "),
ISBN: matches[3], ISBN: matches[3],
Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear), Date: fmt.Sprintf("01-%02d-%s", currentMonth, currentYear),
Filename: filename, Filename: filename,
}) })
} }
@ -131,7 +118,7 @@ func getTemplate(sourceFile string, templateFilename string, w http.ResponseWrit
// Get all items from the git repo // Get all items from the git repo
items := getItems(sourceFile) items := getItems(sourceFile)
// Generate the resulting HTML // Generate the restulting HTML
t, err := xmltpl.ParseFiles("templates/" + templateFilename) t, err := xmltpl.ParseFiles("templates/" + templateFilename)
if err != nil { if err != nil {
panic(err) panic(err)

View file

@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="de"> <html>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>okoyono.de -- Buch des Monats</title> <title>okoyono.de -- Buch des Monats</title>