Add a new config to scrap description from babelio
This commit is contained in:
@@ -2,10 +2,14 @@ package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/appcontext"
|
||||
"git.artlef.fr/bibliomane/internal/babelio"
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/fileutils"
|
||||
"git.artlef.fr/bibliomane/internal/i18nresource"
|
||||
"git.artlef.fr/bibliomane/internal/inventaire"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"git.artlef.fr/bibliomane/internal/myvalidator"
|
||||
@@ -62,10 +66,40 @@ func saveInventaireBookToDb(ac appcontext.AppContext, inventaireEdition inventai
|
||||
}
|
||||
book.Cover = cover
|
||||
}
|
||||
|
||||
if ac.Config.BookDescriptionFromBabelio {
|
||||
isbn := findIsbn(&inventaireEdition)
|
||||
if isbn != "" {
|
||||
desc, err := babelio.GetDescriptionFromISBN(ac.Config.BabelioUrl, isbn)
|
||||
if err != nil {
|
||||
te, isTrError := errors.AsType[myvalidator.TranslatedError](err)
|
||||
var errToPrint string
|
||||
if isTrError {
|
||||
errToPrint = i18nresource.GetTranslatedMessage(&ac, te.Error())
|
||||
} else {
|
||||
errToPrint = err.Error()
|
||||
}
|
||||
log.Println(errToPrint)
|
||||
} else {
|
||||
book.Summary = desc
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := ac.Db.Save(&book).Error
|
||||
return &book, err
|
||||
}
|
||||
|
||||
func findIsbn(inventaireEdition *inventaire.InventaireEditionDetailedSingleResult) string {
|
||||
if inventaireEdition.ISBN != "" {
|
||||
return strings.ReplaceAll(inventaireEdition.ISBN, "-", "")
|
||||
}
|
||||
if strings.HasPrefix(inventaireEdition.Id, "isbn:") {
|
||||
return inventaireEdition.Id[5:]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func fetchOrCreateInventaireAuthor(ac appcontext.AppContext, inventaireAuthor *inventaire.InventaireAuthorResult) (*model.Author, error) {
|
||||
var author model.Author
|
||||
res := ac.Db.Where("inventaire_id = ?", inventaireAuthor.ID).First(&author)
|
||||
|
||||
Reference in New Issue
Block a user