Add import edition from inventaire
This commit is contained in:
88
internal/inventaire/inventaireeditionfrombook.go
Normal file
88
internal/inventaire/inventaireeditionfrombook.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package inventaire
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/callapiutils"
|
||||
)
|
||||
|
||||
type InventaireEditionResult struct {
|
||||
Results []InventaireEditionResultBook `json:"results"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
type InventaireEditionResultBook struct {
|
||||
Id string `json:"uri"`
|
||||
Title string `json:"title"`
|
||||
ISBN string `json:"isbn"`
|
||||
Publisher string `json:"publisher"`
|
||||
ReleaseDate string `json:"date"`
|
||||
Image string `json:"image"`
|
||||
Lang string `json:"lang"`
|
||||
}
|
||||
|
||||
type inventaireReverseClaimsResult struct {
|
||||
Uris []string `json:"uris"`
|
||||
}
|
||||
|
||||
func CallInventaireEditionFromWork(workId string, lang string, limit int, offset int) (InventaireEditionResult, error) {
|
||||
var queryResult InventaireEditionResult
|
||||
uris, err := callInventaireUris(workId)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
queryResult.Count = int64(len(uris.Uris))
|
||||
sort.Strings(uris.Uris)
|
||||
limitedUris := uris.Uris
|
||||
if limit != 0 {
|
||||
l := len(uris.Uris)
|
||||
startIndex := int(math.Min(float64(offset), float64(l)))
|
||||
endIndex := int(math.Min(float64(limit+offset), float64(l)))
|
||||
limitedUris = uris.Uris[startIndex:endIndex]
|
||||
}
|
||||
editionEntities, err := callInventaireEditionEntities(limitedUris)
|
||||
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
sortedEntities := editionEntities.Entities
|
||||
sort.Slice(sortedEntities, func(i, j int) bool {
|
||||
return sortedEntities[i].Uri < sortedEntities[j].Uri
|
||||
})
|
||||
|
||||
for _, entity := range sortedEntities {
|
||||
publisher := ""
|
||||
if entity.EditionId != "" {
|
||||
publisher, err = callInventairePublisherGetName(entity.EditionId, lang)
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
}
|
||||
queryResult.Results = append(queryResult.Results, InventaireEditionResultBook{
|
||||
Id: entity.Uri,
|
||||
ISBN: entity.ISBN,
|
||||
Title: entity.Title,
|
||||
ReleaseDate: entity.ReleaseDate,
|
||||
Image: entity.Image,
|
||||
Publisher: publisher,
|
||||
Lang: entity.Lang,
|
||||
})
|
||||
}
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
func callInventaireUris(workId string) (inventaireReverseClaimsResult, error) {
|
||||
var queryResult inventaireReverseClaimsResult
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
}
|
||||
callapiutils.AddQueryParam(u, "action", "reverse-claims")
|
||||
callapiutils.AddQueryParam(u, "property", "wdt:P629")
|
||||
callapiutils.AddQueryParam(u, "value", workId)
|
||||
|
||||
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
||||
return queryResult, err
|
||||
}
|
||||
Reference in New Issue
Block a user