aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-04-27 21:37:47 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-04-27 21:37:47 +0200
commit47751477adae4260b8fde62abfea883a6cd31cd2 (patch)
treed137c333fa9ec7133301377a17ac74012ef2ce37
parentf001a9df685bf52fdedc4da979835bb51405bd8c (diff)
downloadrefcat-47751477adae4260b8fde62abfea883a6cd31cd2.tar.gz
refcat-47751477adae4260b8fde62abfea883a6cd31cd2.zip
wip: ol conversion and tests
-rw-r--r--skate/schema.go48
-rw-r--r--skate/schema_test.go147
2 files changed, 184 insertions, 11 deletions
diff --git a/skate/schema.go b/skate/schema.go
index 1dd02d9..a9b1e8a 100644
--- a/skate/schema.go
+++ b/skate/schema.go
@@ -4,6 +4,9 @@ import (
"fmt"
"strconv"
"strings"
+
+ "git.archive.org/martin/cgraph/skate/isbn"
+ "git.archive.org/martin/cgraph/skate/set"
)
// RefToRelease converts a ref to a release. Set a extra.skate.status flag to
@@ -81,15 +84,15 @@ type Release struct {
Role string `json:"role,omitempty"`
} `json:"contribs,omitempty"`
ExtIDs struct {
- DOI string `json:"doi,omitempty"`
- PMID string `json:"pmid,omitempty"`
- PMCID string `json:"pmcid,omitempty"`
- Arxiv string `json:"arxiv,omitempty"`
- Core string `json:"core,omitempty"`
- WikidataQID string `json:"wikidata_qid,omitempty"`
- Jstor string `json:"jstor,omitempty"`
- ISBN10 string `json:"isbn10,omitempty"`
- ISBN13 string `json:"isbn13,omitempty"`
+ Arxiv string `json:"arxiv,omitempty"`
+ Core string `json:"core,omitempty"`
+ DOI string `json:"doi,omitempty"`
+ ISBN []string `json:"isbn,omitempty"` // should be isbn13
+ Jstor string `json:"jstor,omitempty"`
+ OLID string `json:"olid,omitempty"`
+ PMCID string `json:"pmcid,omitempty"`
+ PMID string `json:"pmid,omitempty"`
+ WikidataQID string `json:"wikidata_qid,omitempty"`
} `json:"ext_ids,omitempty"`
Ident string `json:"ident,omitempty"`
Publisher string `json:"publisher,omitempty"`
@@ -140,6 +143,9 @@ type Release struct {
URL string `json:"url,omitempty"`
} `json:"rg,omitempty"`
} `json:"skate,omitempty"`
+ OpenLibrary struct {
+ HasFulltext bool `json:"has_fulltext,omitempty"`
+ } `json:"ol,omitempty"`
} `json:"extra,omitempty"`
}
@@ -356,6 +362,7 @@ func OpenLibraryToRelease(w *OpenLibraryWork) (*Release, error) {
RawName string `json:"raw_name,omitempty"`
Role string `json:"role,omitempty"`
}, len(w.AuthorName))
+ s = set.New()
)
for i, author := range w.AuthorName {
contribs[i].RawName = author
@@ -364,6 +371,25 @@ func OpenLibraryToRelease(w *OpenLibraryWork) (*Release, error) {
if len(w.PublishYear) > 0 {
release.ReleaseYearValue = w.FirstPublishYear
}
- // XXX: isbn normalization
- return nil, nil
+ for _, v := range w.Isbn {
+ switch {
+ case len(v) == 9 || len(v) == 10:
+ if w, err := isbn.To13(v); err == nil {
+ s.Add(w)
+ }
+ case len(v) == 12 || len(v) == 13:
+ if w, err := isbn.To13(v); err == nil {
+ s.Add(w)
+ }
+ default:
+ continue
+ }
+ }
+ if len(w.Publisher) > 0 {
+ release.Publisher = w.Publisher[0]
+ }
+ release.ExtIDs.ISBN = s.Slice()
+ release.ExtIDs.OLID = strings.Replace(w.Key, "/works/", "", 1)
+ release.Extra.OpenLibrary.HasFulltext = w.HasFulltext
+ return &release, nil
}
diff --git a/skate/schema_test.go b/skate/schema_test.go
new file mode 100644
index 0000000..6a95115
--- /dev/null
+++ b/skate/schema_test.go
@@ -0,0 +1,147 @@
+package skate
+
+import (
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "testing"
+
+ "github.com/nsf/jsondiff"
+)
+
+func TestOpenLibraryToRelease(t *testing.T) {
+ var cases = []struct {
+ work OpenLibraryWork
+ release Release
+ err error
+ }{
+ {
+ work: OpenLibraryWork{},
+ release: Release{
+ ExtIDs: struct {
+ Arxiv string `json:"arxiv,omitempty"`
+ Core string `json:"core,omitempty"`
+ DOI string `json:"doi,omitempty"`
+ ISBN []string `json:"isbn,omitempty"`
+ Jstor string `json:"jstor,omitempty"`
+ OLID string `json:"olid,omitempty"`
+ PMCID string `json:"pmcid,omitempty"`
+ PMID string `json:"pmid,omitempty"`
+ WikidataQID string `json:"wikidata_qid,omitempty"`
+ }{},
+ },
+ err: nil,
+ },
+ {
+ work: OpenLibraryWork{
+ Title: "Hello World",
+ Isbn: []string{
+ "2844273386",
+ "9782844273383",
+ },
+ },
+ release: Release{
+ ExtIDs: struct {
+ Arxiv string `json:"arxiv,omitempty"`
+ Core string `json:"core,omitempty"`
+ DOI string `json:"doi,omitempty"`
+ ISBN []string `json:"isbn,omitempty"`
+ Jstor string `json:"jstor,omitempty"`
+ OLID string `json:"olid,omitempty"`
+ PMCID string `json:"pmcid,omitempty"`
+ PMID string `json:"pmid,omitempty"`
+ WikidataQID string `json:"wikidata_qid,omitempty"`
+ }{
+ ISBN: []string{"9782844273383"},
+ },
+ Title: "Hello World",
+ },
+ err: nil,
+ },
+ {
+ work: OpenLibraryWork{
+ Key: "/works/OL10000003W",
+ Title: "Hello World",
+ Isbn: []string{
+ "2844273386",
+ "9782844273383",
+ },
+ HasFulltext: false,
+ },
+ release: Release{
+ ExtIDs: struct {
+ Arxiv string `json:"arxiv,omitempty"`
+ Core string `json:"core,omitempty"`
+ DOI string `json:"doi,omitempty"`
+ ISBN []string `json:"isbn,omitempty"`
+ Jstor string `json:"jstor,omitempty"`
+ OLID string `json:"olid,omitempty"`
+ PMCID string `json:"pmcid,omitempty"`
+ PMID string `json:"pmid,omitempty"`
+ WikidataQID string `json:"wikidata_qid,omitempty"`
+ }{
+ ISBN: []string{"9782844273383"},
+ OLID: "OL10000003W",
+ },
+ Title: "Hello World",
+ Extra: struct {
+ ContainerName string `json:"container_name,omitempty"`
+ SubtitleValue interface{} `json:"subtitle,omitempty"` // []str or str
+ Crossref struct {
+ Type string `json:"type,omitempty"`
+ } `json:"crossref,omitempty"`
+ DataCite struct {
+ MetadataVersion int `json:"metadataVersion,omitempty"`
+ Relations []DataCiteRelation `json:"relations,omitempty"`
+ } `json:"datacite,omitempty"`
+ Skate struct {
+ // Mark as converted (e.g. by setting status to "ref")
+ Status string `json:"status,omitempty"`
+ // Carry the ref index and key around.
+ Ref struct {
+ Index int64 `json:"index,omitempty"`
+ Key string `json:"key,omitempty"`
+ Locator string `json:"locator,omitempty"`
+ } `json:"ref,omitempty"`
+ ResearchGate struct {
+ URL string `json:"url,omitempty"`
+ } `json:"rg,omitempty"`
+ } `json:"skate,omitempty"`
+ OpenLibrary struct {
+ HasFulltext bool `json:"has_fulltext,omitempty"`
+ } `json:"ol,omitempty"`
+ }{
+ OpenLibrary: struct {
+ HasFulltext bool `json:"has_fulltext,omitempty"`
+ }{
+ HasFulltext: false,
+ },
+ },
+ },
+ err: nil,
+ },
+ }
+ for _, c := range cases {
+ r, err := OpenLibraryToRelease(&c.work)
+ if err != nil {
+ t.Fatalf("got %v, want %v", err, c.err)
+ }
+ if !reflect.DeepEqual(r, &c.release) {
+ t.Fatalf(prettyStructDiff(r, &c.release))
+ }
+ }
+}
+
+func prettyStructDiff(s, t interface{}) string {
+ b, err := json.MarshalIndent(s, "", " ")
+ if err != nil {
+ return fmt.Sprintf("diff failed: %v", err)
+ }
+ c, err := json.MarshalIndent(t, "", " ")
+ if err != nil {
+ return fmt.Sprintf("diff failed: %v", err)
+ }
+ opts := jsondiff.DefaultConsoleOptions()
+ _, d := jsondiff.Compare(b, c, &opts)
+ return d
+}