Add import edition from inventaire
This commit is contained in:
@@ -43,7 +43,7 @@ func TestCallInventaireSearch_Offset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCallInventaireBook_BraveNewWorld(t *testing.T) {
|
||||
result, err := CallInventaireBook("Q191949", "fr")
|
||||
result, err := callInventaireBook("wd:Q191949", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -54,8 +54,8 @@ func TestCallInventaireBook_BraveNewWorld(t *testing.T) {
|
||||
assert.Equal(t, "écrivain, romancier et philosophe britannique (1894–1963)", result.Author.Description)
|
||||
}
|
||||
|
||||
func TestCallInventaireEdition_TestLimit(t *testing.T) {
|
||||
result, err := CallInventaireEdition("wd:Q339761", "fr", 10, 0)
|
||||
func TestCallInventaireEditionFromWork_TestLimit(t *testing.T) {
|
||||
result, err := CallInventaireEditionFromWork("wd:Q339761", "fr", 10, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -63,8 +63,8 @@ func TestCallInventaireEdition_TestLimit(t *testing.T) {
|
||||
assert.Equal(t, 10, len(result.Results))
|
||||
}
|
||||
|
||||
func TestCallInventaireEdition_TestOffset(t *testing.T) {
|
||||
result, err := CallInventaireEdition("wd:Q3213142", "fr", 0, 0)
|
||||
func TestCallInventaireEditionFromWork_TestOffset(t *testing.T) {
|
||||
result, err := CallInventaireEditionFromWork("wd:Q3213142", "fr", 0, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -101,3 +101,27 @@ func TestCallInventaireEdition_TestOffset(t *testing.T) {
|
||||
},
|
||||
result.Results[2])
|
||||
}
|
||||
|
||||
func TestCallInventaireEdition(t *testing.T) {
|
||||
result, err := CallInventaireEdition("isbn:9782266003698", "fr")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t,
|
||||
InventaireEditionDetailedSingleResult{
|
||||
Id: "isbn:9782266003698",
|
||||
Title: "Bel-Ami",
|
||||
Author: &InventaireAuthorResult{
|
||||
ID: "Q9327",
|
||||
Name: "Guy de Maupassant",
|
||||
Description: "écrivain et journaliste littéraire français (1850-1893)",
|
||||
},
|
||||
Description: "roman de Guy De Maupassant",
|
||||
ISBN: "978-2-266-00369-8",
|
||||
Publisher: "Pocket",
|
||||
ReleaseDate: "1977",
|
||||
Image: "https://inventaire.io/img/entities/b237c0b5de5f6c765928d9eee26b55804a33557a",
|
||||
Lang: "fr",
|
||||
},
|
||||
result)
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (i *InventaireBookResult) UnmarshalJSON(b []byte) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if inventaireEntity.WdId == i.ID {
|
||||
if ("wd:" + inventaireEntity.WdId) == i.ID {
|
||||
title, err := findLangageField(inventaireEntity.Labels, i.Lang)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -73,20 +73,28 @@ func (i *InventaireBookResult) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
|
||||
func findLangageField(multipleMessageFields map[string]json.RawMessage, lang string) (string, error) {
|
||||
fieldToParse, ok := multipleMessageFields[lang]
|
||||
if ok {
|
||||
var parsedField string
|
||||
err := json.Unmarshal(fieldToParse, &parsedField)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return parsedField, err
|
||||
} else {
|
||||
return "", errors.New("multi lang field could not be parsed")
|
||||
if len(multipleMessageFields) == 0 {
|
||||
return "", errors.New("empty multilang field")
|
||||
}
|
||||
|
||||
var fieldToParse json.RawMessage
|
||||
var ok bool
|
||||
fieldToParse, ok = multipleMessageFields[lang]
|
||||
if !ok {
|
||||
for _, field := range multipleMessageFields {
|
||||
fieldToParse = field
|
||||
break
|
||||
}
|
||||
}
|
||||
var parsedField string
|
||||
err := json.Unmarshal(fieldToParse, &parsedField)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return parsedField, err
|
||||
}
|
||||
|
||||
func CallInventaireBook(inventaireId string, lang string) (InventaireBookResult, error) {
|
||||
func callInventaireBook(inventaireId string, lang string) (InventaireBookResult, error) {
|
||||
queryResult := InventaireBookResult{ID: inventaireId, Lang: lang}
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
if err != nil {
|
||||
@@ -95,7 +103,7 @@ func CallInventaireBook(inventaireId string, lang string) (InventaireBookResult,
|
||||
callapiutils.AddQueryParam(u, "action", "by-uris")
|
||||
callapiutils.AddQueryParam(u, "relatives", "wdt:P50")
|
||||
callapiutils.AddQueryParam(u, "lang", lang)
|
||||
callapiutils.AddQueryParam(u, "uris", "wd:"+inventaireId)
|
||||
callapiutils.AddQueryParam(u, "uris", inventaireId)
|
||||
|
||||
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
||||
return queryResult, err
|
||||
|
||||
@@ -2,38 +2,19 @@ package inventaire
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"sort"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"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"`
|
||||
}
|
||||
|
||||
type inventaireEditionQueryResult struct {
|
||||
Entities []inventaireEditionQueryEntity
|
||||
}
|
||||
|
||||
type inventaireEditionQueryEntity struct {
|
||||
WdId string
|
||||
WorkId string
|
||||
EditionId string
|
||||
Title string
|
||||
ISBN string
|
||||
@@ -66,6 +47,10 @@ func (i *inventaireEditionQueryResult) UnmarshalJSON(b []byte) error {
|
||||
return err
|
||||
}
|
||||
if parsedEntity.Type == "edition" {
|
||||
workId, err := parseStringArrayFieldInJsonRaw(parsedEntity.Claims, "wdt:P629")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
editionId, err := parseStringArrayFieldInJsonRaw(parsedEntity.Claims, "wdt:P123")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -95,6 +80,7 @@ func (i *inventaireEditionQueryResult) UnmarshalJSON(b []byte) error {
|
||||
}
|
||||
i.Entities = append(i.Entities, inventaireEditionQueryEntity{
|
||||
WdId: parsedEntity.WdId,
|
||||
WorkId: workId,
|
||||
EditionId: editionId,
|
||||
Title: label,
|
||||
ISBN: isbn,
|
||||
@@ -125,80 +111,26 @@ func parseStringArrayFieldInJsonRaw(jsonRawMap map[string]json.RawMessage, key s
|
||||
return s, err
|
||||
}
|
||||
|
||||
func CallInventaireEdition(inventaireId string, lang string, limit int, offset int) (InventaireEditionResult, error) {
|
||||
var queryResult InventaireEditionResult
|
||||
uris, err := callInventaireUris(inventaireId)
|
||||
func callInventaireEditionEntities(uris []string) (inventaireEditionQueryResult, error) {
|
||||
var queryResult inventaireEditionQueryResult
|
||||
u, err := getInventaireEditionEntitiesUri(strings.Join(uris, "|"))
|
||||
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(inventaireId 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", inventaireId)
|
||||
|
||||
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
||||
return queryResult, err
|
||||
}
|
||||
|
||||
func callInventaireEditionEntities(uris []string) (inventaireEditionQueryResult, error) {
|
||||
var queryResult inventaireEditionQueryResult
|
||||
func getInventaireEditionEntitiesUri(uris string) (*url.URL, error) {
|
||||
u, err := computeInventaireApiUrl("entities")
|
||||
if err != nil {
|
||||
return queryResult, err
|
||||
return u, err
|
||||
}
|
||||
|
||||
callapiutils.AddQueryParam(u, "action", "by-uris")
|
||||
callapiutils.AddQueryParam(u, "uris", strings.Join(uris, "|"))
|
||||
callapiutils.AddQueryParam(u, "uris", uris)
|
||||
|
||||
err = callapiutils.FetchAndParseResult(u, &queryResult)
|
||||
|
||||
return queryResult, err
|
||||
return u, err
|
||||
}
|
||||
|
||||
type inventaireEditionPublisherResult struct {
|
||||
|
||||
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
|
||||
}
|
||||
62
internal/inventaire/inventaireeditionfromid.go
Normal file
62
internal/inventaire/inventaireeditionfromid.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package inventaire
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type InventaireEditionDetailedSingleResult struct {
|
||||
Id string
|
||||
Title string
|
||||
Description string
|
||||
Author *InventaireAuthorResult
|
||||
ISBN string
|
||||
Publisher string
|
||||
ReleaseDate string
|
||||
Image string
|
||||
Lang string
|
||||
}
|
||||
|
||||
func CallInventaireEdition(inventaireId string, lang string) (InventaireEditionDetailedSingleResult, error) {
|
||||
var result InventaireEditionDetailedSingleResult
|
||||
editionQueryResults, err := callInventaireEditionEntities([]string{inventaireId})
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
var editionQueryResult inventaireEditionQueryEntity
|
||||
if len(editionQueryResults.Entities) < 1 {
|
||||
return result, fmt.Errorf("No edition found on inventaire for id %s", inventaireId)
|
||||
}
|
||||
|
||||
editionQueryResult = editionQueryResults.Entities[0]
|
||||
|
||||
var publisher string
|
||||
if editionQueryResult.EditionId != "" {
|
||||
publisher, err = callInventairePublisherGetName(editionQueryResult.EditionId, lang)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
var author *InventaireAuthorResult
|
||||
var description string
|
||||
if editionQueryResult.WorkId != "" {
|
||||
workQueryResult, err := callInventaireBook(editionQueryResult.WorkId, lang)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
author = workQueryResult.Author
|
||||
description = workQueryResult.Description
|
||||
}
|
||||
result = InventaireEditionDetailedSingleResult{
|
||||
Id: editionQueryResult.Uri,
|
||||
Title: editionQueryResult.Title,
|
||||
Author: author,
|
||||
Description: description,
|
||||
ISBN: editionQueryResult.ISBN,
|
||||
Publisher: publisher,
|
||||
ReleaseDate: editionQueryResult.ReleaseDate,
|
||||
Image: editionQueryResult.Image,
|
||||
Lang: editionQueryResult.Lang,
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
Reference in New Issue
Block a user