Skip to content

Commit

Permalink
support rogue scholar legacy database
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 17, 2024
1 parent bcf0124 commit e362e87
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ commonmeta push --sample -f crossref -t inveniordm -h rogue-scholar.org --token
to, _ := cmd.Flags().GetString("to")
host, _ := cmd.Flags().GetString("host")
token, _ := cmd.Flags().GetString("token")
legacyKey, _ := cmd.Flags().GetString("legacyKey")

cmd.SetOut(os.Stdout)
cmd.SetErr(os.Stderr)
Expand Down Expand Up @@ -97,7 +98,7 @@ commonmeta push --sample -f crossref -t inveniordm -h rogue-scholar.org --token

var output []byte
if to == "inveniordm" {
output, err = inveniordm.UpsertAll(data, host, token)
output, err = inveniordm.UpsertAll(data, host, token, legacyKey)
} else {
fmt.Println("Please provide a valid service")
return
Expand Down
3 changes: 2 additions & 1 deletion cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ commonmeta put 10.5555/12345678 -f crossref -t inveniordm -h rogue-scholar.org -
to, _ := cmd.Flags().GetString("to")
host, _ := cmd.Flags().GetString("host")
token, _ := cmd.Flags().GetString("token")
legacyKey, _ := cmd.Flags().GetString("legacyKey")

cmd.SetOut(os.Stdout)
cmd.SetErr(os.Stderr)
Expand Down Expand Up @@ -111,7 +112,7 @@ commonmeta put 10.5555/12345678 -f crossref -t inveniordm -h rogue-scholar.org -
var output []byte
if to == "inveniordm" {
var record inveniordm.APIResponse
record, err = inveniordm.Upsert(record, host, token, data)
record, err = inveniordm.Upsert(record, host, token, legacyKey, data)
if err != nil {
cmd.PrintErr(err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ func init() {
rootCmd.PersistentFlags().StringP("registrant", "", "", "Crossref account registrant")
rootCmd.PersistentFlags().StringP("host", "", "", "InvenioRDM host")
rootCmd.PersistentFlags().StringP("token", "", "", "API token")
rootCmd.PersistentFlags().StringP("legacyKey", "", "", "Legacy API token")
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Commonmeta v0.5.27 -- HEAD")
cmd.Println("Commonmeta v0.5.28 -- HEAD")
},
}

Expand Down
7 changes: 7 additions & 0 deletions dateutils/dateutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ func GetDateFromDatetime(iso8601Time string) string {
return date.Format(Iso8601DateFormat)
}

// GetUnixTimestampFromDatetime returns a Unix timestamp from a datetime
func GetUnixTimestampFromDatetime(iso8601Time string) int64 {
time, _ := time.Parse(Iso8601DateTimeFormat, iso8601Time)
return time.Unix()
}

// StripMilliseconds removes milliseconds from an ISO 8601 datetime string
func StripMilliseconds(iso8601Time string) string {
if iso8601Time == "" {
return ""
Expand Down
11 changes: 9 additions & 2 deletions dateutils/dateutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ func ExampleGetDateFromUnixTimestamp() {
// 2021-01-22
}

func ExampleGetUnixTimestampFromDatetime() {
s := dateutils.GetUnixTimestampFromDatetime("2024-11-16T22:59:41Z")
fmt.Println(s)
// Output:
// 1731797981
}

func ExampleStripMilliseconds() {
s := dateutils.StripMilliseconds("2021-01-22T10:00:00.000Z")
s := dateutils.StripMilliseconds("2024-11-16T22:59:41.517474+00:00")
fmt.Println(s)
// Output:
// 2021-01-22T10:00:00Z
// 2024-11-16T22:59:41Z
}

func ExampleParseDate() {
Expand Down
38 changes: 35 additions & 3 deletions inveniordm/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"path/filepath"
"slices"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -380,7 +381,7 @@ func WriteAll(list []commonmeta.Data) ([]byte, []gojsonschema.ResultError) {
}

// Upsert updates or creates a record in InvenioRDM.
func Upsert(record APIResponse, host string, apiKey string, data commonmeta.Data) (APIResponse, error) {
func Upsert(record APIResponse, host string, apiKey string, legacyKey string, data commonmeta.Data) (APIResponse, error) {
inveniordm, err := Convert(data)
if err != nil {
return record, err
Expand Down Expand Up @@ -455,18 +456,26 @@ func Upsert(record APIResponse, host string, apiKey string, data commonmeta.Data
}
}

// update rogue-scholar legacy record if host is rogue-scholar.org
if host == "rogue-scholar.org" && legacyKey != "" {
record, err = UpdateLegacyRecord(record, legacyKey)
if err != nil {
return record, err
}
}

return record, nil
}

// UpsertAll updates or creates a list of records in InvenioRDM.
func UpsertAll(list []commonmeta.Data, host string, apiKey string) ([]byte, error) {
func UpsertAll(list []commonmeta.Data, host string, apiKey string, legacyKey string) ([]byte, error) {
var records []APIResponse

// iterate over list of records
for _, data := range list {
record := APIResponse{DOI: data.ID}

record, err := Upsert(record, host, apiKey, data)
record, err := Upsert(record, host, apiKey, legacyKey, data)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -696,3 +705,26 @@ func AddRecordToCommunity(record APIResponse, host string, apiKey string, commun
record.Status = "added_to_community"
return record, err
}

// UpdateLegacyRecord updates a record in Rogue Scholar legacy database.
func UpdateLegacyRecord(record APIResponse, legacyKey string) (APIResponse, error) {
now := strconv.FormatInt(time.Now().Unix(), 10)
var output = []byte(`{"doi":"` + record.DOI + `", "rid":"` + record.ID + `", "indexed_at":"` + now + `", "indexed":"true"}`)
requestURL := fmt.Sprintf("https://db.rogue-scholar.org/rest/v1/posts?id=eq.%s", record.UUID)
req, _ := http.NewRequest(http.MethodPatch, requestURL, bytes.NewReader(output))
req.Header = http.Header{
"Content-Type": {"application/json"},
"apikey": {legacyKey},
"Authorization": {"Bearer " + legacyKey},
"Prefer": {"return=minimal"},
}
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if resp.StatusCode != 204 {
return record, err
}
record.Status = "updated_legacy"
return record, nil
}

0 comments on commit e362e87

Please sign in to comment.