adding fetching and saving an entire book
This commit is contained in:
@@ -7,6 +7,7 @@ import(
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"strconv"
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func FetchChapter(cfg *config.Cfg , searchedChapter string) ( error){
|
||||
@@ -33,12 +34,13 @@ func FetchChapter(cfg *config.Cfg , searchedChapter string) ( error){
|
||||
|
||||
chapterNum, err := strconv.Atoi(searchedChapter)
|
||||
if err != nil{
|
||||
log.Printf("searched chapter %v is not a valid number%v", searchedChapter, err)
|
||||
log.Printf("searched chapter %v is not a valid number - %v", searchedChapter, err)
|
||||
}
|
||||
|
||||
chapterText := config.Chapter{
|
||||
Number: chapterNum,
|
||||
}
|
||||
|
||||
replacer := strings.NewReplacer(
|
||||
"L'Éternel", "YAHWEH",
|
||||
"l'Éternel", "YAHWEH",
|
||||
@@ -65,22 +67,35 @@ func FetchChapter(cfg *config.Cfg , searchedChapter string) ( error){
|
||||
|
||||
chapterText = StripAlternativeVersification(chapterText)
|
||||
cfg.Book.Chapters = append(cfg.Book.Chapters, chapterText)
|
||||
|
||||
err= SaveChapter(*cfg, &chapterText)
|
||||
if err != nil{
|
||||
log.Printf("Error saving chapter %v in yaml format.", chapterNum)
|
||||
return fmt.Errorf("Error saving chapter %v in yaml format.", chapterNum)
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func fecthBook (book, version string, chapterNumbers int) book, error{
|
||||
failedChapter := 0
|
||||
bookChapters := book{}
|
||||
|
||||
for i:= 1 ; i > chapterNumbers ; i++{
|
||||
verses, err := fetchChapter(book, strconv.Itoa(i), version)
|
||||
func FetchBook (cfg *config.Cfg) error{
|
||||
|
||||
chapterNum, exist := cfg.BookList[cfg.Book.Name]
|
||||
|
||||
if !exist {
|
||||
log.Printf("%v is not a Bible book or has not been inserted yet.", cfg.Book.Name)
|
||||
return fmt.Errorf("%v is not a Bible book or has not been inserted yet.", cfg.Book.Name)
|
||||
}
|
||||
|
||||
for i:= 1 ; i <= chapterNum ; i++{
|
||||
err := FetchChapter(cfg, strconv.Itoa(i))
|
||||
if err != nil {
|
||||
failedChapter++
|
||||
log.Printf("%s", err)
|
||||
log.Printf("Warning : chapter %v has not been fetched: %v", i, err)
|
||||
continue
|
||||
}
|
||||
book.chapters = append(book,
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
59
internal/fetcher/savechapter.go
Normal file
59
internal/fetcher/savechapter.go
Normal 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("can’t remove %v: %v", fileName, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = os.WriteFile(fileName, yamlData, 0600)
|
||||
if err != nil{
|
||||
log.Printf("can’t write into %v", fileName)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ func StripAlternativeVersification(rawChapter config.Chapter) config.Chapter{
|
||||
regExp:=regexp.MustCompile(`\(\d+:\d+\)`)
|
||||
|
||||
var treatedChapter config.Chapter
|
||||
treatedChapter.Number = rawChapter.Number
|
||||
for _, verse := range rawChapter.Verses{
|
||||
modifiedVerse := string(regExp.ReplaceAll([]byte(verse), []byte("")))
|
||||
log.Printf("%v\n", modifiedVerse)
|
||||
@@ -29,7 +30,7 @@ func StripAlternativeVersification(rawChapter config.Chapter) config.Chapter{
|
||||
}
|
||||
|
||||
func SplitInPart(verse string, properName map[string]struct{})([]string){
|
||||
|
||||
|
||||
var parts []string
|
||||
currentPart := ""
|
||||
words := strings.Split(strings.TrimSpace(verse), " ")
|
||||
@@ -39,7 +40,7 @@ func SplitInPart(verse string, properName map[string]struct{})([]string){
|
||||
for i :=0 ; i < wordsCount ; i++{
|
||||
word := words[i]
|
||||
r, _ := utf8.DecodeRuneInString(word)
|
||||
trimedWord := strings.TrimRight(word, ".,")
|
||||
trimedWord := strings.TrimRight(word, ".,!;:")
|
||||
_, isProperName := properName[trimedWord]
|
||||
if i != 0 && unicode.IsUpper(r) && !isProperName{
|
||||
parts = append(parts, currentPart)
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package models
|
||||
|
||||
type yaml struct {
|
||||
Strophes []struct {
|
||||
Lines []struct {
|
||||
Verse int `yaml:"verse"`
|
||||
Tet string `yaml:"text"`
|
||||
Who string `yaml:"who"`
|
||||
To string `yaml:"to"`
|
||||
} `yaml:"lines"`
|
||||
} `yaml:"strophes"`
|
||||
type Yaml struct {
|
||||
Strophes []Strophe `yaml:"strophes"`
|
||||
}
|
||||
|
||||
type Strophe struct {
|
||||
Lines []Line `yaml:"lines"`
|
||||
}
|
||||
|
||||
type Line struct {
|
||||
Verse int `yaml:"verse"`
|
||||
Text string `yaml:"text"`
|
||||
Who string `yaml:"who"`
|
||||
To string `yaml:"to"`
|
||||
Of string `yaml:"Of"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user