adding fetching and saving an entire book

This commit is contained in:
github_username_here
2026-03-10 17:48:17 +01:00
parent 7355158ff9
commit 874ba49512
157 changed files with 31768 additions and 32 deletions

View File

@@ -0,0 +1,59 @@
package fetcher
import (
"log"
"project.hechon.fr/internal/config"
"project.hechon.fr/internal/models"
"gopkg.in/yaml.v3"
"os"
"path"
"strconv"
)
func SaveChapter(cfg config.Cfg, chapter *config.Chapter) (error){
var chapterYaml models.Yaml
for i, verse:= range chapter.Verses{
strophe := models.Strophe{}
parts := SplitInPart(verse, cfg.ProperName)
for _, part := range parts{
line := models.Line{
Verse: i+1,
Text: part,
Who: "",
To: "",
Of: "",
}
strophe.Lines = append(strophe.Lines, line)
}
chapterYaml.Strophes = append(chapterYaml.Strophes, strophe)
log.Printf("%v", parts)
}
yamlData, err := yaml.Marshal(chapterYaml)
if err != nil{
log.Printf("failed unpacking this yaml struct:%v", chapterYaml)
return err
}
fileName := path.Join(cfg.YamlPath, cfg.Book.Name + strconv.Itoa(chapter.Number) + ".yaml")
_,err = os.Stat(fileName)
if err == nil {
err = os.Remove(fileName)
if err != nil {
log.Printf("cant remove %v: %v", fileName, err)
return err
}
}
err = os.WriteFile(fileName, yamlData, 0600)
if err != nil{
log.Printf("cant write into %v", fileName)
return err
}
return nil
}