refactoring project structure
This commit is contained in:
83
internal/fetcher/fetchhtml.go
Normal file
83
internal/fetcher/fetchhtml.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import(
|
||||
"log"
|
||||
"net/http"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
type chapter struct{
|
||||
verses []string
|
||||
}
|
||||
|
||||
type book struct{
|
||||
chapters []chapter
|
||||
}
|
||||
|
||||
func fetchChapter(book, searchedChapter, version string) (chapter, error){
|
||||
|
||||
url := "https://www.biblegateway.com/passage/?search=" + book + "%20" + searchedChapter +"&version=" + version
|
||||
|
||||
res, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Printf("can’t get %s : %v\n", url, err)
|
||||
return chapter{}, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
log.Printf("error code %v: %v\n", res.StatusCode, res.Status)
|
||||
return chapter{}, err
|
||||
}
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||
if err != nil {
|
||||
log.Printf("error reading the http response: %v\n", err)
|
||||
return chapter{}, err
|
||||
}
|
||||
|
||||
chapterText := chapter{}
|
||||
replacer := strings.NewReplacer(
|
||||
"L'Éternel", "YAHWEH",
|
||||
"l'Éternel", "YAHWEH",
|
||||
"Éternel", "YAHWEH",
|
||||
)
|
||||
|
||||
doc.Find(".verse > span").Each(func(i int, s *goquery.Selection){
|
||||
/**********************************************************************************/
|
||||
/**** verses are marked by an .verse class. Inside the text is in a span and *****/
|
||||
/**** begin either by the chapter number (first verse), or by the verse *****/
|
||||
/**** (the other verses). With that the code is straight forward. *****/
|
||||
/**********************************************************************************/
|
||||
rawText := s.Text()
|
||||
verse:= ""
|
||||
if i == 0 {
|
||||
verse, _ = strings.CutPrefix(rawText, searchedChapter )
|
||||
}else{
|
||||
|
||||
verse, _ = strings.CutPrefix(rawText, strconv.Itoa(i+1))
|
||||
}
|
||||
verse = replacer.Replace(verse)
|
||||
chapterText.verses = append(chapterText.verses, strings.TrimSpace(verse))
|
||||
})
|
||||
|
||||
return chapterText, 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)
|
||||
if err != nil {
|
||||
failedChapter++
|
||||
log.Printf("%s", err)
|
||||
continue
|
||||
}
|
||||
book.chapters = append(book,
|
||||
|
||||
}
|
||||
*/
|
||||
50
internal/fetcher/textmanipulation.go
Normal file
50
internal/fetcher/textmanipulation.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import(
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func stripAlternativeVersification(rawChapter chapter) chapter{
|
||||
regExp:=regexp.MustCompile(`\(\d+:\d+\)`)
|
||||
|
||||
var treatedChapter chapter
|
||||
for _, verse := range rawChapter.verses{
|
||||
modifiedVerse := string(regExp.ReplaceAll([]byte(verse), []byte("")))
|
||||
log.Printf("%v\n", modifiedVerse)
|
||||
treatedChapter.verses = append(treatedChapter.verses, modifiedVerse)
|
||||
}
|
||||
|
||||
return treatedChapter
|
||||
}
|
||||
|
||||
func splitInPart(verse string, properName map[string]struct{})(){
|
||||
|
||||
currentPart := ""
|
||||
words := strings.Split(strings.TrimSpace(verse), " ")
|
||||
wordsCount := len(words)
|
||||
isTreated := false
|
||||
|
||||
for i :=0 ; i < wordsCount ; i++{
|
||||
word := words[i]
|
||||
r, _ := utf8.DecodeRuneInString(word)
|
||||
trimedWord := strings.TrimRight(word, ".,")
|
||||
_, isProperName := properName[trimedWord]
|
||||
if i != 0 && unicode.IsUpper(r) && !isProperName{
|
||||
log.Printf("%s", currentPart)
|
||||
currentPart = word
|
||||
isTreated = true
|
||||
}else{
|
||||
currentPart += " " + word
|
||||
isTreated = false
|
||||
}
|
||||
}
|
||||
|
||||
if !isTreated {
|
||||
log.Printf("%s", currentPart)
|
||||
}
|
||||
|
||||
}
|
||||
12
internal/models/yaml.go
Normal file
12
internal/models/yaml.go
Normal file
@@ -0,0 +1,12 @@
|
||||
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"`
|
||||
}
|
||||
Reference in New Issue
Block a user