-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package crossref | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/lehigh-university-libraries/go-islandora/model" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
type Contributor struct { | ||
Name PersonName | ||
Suffix string | ||
Organization string | ||
Orcid string | ||
Role string | ||
Sequence string | ||
} | ||
|
||
type PersonName struct { | ||
Surname string `xml:"surname"` | ||
Given string `xml:"given_name,omitempty"` | ||
Institution string | ||
ORCID string | ||
} | ||
|
||
func GetContributor(name string, first bool) Contributor { | ||
contributor := Contributor{ | ||
Role: "author", | ||
} | ||
|
||
url := fmt.Sprintf("https://preserve.lehigh.edu/term_from_term_name?name=%s&vocab=person", url.QueryEscape(name)) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Fatalf("Error fetching URL: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
log.Fatalf("Error: received non-200 status code: %v", resp.StatusCode) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalf("Error reading response body: %v", err) | ||
} | ||
|
||
var c []model.TermResponse | ||
if err := json.Unmarshal(body, &c); err != nil { | ||
log.Fatalf("Error unmarshaling JSON: %v", err) | ||
} | ||
|
||
if len(c) > 0 { | ||
for _, r := range c[0].Relationships { | ||
if r.Url == "" { | ||
continue | ||
} | ||
if r.RelType != "schema:worksFor" { | ||
continue | ||
} | ||
|
||
relationshipUrl := fmt.Sprintf("https://preserve.lehigh.edu%s?_format=json", r.Url) | ||
respRel, err := http.Get(relationshipUrl) | ||
if err != nil { | ||
log.Fatalf("Error fetching relationship URL: %v", err) | ||
} | ||
defer respRel.Body.Close() | ||
|
||
if respRel.StatusCode != http.StatusOK { | ||
log.Fatalf("Error: received non-200 status code from relationships URL: %v", respRel.StatusCode) | ||
} | ||
|
||
bodyRel, err := io.ReadAll(respRel.Body) | ||
if err != nil { | ||
log.Fatalf("Error reading relationships response body: %v", err) | ||
} | ||
|
||
var cr model.TermResponse | ||
if err := json.Unmarshal(bodyRel, &cr); err != nil { | ||
log.Fatalf("Error unmarshaling relationships JSON: %v", err) | ||
} | ||
|
||
contributor.Name.Institution = cr.Name[0].Value | ||
name = strings.Replace(name, fmt.Sprintf(" - %s", contributor.Name.Institution), "", 1) | ||
} | ||
for _, i := range c[0].Identifier { | ||
if i.Attr0 != "orcid" { | ||
continue | ||
} | ||
|
||
contributor.Name.ORCID = i.Value | ||
} | ||
|
||
} | ||
nameComponents := strings.Split(name, ", ") | ||
surname := nameComponents[0] | ||
given := "" | ||
if len(nameComponents) > 1 { | ||
given = strings.Join(nameComponents[1:], ", ") | ||
} | ||
sequence := "additional" | ||
if first { | ||
sequence = "first" | ||
} | ||
contributor.Name.Given = html.EscapeString(given) | ||
contributor.Name.Surname = html.EscapeString(surname) | ||
contributor.Sequence = sequence | ||
return contributor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package model | ||
|
||
type TermResponse struct { | ||
ID IntField `json:"tid"` | ||
Name GenericField `json:"name"` | ||
Relationships TypedRelationField `json:"field_relationships"` | ||
Identifier TypedTextField `json:"field_identifier"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.